Skip to content

Commit

Permalink
Merge pull request #10 from lovedboy/master
Browse files Browse the repository at this point in the history
To be efficient in Go, minimize memory allocations
  • Loading branch information
jolestar committed Jan 12, 2016
2 parents 0d3f3ae + 5043541 commit 5bc7dcb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions collections/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ func (this *SyncIdentityMap) Size() int {
func (this *SyncIdentityMap) Values() []interface{} {
this.RLock()
defer this.RUnlock()
var list []interface{}
list := make([]interface{}, len(this.m))
i := 0
for _, v := range this.m {
list = append(list, v)
list[i] = v
i++
}
return list
}
13 changes: 13 additions & 0 deletions collections/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ func TestSyncMapString(t *testing.T) {
assert.Nil(t, m.Get(&key))
}

func TestSyncMapValues(t *testing.T) {
m := NewSyncMap()
key := "key"
key2 := "key2"
m.Put(&key, "value1")
m.Put(&key2, "value2")
values := m.Values()
assert.Equal(t, 2, len(values))
assert.Equal(t, "value1", values[0])
assert.Equal(t, "value2", values[1])

}

func TestSyncMapHashableObject(t *testing.T) {
m := NewSyncMap()
o1 := HashableObject{}
Expand Down

0 comments on commit 5bc7dcb

Please sign in to comment.