-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoc.go
104 lines (104 loc) · 2.47 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// jsonmap is an ordered map. Same as native Go map, but keeps order of insertion when iterating
// or serializing to JSON, and with additional methods to iterate from any point.
// Similar to native map, user has to take care of concurrency and nil map value.
//
// Create new map:
//
// m := jsonmap.New()
//
// Set values (remembering order of insertion):
//
// m.Set("someString", "value1")
// m.Set("someNumber", 42.1)
// m.Set("someArray", []string{"a", "b", "c"})
//
// Get value:
//
// value, ok := m.Get("someString")
// str, ok := value.(string) // value can be any type
// fmt.Println(str, ok)
//
// Get value as specific type:
//
// str, ok := jsonmap.GetAs[string](m, "someString")
// fmt.Println(str, ok) // str is string
//
// Iterate forwards:
//
// for elem := m.First(); elem != nil; elem = elem.Next() {
// fmt.Println(elem.Key(), elem.Value())
// }
//
// Iterate backwards:
//
// for elem := m.Last(); elem != nil; elem = elem.Prev() {
// fmt.Println(elem.Key(), elem.Value())
// }
//
// Iterate from any point:
//
// for elem := m.GetElement("someNumber"); elem != nil; elem = elem.Next() {
// fmt.Println(elem.Key(), elem.Value())
// }
//
// Delete element:
//
// m.Delete("someNumber")
//
// Push element to the end:
//
// m.Push("someNumber", 42.1)
//
// Pop element from the end:
//
// key, value, ok := m.Pop()
//
// Clear map:
//
// m.Clear()
//
// Serialize to JSON:
//
// data, err := json.Marshal(m)
//
// Deserialize from JSON:
//
// err = json.Unmarshal(data, &m)
//
// or use jsonmap.Map as a field in a struct:
//
// type MyStruct struct {
// SomeMap *jsonmap.Map `json:"someMap"`
// }
//
// And serialize/deserialize the struct:
//
// data, err := json.Marshal(myStruct)
// err = json.Unmarshal(data, &myStruct)
//
// Time complexity of operations:
//
// | Operation | Time |
// |-----------|-------------|
// | Clear | O(1) |
// | Get | O(1) |
// | Set | O(1) |
// | Delete | O(1) |
// | Push | O(1) |
// | Pop | O(1) |
// | | |
// | First | O(1) |
// | Last | O(1) |
// | GetElement| O(1) |
// | el.Next | O(1) |
// | el.Prev | O(1) |
// | | |
// | SetFront | O(1) |
// | PushFront | O(1) |
// | PopFront | O(1) |
// | | |
// | KeyIndex | O(N) |
// | Keys | O(N) |
// | Values | O(N) |
// | SortKeys | O(N*log(N)) |
package jsonmap