|
| 1 | +package hashing |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha1" |
| 5 | + "errors" |
| 6 | + "hash" |
| 7 | + "strconv" |
| 8 | +) |
| 9 | + |
| 10 | +type Node struct { |
| 11 | + key string |
| 12 | + value interface{} |
| 13 | +} |
| 14 | + |
| 15 | +type HashMap struct { |
| 16 | + count int |
| 17 | + size int |
| 18 | + buckets [][]Node |
| 19 | +} |
| 20 | + |
| 21 | +func getHash(s string) int { |
| 22 | + // hsha2 := sha256.Sum256([]byte(s)) |
| 23 | + // fmt.Println(hsha2) |
| 24 | + // hashbytes := hsha2.Sum(nil) |
| 25 | + // var mySlice = []byte{244, 244, 244, 244, 244, 244, 244, 244} |
| 26 | + // data := binary.BigEndian.Uint64(mySlice) |
| 27 | + // hash, err := strconv.Atoi(fmt.Sprintf("%s", hsha2)) |
| 28 | + // if err != nil { |
| 29 | + // fmt.Printf("Error in getHash function: %v", err) |
| 30 | + // return -1 |
| 31 | + // } |
| 32 | + // return hash |
| 33 | + |
| 34 | + var hashVal hash.Hash |
| 35 | + hashVal = sha1.New() |
| 36 | + hashVal.Write([]byte(s)) |
| 37 | + |
| 38 | + var bytes []byte |
| 39 | + |
| 40 | + bytes = hashVal.Sum(nil) |
| 41 | + byteToInt, _ := strconv.Atoi(string(bytes)) |
| 42 | + return byteToInt |
| 43 | +} |
| 44 | + |
| 45 | +func (h *HashMap) getIndex(key string) int { |
| 46 | + hashInt := getHash(key) |
| 47 | + if hashInt != -1 { |
| 48 | + return getHash(key) % h.size |
| 49 | + } |
| 50 | + return -1 |
| 51 | +} |
| 52 | + |
| 53 | +func (h *HashMap) getValue(key string) (*Node, bool) { |
| 54 | + index := h.getIndex(key) |
| 55 | + if index == -1 { |
| 56 | + return nil, false |
| 57 | + } |
| 58 | + chain := h.buckets[index] |
| 59 | + |
| 60 | + for _, node := range chain { |
| 61 | + if node.key == key { |
| 62 | + return &node, true |
| 63 | + } |
| 64 | + } |
| 65 | + return nil, false |
| 66 | +} |
| 67 | + |
| 68 | +func (h *HashMap) putValue(key string, value interface{}) bool { |
| 69 | + index := h.getIndex(key) |
| 70 | + if index == -1 { |
| 71 | + return false |
| 72 | + } |
| 73 | + chain := h.buckets[index] |
| 74 | + |
| 75 | + //found := false |
| 76 | + |
| 77 | + for i := range chain { |
| 78 | + node := &chain[i] |
| 79 | + if node.key == key { |
| 80 | + node.value = value |
| 81 | + return true |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if h.size == h.count { |
| 86 | + return false |
| 87 | + } |
| 88 | + |
| 89 | + node := Node{key: key, value: value} |
| 90 | + chain = append(chain, node) |
| 91 | + h.buckets[index] = chain |
| 92 | + h.count++ |
| 93 | + return true |
| 94 | +} |
| 95 | + |
| 96 | +func createHashMap(expectedSize int) (*HashMap, error) { |
| 97 | + if expectedSize < 1 { |
| 98 | + return nil, errors.New("Size of the hashmap cannot be less than 1") |
| 99 | + } |
| 100 | + b := make([][]Node, expectedSize) |
| 101 | + for i := 0; i < expectedSize; i++ { |
| 102 | + b[i] = make([]Node, 0) |
| 103 | + } |
| 104 | + h := &HashMap{ |
| 105 | + count: 0, |
| 106 | + size: expectedSize, |
| 107 | + buckets: b, |
| 108 | + } |
| 109 | + return h, nil |
| 110 | +} |
| 111 | + |
| 112 | +func (h *HashMap) getCount() int { |
| 113 | + return h.count |
| 114 | +} |
| 115 | + |
| 116 | +func (h *HashMap) getsize() int { |
| 117 | + return h.size |
| 118 | +} |
0 commit comments