loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.1242231845500088","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"cGFja2FnZSBtYWluCgppbXBvcnQgKAogICJmbXQiCiAgImJ5dGVzIgogICkKCmNvbnN0IE1BUF9TSVpFID0gNTAKCnR5cGUgTm9kZSBzdHJ1Y3QgewogIGtleSBzdHJpbmcKICB2YWx1ZSBzdHJpbmcKICBuZXh0ICpOb2RlCn0KCnR5cGUgSGFzaE1hcCBzdHJ1Y3QgewogIERhdGEgW10qTm9kZQp9CgpmdW5jIE5ld0RpY3QoKSAqSGFzaE1hcCB7CiAgcmV0dXJuICZIYXNoTWFweyBEYXRhOiBtYWtlKFtdKk5vZGUsIE1BUF9TSVpFKSB9IAp9CgovLyBmdW5jIChuICpOb2RlKSBTdHJpbmcoKSBzdHJpbmcgewovLyAgIHJldHVybiBmbXQuU3ByaW50ZigiPEtleTogJXMsIFZhbHVlOiAlcz5cbiIsIG4ua2V5LCBuLnZhbHVlKQovLyB9CgovLyBmdW5jIChoICpIYXNoTWFwKSBTdHJpbmcoKSBzdHJpbmcgewovLyAgIHZhciBvdXRwdXQgYnl0ZXMuQnVmZmVyCi8vICAgZm10LkZwcmludGxuKCZvdXRwdXQsICJ7IikKLy8gICBmb3IgXywgbiA6PSByYW5nZSBoLkRhdGEgewovLyAgICAgaWYgbiAhPSBuaWwgewovLyAgICAgICBmbXQuRnByaW50Zigmb3V0cHV0LCAiXHQlczogJXNcbiIsIG4ua2V5LCBuLnZhbHVlKQovLyAgICAgICBmb3Igbm9kZSA6PSBuLm5leHQ7IG5vZGUgIT0gbmlsOyBub2RlID0gbm9kZS5uZXh0IHsKLy8gICAgICAgICBmbXQuRnByaW50Zigmb3V0cHV0LCAiXHQlczogJXNcbiIsIG5vZGUua2V5LCBub2RlLnZhbHVlKQovLyAgICAgICB9Ci8vICAgICB9Ci8vICAgfQogIAovLyAgIGZtdC5GcHJpbnRsbigmb3V0cH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c2VydCgibGFzdG5hbWUiLCAia2hhcmUiKQogIGlmIHZhbHVlLCBvayA6PSBhLkdldCgibmFtZSIpOyBvayB7CiAgICBmbXQuUHJpbnRsbih2YWx1ZSk7CiAgfSBlbHNlIHsKICAgIGZtdC5QcmludGxuKCJWYWx1ZSBkaWQgbm90IG1hdGNoISIpCiAgfQogIC8vZm10LlByaW50bG4oIiVxIiwgYSkKICAKICBmbXQuUHJpbnRsbihhKQp9"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main

import (
  "fmt"
  "bytes"
  )

const MAP_SIZE = 50

type Node struct {
  key string
  value string
  next *Node
}

type HashMap struct {
  Data []*Node
}

func NewDict() *HashMap {
  return &HashMap{ Data: make([]*Node, MAP_SIZE) } 
}

// func (n *Node) String() string {
//   return fmt.Sprintf("<Key: %s, Value: %s>\n", n.key, n.value)
// }

// func (h *HashMap) String() string {
//   var output bytes.Buffer
//   fmt.Fprintln(&output, "{")
//   for _, n := range h.Data {
//     if n != nil {
//       fmt.Fprintf(&output, "\t%s: %s\n", n.key, n.value)
//       for node := n.next; node != nil; node = node.next {
//         fmt.Fprintf(&output, "\t%s: %s\n", node.key, node.value)
//       }
//     }
//   }
  
//   fmt.Fprintln(&output, "}")
  
//   return output.String()
// }

func (h *HashMap) Insert(key string, value string) {
  index := getIndex(key)
  
  if h.Data[index] == nil {
    // index is empty, go ahead and insert
    h.Data[index] = &Node{key: key, value: value}
  } else {
    // there is a collision, get into linked-list mode
    starting_node := h.Data[index]
    for ; starting_node.next != nil; starting_node = starting_node.next {
      if starting_node.key == key {
        // the key exists, its a modifying operation
        starting_node.value = value
        return
      }
    }
    starting_node.next = &Node{key: key, value: value}
  }
}

func (h *HashMap) Get(key string) (string, bool) {
  index := getIndex(key)
  if h.Data[index] != nil {
    // key is on this index, but might be somewhere in linked list
    starting_node := h.Data[index]
    for ; ; starting_node = starting_node.next {
      if starting_node.key == key {
        // key matched
        return starting_node.value, true
      }
      
      if starting_node.next == nil {
        break
      }
    }
  }

  // key does not exists
  return "", false
}

func hash(key string) (hash uint8) {
  // a jenkins one-at-a-time-hash
  // refer https://en.wikipedia.org/wiki/Jenkins_hash_function

  hash = 0
  for _, ch := range key {
    hash += uint8(ch)
    hash += hash << 10
    hash ^= hash >> 6
  }
  
  hash += hash << 3
  hash ^= hash >> 11
  hash += hash << 15
  
  return 
}

func getIndex(key string) (index int) {
  return int(hash(key)) % MAP_SIZE
}

func main() {
  a := NewDict()
  a.Insert("name", "ishan")
  a.Insert("gender", "male")
  a.Insert("city", "mumbai")
  a.Insert("lastname", "khare")
  if value, ok := a.Get("name"); ok {
    fmt.Println(value);
  } else {
    fmt.Println("Value did not match!")
  }
  //fmt.Println("%q", a)
  
  fmt.Println(a)
}