|
| 1 | +/* |
| 2 | +#137 |
| 3 | +Amazon |
| 4 | +
|
| 5 | +A bit array is a space efficient array that holds a value of 1 or 0 at each index. |
| 6 | + - init(size): initialize the array with size |
| 7 | + - set(i, val): updates index at i with val where val is either 1 or 0 |
| 8 | + - get(i): gets the value at index i |
| 9 | +
|
| 10 | +Solution reference: https://web.archive.org/web/20181015104504/http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/bit-array.html |
| 11 | +*/ |
| 12 | +package main |
| 13 | + |
| 14 | +import ( |
| 15 | + "errors" |
| 16 | + "fmt" |
| 17 | + "math" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + indexError = errors.New("Error: Index out of range.") |
| 22 | +) |
| 23 | + |
| 24 | +type BitArray struct { |
| 25 | + array []int32 |
| 26 | + size uint |
| 27 | +} |
| 28 | + |
| 29 | +func (bitArray *BitArray) init(neededSize uint) { |
| 30 | + implementationSize := int(math.Ceil(float64(neededSize) / 32.0)) |
| 31 | + bitArray.array = make([]int32, implementationSize) |
| 32 | + bitArray.size = neededSize |
| 33 | + |
| 34 | + for index := 0; index < implementationSize; index++ { |
| 35 | + bitArray.array[index] = 0 |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (bitArray *BitArray) set(i, val uint) error { |
| 40 | + implementationIndex := uint(i / 32) |
| 41 | + if i >= bitArray.size { |
| 42 | + return indexError |
| 43 | + } |
| 44 | + |
| 45 | + var temp int32 = 1 |
| 46 | + if val == 1 { |
| 47 | + temp = (temp << (i % 32)) |
| 48 | + bitArray.array[implementationIndex] |= temp |
| 49 | + } else { |
| 50 | + temp = ^(temp << (i % 32)) |
| 51 | + bitArray.array[implementationIndex] &= temp |
| 52 | + } |
| 53 | + |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func (bitArray BitArray) get(i uint) (int32, error) { |
| 58 | + implementationIndex := uint(i / 32) |
| 59 | + if i >= bitArray.size { |
| 60 | + return 0, indexError |
| 61 | + } |
| 62 | + |
| 63 | + var temp int32 = 1 |
| 64 | + if bitArray.array[implementationIndex]&(temp<<i%32) != 0 { |
| 65 | + return 1, nil |
| 66 | + } else { |
| 67 | + return 0, nil |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (bitArray *BitArray) print() { |
| 72 | + for _, val := range bitArray.array { |
| 73 | + op := fmt.Sprintf("%b", val) |
| 74 | + |
| 75 | + for len(op) != 32 { |
| 76 | + op = "0" + op |
| 77 | + } |
| 78 | + |
| 79 | + fmt.Print(op, " ") |
| 80 | + } |
| 81 | + fmt.Println() |
| 82 | +} |
| 83 | + |
| 84 | +func main() { |
| 85 | + var bitArray1 BitArray |
| 86 | + bitArray1.init(10) |
| 87 | + // 00000000000000000000000000000000 |
| 88 | + |
| 89 | + bitArray1.set(0, 1) |
| 90 | + bitArray1.print() |
| 91 | + // 00000000000000000000000000000001 |
| 92 | + |
| 93 | + bitArray1.set(1, 1) |
| 94 | + bitArray1.print() |
| 95 | + // 00000000000000000000000000000011 |
| 96 | + |
| 97 | + bitArray1.set(2, 1) |
| 98 | + bitArray1.print() |
| 99 | + // 00000000000000000000000000000111 |
| 100 | + |
| 101 | + bitArray1.set(1, 0) |
| 102 | + bitArray1.print() |
| 103 | + // 00000000000000000000000000000101 |
| 104 | + |
| 105 | + if err := bitArray1.set(10, 1); err != nil { |
| 106 | + fmt.Println(err.Error()) |
| 107 | + } |
| 108 | + // Error: Index out of range. |
| 109 | + |
| 110 | + var bitArray2 BitArray |
| 111 | + bitArray2.init(35) |
| 112 | + // 00000000000000000000000000000000 00000000000000000000000000000000 |
| 113 | + |
| 114 | + if err := bitArray2.set(10, 1); err != nil { |
| 115 | + fmt.Println(err.Error()) |
| 116 | + } |
| 117 | + bitArray2.print() |
| 118 | + // 00000000000000000000010000000000 00000000000000000000000000000000 |
| 119 | + |
| 120 | + bitArray2.set(33, 1) |
| 121 | + bitArray2.print() |
| 122 | + // 00000000000000000000010000000000 00000000000000000000000000000010 |
| 123 | + |
| 124 | + if err := bitArray2.set(35, 1); err != nil { |
| 125 | + fmt.Println(err.Error()) |
| 126 | + } |
| 127 | + // Error: Index out of range. |
| 128 | +} |
0 commit comments