|
| 1 | +package convutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +type BitSetConvert struct{} |
| 11 | + |
| 12 | +func (BitSetConvert) Enable() bool { |
| 13 | + return true |
| 14 | +} |
| 15 | + |
| 16 | +func (BitSetConvert) Encode(str string) (string, bool) { |
| 17 | + var result strings.Builder |
| 18 | + |
| 19 | + str = strings.ReplaceAll(str, "\r\n", "\n") // CRLF → LF |
| 20 | + str = strings.ReplaceAll(str, "\r", "\n") // CR → LF |
| 21 | + |
| 22 | + lines := strings.Split(str, "\n") |
| 23 | + bytes := EncodeToRedisBitset(lines) |
| 24 | + result.Write(bytes) |
| 25 | + |
| 26 | + return result.String(), true |
| 27 | +} |
| 28 | + |
| 29 | +// EncodeToRedisBitset encodes a list of strings with integers (positions) into a Redis bitset byte array. |
| 30 | +// The bit at position 'n' will be set to 1 if n is in the input array. |
| 31 | +// The resulting byte slice can be stored in Redis using SET command. |
| 32 | +func EncodeToRedisBitset(numbers []string) []byte { |
| 33 | + if len(numbers) == 0 { |
| 34 | + return []byte{} |
| 35 | + } |
| 36 | + |
| 37 | + // Find the maximum number to determine the required bit length and convert strings to numbers |
| 38 | + maxNum := uint64(0) |
| 39 | + var validNumbers []uint64 |
| 40 | + for _, s := range numbers { |
| 41 | + if s == "" { |
| 42 | + continue |
| 43 | + } |
| 44 | + num, err := strconv.ParseUint(s, 10, 64) |
| 45 | + if err != nil || num < 0 || num > math.MaxUint32 { |
| 46 | + fmt.Printf("Warning: skipping invalid number '%s': %v\n", s, err) |
| 47 | + continue |
| 48 | + } |
| 49 | + validNumbers = append(validNumbers, num) |
| 50 | + if num > maxNum { |
| 51 | + maxNum = num |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if len(validNumbers) == 0 { |
| 56 | + return []byte{} |
| 57 | + } |
| 58 | + |
| 59 | + // Calculate required byte length (8 bits per byte) |
| 60 | + byteLen := ((maxNum + 7) / 8) + 1 |
| 61 | + |
| 62 | + // Initialize byte array |
| 63 | + bitset := make([]byte, byteLen) |
| 64 | + |
| 65 | + // Set bits for each number |
| 66 | + for _, num := range validNumbers { |
| 67 | + byteIndex := num / 8 |
| 68 | + if byteIndex < byteLen { |
| 69 | + bitIndex := uint(num % 8) |
| 70 | + // Set the bit (big-endian bit order within byte) |
| 71 | + bitset[byteIndex] |= 1 << (7 - bitIndex) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return bitset |
| 76 | +} |
| 77 | + |
| 78 | +func (BitSetConvert) Decode(str string) (string, bool) { |
| 79 | + bitset := getBitSet([]byte(str)) |
| 80 | + |
| 81 | + var binBuilder strings.Builder |
| 82 | + for key, value := range bitset { |
| 83 | + if value { |
| 84 | + binBuilder.WriteString(fmt.Sprintf("%v\n", key)) |
| 85 | + //binBuilder.WriteString(fmt.Sprintf("Bit %v = %v \n", key, value)) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return binBuilder.String(), true |
| 90 | +} |
| 91 | + |
| 92 | +func getBitSet(redisResponse []byte) []bool { |
| 93 | + bitset := make([]bool, len(redisResponse)*8) |
| 94 | + for i := range redisResponse { |
| 95 | + for j := 7; j >= 0; j-- { |
| 96 | + bit_n := uint(i*8 + (7 - j)) |
| 97 | + bitset[bit_n] = (redisResponse[i] & (1 << uint(j))) > 0 |
| 98 | + } |
| 99 | + } |
| 100 | + return bitset |
| 101 | +} |
0 commit comments