bitbucket.org/ai69/amoy@v0.2.3/range.go (about)

     1  package amoy
     2  
     3  // EnsureRange returns the value if it's between min and max, otherwise it returns min or max, whichever is closer. It panics if min is greater than max.
     4  func EnsureRange(value, min, max int) int {
     5  	if min > max {
     6  		panic("min cannot be greater than max")
     7  	}
     8  	if value < min {
     9  		return min
    10  	}
    11  	if value > max {
    12  		return max
    13  	}
    14  	return value
    15  }
    16  
    17  // UpdateMinMaxFloat64 updates the min and max values if the value is less than the current min or greater than the current max.
    18  //
    19  // The function takes three arguments:
    20  //
    21  // * value: The value to compare against the current min and max.
    22  // * min: A pointer to the current min value.
    23  // * max: A pointer to the current max value.
    24  func UpdateMinMaxFloat64(value float64, min, max *float64) {
    25  	if value < *min {
    26  		*min = value
    27  	}
    28  	if value > *max {
    29  		*max = value
    30  	}
    31  }
    32  
    33  // UpdateMinMaxInt updates the min and max values if the value is less than the current min or greater than the current max.
    34  //
    35  // The function takes three arguments:
    36  //
    37  // * value: The value to compare against the current min and max.
    38  // * min: A pointer to the current min value.
    39  // * max: A pointer to the current max value.
    40  func UpdateMinMaxInt(value int, min, max *int) {
    41  	if value < *min {
    42  		*min = value
    43  	}
    44  	if value > *max {
    45  		*max = value
    46  	}
    47  }