structure/map/hashmap/hashmap.go

64 lines
1.1 KiB
Go
Raw Normal View History

2019-04-16 19:22:07 +00:00
package hashmap
2019-05-07 10:28:45 +00:00
import "fmt"
2019-04-16 19:22:07 +00:00
type HashMap struct {
2019-05-07 10:28:45 +00:00
hm map[interface{}]interface{}
}
2019-05-07 05:59:29 +00:00
2019-05-07 10:28:45 +00:00
// New instantiates a hash map.
func New() *HashMap {
return &HashMap{hm: make(map[interface{}]interface{})}
2019-04-18 01:39:44 +00:00
}
2019-05-07 10:28:45 +00:00
// Put inserts element into the map.
func (hm *HashMap) Put(key interface{}, value interface{}) {
hm.hm[key] = value
2019-04-16 19:22:07 +00:00
}
2019-05-07 10:28:45 +00:00
func (hm *HashMap) Get(key interface{}) (value interface{}, isfound bool) {
value, isfound = hm.hm[key]
return
2019-04-16 19:22:07 +00:00
}
2019-05-07 10:28:45 +00:00
func (hm *HashMap) Remove(key interface{}) {
delete(hm.hm, key)
2019-04-21 20:46:07 +00:00
}
2019-05-07 10:28:45 +00:00
func (hm *HashMap) Empty() bool {
return len(hm.hm) == 0
}
2019-04-22 06:06:19 +00:00
2019-05-07 10:28:45 +00:00
func (hm *HashMap) Size() int {
return len(hm.hm)
}
2019-04-22 06:06:19 +00:00
2019-05-07 10:28:45 +00:00
func (hm *HashMap) Keys() []interface{} {
keys := make([]interface{}, len(hm.hm))
count := 0
for key := range hm.hm {
keys[count] = key
count++
2019-04-21 05:31:39 +00:00
}
2019-05-07 10:28:45 +00:00
return keys
2019-04-21 05:31:39 +00:00
}
2019-05-07 10:28:45 +00:00
func (hm *HashMap) Values() []interface{} {
values := make([]interface{}, len(hm.hm))
count := 0
for _, value := range hm.hm {
values[count] = value
count++
2019-05-07 05:59:29 +00:00
}
2019-05-07 10:28:45 +00:00
return values
2019-04-21 20:46:07 +00:00
}
2019-04-21 05:31:39 +00:00
2019-05-07 10:28:45 +00:00
func (hm *HashMap) Clear() {
hm.hm = make(map[interface{}]interface{})
}
2019-04-22 06:06:19 +00:00
2019-05-07 10:28:45 +00:00
func (hm *HashMap) String() string {
str := fmt.Sprintf("%v", hm.hm)
return str
2019-04-16 19:22:07 +00:00
}