github.com/pavlo67/common@v0.5.3/common/mathlib/sets/connection.go (about)

     1  package sets
     2  
     3  func Intersect[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64](from1, to1, from2, to2 T) bool {
     4  	if from1 < from2 {
     5  		return from2 <= to1
     6  	}
     7  	return from1 <= to2
     8  }
     9  
    10  func Intersection[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64](from1, to1, from2, to2 T) *[2]T {
    11  	if from1 < from2 {
    12  		if to1 < from2 {
    13  			return nil
    14  		} else if to1 <= to2 {
    15  			return &[2]T{from2, to1}
    16  		}
    17  		return &[2]T{from2, to2}
    18  	}
    19  
    20  	// else from2 <= from1
    21  
    22  	if to2 < from1 {
    23  		return nil
    24  	} else if to2 <= to1 {
    25  		return &[2]T{from1, to2}
    26  	}
    27  	return &[2]T{from1, to1}
    28  }
    29  
    30  func Connection[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64](from1, to1, from2, to2 T) *[2]T {
    31  	if from1 < from2 {
    32  		if to1 < from2 {
    33  			return nil
    34  		} else if to1 <= to2 {
    35  			return &[2]T{from1, to2}
    36  		}
    37  		return &[2]T{from1, to1}
    38  	}
    39  
    40  	// else from2 <= from1
    41  
    42  	if to2 < from1 {
    43  		return nil
    44  	} else if to2 <= to1 {
    45  		return &[2]T{from2, to1}
    46  	}
    47  	return &[2]T{from2, to2}
    48  }