jsonmap

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2023 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAs added in v0.4.0

func GetAs[T any](m *Map, key Key) (value T, ok bool)

Helper function to get the value as a specific type. Returns ok=false if the key is not in the map or the value is not of the requested type.

str, ok := jsonmap.GetAs[string](m, key)

Types

type Key

type Key = string

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 New

func New() *Map

New returns a new Map. O(1) time.

m := jsonmap.New()

func (*Map) Clear

func (m *Map) Clear()

Clear removes all elements from the map. O(1) time.

m.Clear()

func (*Map) Delete

func (m *Map) Delete(key Key)

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

func (m *Map) First() jsonmap.Element

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

func (m *Map) Get(key Key) (value Value, ok bool)

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

func (m *Map) GetElement(key Key) jsonmap.Element

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

func (m *Map) KeyIndex(key Key) int

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

func (m *Map) Keys() []Key

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

func (m *Map) Last() jsonmap.Element

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) Len

func (m *Map) Len() int

Len returns the number of elements in the map. O(1) time.

n := m.Len()

func (*Map) MarshalJSON

func (m *Map) MarshalJSON() ([]byte, error)

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

func (m *Map) Pop() (key Key, value Value, ok bool)

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

func (m *Map) PopFront() (key Key, value Value, ok bool)

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

func (m *Map) Push(key Key, value Value)

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

func (m *Map) PushFront(key Key, value Value)

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

func (m *Map) Set(key Key, value Value)

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

func (m *Map) SetFront(key Key, value Value)

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

func (m *Map) SortKeys(less func(a, b Key) bool)

SortKeys sorts keys in the map. O(n*log(n)) time.

m.SortKeys(func(a, b Key) bool {
	return a < b
})

func (*Map) String

func (m *Map) String() string

String returns a string representation of the map. O(n) time.

func (*Map) UnmarshalJSON

func (m *Map) UnmarshalJSON(data []byte) error

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}`))

func (*Map) Values

func (m *Map) Values() []Value

Values returns all values in the map. O(n) time and O(n) space. O(1) time and space.

values := m.Values()

type Value

type Value = any

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL