diff options
Diffstat (limited to 'test/hashmap.go')
-rwxr-xr-x | test/hashmap.go | 123 |
1 files changed, 61 insertions, 62 deletions
diff --git a/test/hashmap.go b/test/hashmap.go index 62943a713..0a4d7ab61 100755 --- a/test/hashmap.go +++ b/test/hashmap.go @@ -11,7 +11,7 @@ package main func ASSERT(p bool) { if !p { - // panic 0; + // panic 0 } } @@ -20,8 +20,8 @@ func ASSERT(p bool) { // Implementation of the HashMap type KeyType interface { - Hash() uint32; - Match(other *KeyType) bool + Hash() uint32 + Match(other KeyType) bool } @@ -31,31 +31,30 @@ type ValueType interface { type Entry struct { - key *KeyType; - value *ValueType; + key KeyType + value ValueType } -// Using the Array type below doesn't seem to work -//type Array array [1024] Entry; +type Array [1024]Entry type HashMap struct { - map_ *[1024] Entry; - log2_capacity_ uint32; - occupancy_ uint32; + map_ *Array + log2_capacity_ uint32 + occupancy_ uint32 } func (m *HashMap) capacity() uint32 { - return 1 << m.log2_capacity_; + return 1 << m.log2_capacity_ } func (m *HashMap) Clear() { // Mark all entries as empty. - var i uint32 = m.capacity() - 1; + var i uint32 = m.capacity() - 1 for i > 0 { - m.map_[i].key = nil; + m.map_[i].key = nil i = i - 1 } m.occupancy_ = 0 @@ -63,72 +62,72 @@ func (m *HashMap) Clear() { func (m *HashMap) Initialize (initial_log2_capacity uint32) { - m.log2_capacity_ = initial_log2_capacity; - m.map_ = new([1024] Entry); - m.Clear(); + m.log2_capacity_ = initial_log2_capacity + m.map_ = new(Array) + m.Clear() } -func (m *HashMap) Probe (key *KeyType) *Entry { - ASSERT(key != nil); +func (m *HashMap) Probe (key KeyType) *Entry { + ASSERT(key != nil) - var i uint32 = key.Hash() % m.capacity(); - ASSERT(0 <= i && i < m.capacity()); + var i uint32 = key.Hash() % m.capacity() + ASSERT(0 <= i && i < m.capacity()) - ASSERT(m.occupancy_ < m.capacity()); // guarantees loop termination + ASSERT(m.occupancy_ < m.capacity()) // guarantees loop termination for m.map_[i].key != nil && !m.map_[i].key.Match(key) { - i++; + i++ if i >= m.capacity() { - i = 0; + i = 0 } } - return &m.map_[i]; + return &m.map_[i] } -func (m *HashMap) Lookup (key *KeyType, insert bool) *Entry { +func (m *HashMap) Lookup (key KeyType, insert bool) *Entry { // Find a matching entry. - var p *Entry = m.Probe(key); + var p *Entry = m.Probe(key) if p.key != nil { - return p; + return p } // No entry found; insert one if necessary. if insert { - p.key = key; - p.value = nil; - m.occupancy_++; + p.key = key + p.value = nil + m.occupancy_++ // Grow the map if we reached >= 80% occupancy. if m.occupancy_ + m.occupancy_/4 >= m.capacity() { - m.Resize(); - p = m.Probe(key); + m.Resize() + p = m.Probe(key) } - return p; + return p } // No entry found and none inserted. - return nil; + return nil } func (m *HashMap) Resize() { - var hmap *[1024] Entry = m.map_; - var n uint32 = m.occupancy_; + var hmap *Array = m.map_ + var n uint32 = m.occupancy_ // Allocate a new map of twice the current size. - m.Initialize(m.log2_capacity_ << 1); + m.Initialize(m.log2_capacity_ << 1) // Rehash all current entries. - var i uint32 = 0; + var i uint32 = 0 for n > 0 { if hmap[i].key != nil { - m.Lookup(hmap[i].key, true).value = hmap[i].value; - n = n - 1; + m.Lookup(hmap[i].key, true).value = hmap[i].value + n = n - 1 } - i++; + i++ } } @@ -137,46 +136,46 @@ func (m *HashMap) Resize() { // Test code type Number struct { - x uint32; + x uint32 } func (n *Number) Hash() uint32 { - return n.x * 23; + return n.x * 23 } -func (n *Number) Match(other *KeyType) bool { - // var y *Number = other; - // return n.x == y.x; - return false; +func (n *Number) Match(other KeyType) bool { + // var y *Number = other + // return n.x == y.x + return false } func MakeNumber (x uint32) *Number { - var n *Number = new(Number); - n.x = x; - return n; + var n *Number = new(Number) + n.x = x + return n } func main() { - //f unc (n int) int { return n + 1; }(1); + // func (n int) int { return n + 1; }(1) - //print "HashMap - gri 2/8/2008\n"; + //print "HashMap - gri 2/8/2008\n" - var hmap *HashMap = new(HashMap); - hmap.Initialize(0); + var hmap *HashMap = new(HashMap) + hmap.Initialize(0) - var x1 *Number = MakeNumber(1001); - var x2 *Number = MakeNumber(2002); - var x3 *Number = MakeNumber(3003); - _, _, _ = x1, x2, x3; + var x1 *Number = MakeNumber(1001) + var x2 *Number = MakeNumber(2002) + var x3 *Number = MakeNumber(3003) + _, _, _ = x1, x2, x3 // this doesn't work I think... - //hmap.Lookup(x1, true); - //hmap.Lookup(x2, true); - //hmap.Lookup(x3, true); + //hmap.Lookup(x1, true) + //hmap.Lookup(x2, true) + //hmap.Lookup(x3, true) - //print "done\n"; + //print "done\n" } |