Documentation
¶
Overview ¶
simplemap is a simpler implementation of jsonmap, but with O(N) Delete operation. Here's comparison of time complexity of operations, with differences marked with __bold__:
| Operation | jsonmap | simplemap | |-----------|-------------|-------------| | Clear | O(1) | O(1) | | Get | O(1) | O(1) | | Set | O(1) | O(1) | | Delete | O(1) | __O(N)__ | | Push | O(1) | O(1) | | Pop | O(1) | O(1) | | | | | | First | O(1) | O(1) | | Last | O(1) | O(1) | | GetElement| O(1) | __O(N)__ | | el.Next | O(1) | O(1) | | el.Prev | O(1) | O(1) | | | | | | SetFront | O(1) | __O(N)__ | | PushFront | O(1) | __O(N)__ | | PopFront | O(1) | __O(N)__ | | | | | | KeyIndex | O(N) | O(N) | | Keys | O(N) | __O(1)__ | | Values | O(N) | O(N) | | SortKeys | O(N*log(N)) | O(N*log(N)) |
Index ¶
- func GetAs[T any](m *Map, key Key) (value T, ok bool)
- type Key
- type Map
- func (m *Map) Clear()
- func (m *Map) Delete(key Key)
- func (m *Map) First() jsonmap.Element
- func (m *Map) Get(key Key) (value Value, ok bool)
- func (m *Map) GetElement(key Key) jsonmap.Element
- func (m *Map) KeyIndex(key Key) int
- func (m *Map) Keys() []Key
- func (m *Map) Last() jsonmap.Element
- func (m *Map) Len() int
- func (m *Map) MarshalJSON() ([]byte, error)
- func (m *Map) Pop() (key Key, value Value, ok bool)
- func (m *Map) PopFront() (key Key, value Value, ok bool)
- func (m *Map) Push(key Key, value Value)
- func (m *Map) PushFront(key Key, value Value)
- func (m *Map) Set(key Key, value Value)
- func (m *Map) SetFront(key Key, value Value)
- func (m *Map) SortKeys(less func(a, b Key) bool)
- func (m *Map) String() string
- func (m *Map) UnmarshalJSON(data []byte) error
- func (m *Map) Values() []Value
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is a map[string]any with saved order of elements. It is useful for marshaling/unmarshaling JSON objects. This is a simplified version of jsonmap.Map, but with O(n) Delete.
func (*Map) Clear ¶
func (m *Map) Clear()
Clear removes all elements from the map. O(1) time.
m.Clear()
func (*Map) Delete ¶
Delete deletes the key from the map. O(n) time if the key exists, because it uses KeyIndex(). O(1) time if the key does not exist.
m.Delete(key)
func (*Map) First ¶
First returns the first element in the map, for iteration. Returns nil if the map is empty. O(1) time.
for elem := m.First(); elem != nil; elem = elem.Next() {
fmt.Println(elem.Key(), elem.Value())
}
func (*Map) Get ¶
Get returns the value for the key. Returns ok=false if the key is not in the map. O(1) time.
value, ok := m.Get(key)
func (*Map) GetElement ¶
GetElement returns the element for the key, for iteration from a needle. Returns nil if the key is not in the map. O(n) for existing keys, because it uses KeyIndex().
func (*Map) KeyIndex ¶
KeyIndex returns index of key. O(n) time. If key is not in the map, it returns -1. O(n) time.
i := m.KeyIndex(key)
func (*Map) Keys ¶
Keys returns all keys in the map. O(n) time and O(n) space. O(1) time, as keys slice is stored in the map.
keys := m.Keys()
func (*Map) Last ¶
Last returns the last element in the map, for iteration for backwards iteration. Returns nil if the map is empty. O(1) time.
for elem := m.Last(); elem != nil; elem = elem.Prev() {
fmt.Println(elem.Key(), elem.Value())
}
func (*Map) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface. It marshals the map into JSON object.
data, err := json.Marshal(m)
func (*Map) Pop ¶ added in v0.4.0
Pop removes the last element from the map and returns it. Returns ok=false if the map is empty. O(1) time.
key, value, ok := m.Pop()
func (*Map) PopFront ¶ added in v0.4.0
PopFront removes the first element from the map and returns its key and value. Returns ok=false if the map is empty. O(n) time.
key, value, ok := m.PopFront()
func (*Map) Push ¶
Same as Set, but pushes the key to the end O(n) if key exists, as it uses Delete() O(1) if key is new.
m.Push(key, value)
func (*Map) PushFront ¶
PushFront is same as SetFront, but moves the element to the front of the map, as if it was just added. O(n) time.
m.PushFront(key, value)
func (*Map) Set ¶
Set sets the value for the key. If key is already in the map, it replaces the value, but keeps the original order of the element. O(1) time.
m.Set(key, value)
func (*Map) SetFront ¶
Same as Set, but pushes the key to the front if it is new. O(n) if key is new. O(1) if key exists.
m.SetFront(key, value)
func (*Map) SortKeys ¶ added in v0.4.0
SortKeys sorts keys in the map. O(n*log(n)) time.
m.SortKeys(func(a, b Key) bool {
return a < b
})
func (*Map) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler interface. It supports nested maps and arrays.
Note: it does not clear the map before unmarshaling. If you want to clear it, call Clear() before UnmarshalJSON().
err := m.UnmarshalJSON([]byte(`{"a":1,"b":2}`))