Last active
August 29, 2015 14:05
-
-
Save nanoninja/38d7cbc8fba8b385b792 to your computer and use it in GitHub Desktop.
ArrayList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type ( | |
ListKey string | |
ListVal interface{} | |
) | |
type ArrayList struct { | |
list map[ListKey]ListVal | |
} | |
func NewArrayList() *ArrayList { | |
return &ArrayList{make(map[ListKey]ListVal)} | |
} | |
func (a *ArrayList) Set(k ListKey, v ListVal) { | |
a.Clear() | |
a.Add(k, v) | |
} | |
func (a *ArrayList) Add(k ListKey, v ListVal) { | |
a.list[k] = v | |
} | |
func (a *ArrayList) Get(k ListKey) ListVal { | |
if a.Has(k) { | |
return a.list[k] | |
} | |
return nil | |
} | |
func (a *ArrayList) Del(k ListKey) { | |
delete(a.list, k) | |
} | |
func (a *ArrayList) Clear() { | |
a.list = make(map[ListKey]ListVal) | |
} | |
func (a ArrayList) Has(k ListKey) bool { | |
_, ok := a.list[k] | |
return ok | |
} | |
func (a *ArrayList) Merge(list *ArrayList) { | |
for k, v := range a.list { | |
a.Add(k, v) | |
} | |
} | |
func (a ArrayList) Count() int { | |
return len(a.list) | |
} | |
func (a *ArrayList) ReplaceKey(oldKey, newKey ListKey) { | |
if oldKey == newKey { | |
return | |
} | |
a.Add(newKey, a.Get(oldKey)) | |
a.Del(oldKey) | |
} | |
func (a ArrayList) Keys() []ListKey { | |
keys := make([]ListKey, 0, len(a.list)) | |
for k, _ := range a.list { | |
keys = append(keys, k) | |
} | |
return keys | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment