github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/persistent/hashmap/map.go (about) 1 package hashmap 2 3 import "encoding/json" 4 5 // Map is a persistent associative data structure mapping keys to values. It 6 // is immutable, and supports near-O(1) operations to create modified version of 7 // the map that shares the underlying data structure. Because it is immutable, 8 // all of its methods are safe for concurrent use. 9 type Map interface { 10 json.Marshaler 11 // Len returns the length of the map. 12 Len() int 13 // Index returns whether there is a value associated with the given key, and 14 // that value or nil. 15 Index(k interface{}) (interface{}, bool) 16 // Assoc returns an almost identical map, with the given key associated with 17 // the given value. 18 Assoc(k, v interface{}) Map 19 // Dissoc returns an almost identical map, with the given key associated 20 // with no value. 21 Dissoc(k interface{}) Map 22 // Iterator returns an iterator over the map. 23 Iterator() Iterator 24 } 25 26 // Iterator is an iterator over map elements. It can be used like this: 27 // 28 // for it := m.Iterator(); it.HasElem(); it.Next() { 29 // key, value := it.Elem() 30 // // do something with elem... 31 // } 32 type Iterator interface { 33 // Elem returns the current key-value pair. 34 Elem() (interface{}, interface{}) 35 // HasElem returns whether the iterator is pointing to an element. 36 HasElem() bool 37 // Next moves the iterator to the next position. 38 Next() 39 } 40 41 // HasKey reports whether a Map has the given key. 42 func HasKey(m Map, k interface{}) bool { 43 _, ok := m.Index(k) 44 return ok 45 }