github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/cols/bitarray/inplace_bi_operations.go (about)

     1  package bitarray
     2  
     3  type bitwiseOperation func(a, b uint8) uint8
     4  
     5  func (array *Array) applyBiOperation(other *Array, operation bitwiseOperation) {
     6  	if array.Size() != other.Size() {
     7  		panic("Array sizes don't match")
     8  	}
     9  
    10  	for i, val := range array.slice {
    11  		array.slice[i] = operation(val, other.slice[i])
    12  	}
    13  }
    14  
    15  // And performs an in-place bitwise AND operation with the other array.
    16  func (array *Array) And(other *Array) {
    17  	array.applyBiOperation(other, func(a, b uint8) uint8 {
    18  		return a & b
    19  	})
    20  }
    21  
    22  // Or performs an in-place bitwise OR operation with the other array.
    23  func (array *Array) Or(other *Array) {
    24  	array.applyBiOperation(other, func(a, b uint8) uint8 {
    25  		return a | b
    26  	})
    27  }
    28  
    29  // Xor performs an in-place bitwise XOR operation with the other array.
    30  func (array *Array) Xor(other *Array) {
    31  	array.applyBiOperation(other, func(a, b uint8) uint8 {
    32  		return a ^ b
    33  	})
    34  }