Debian Codegraph @

maps/linkedhashmap/linkedhashmap.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package linkedhashmap is a map that preserves insertion-order.
6 //
7 // It is backed by a hash table to store values and doubly-linked list to store ordering.
8 //
9 // Structure is not thread safe.
10 //
11 // Reference: http://en.wikipedia.org/wiki/Associative_array
12 package linkedhashmap
13
14 import (
15 "fmt"
16 "strings"
17
18 "github.com/emirpasic/gods/v2/lists/doublylinkedlist"
19 "github.com/emirpasic/gods/v2/maps"
20 )
21
22 // Assert Map implementation
23 var _ maps.Map[string, int] = (*Map[string, int])(nil)
24
25 // Map holds the elements in a regular hash table, and uses doubly-linked list to store key ordering.
26 type Map[K comparable, V any] struct {
27 table map[K]V
28 ordering *doublylinkedlist.List[K]
29 }
30
31 // New instantiates a linked-hash-map.
32 func New[K comparable, V any]() *Map[K, V] {
33 return &Map[K, V]{
34 table: make(map[K]V),
35 ordering: doublylinkedlist.New[K](),
36 }
37 }
38
39 // Put inserts key-value pair into the map.
40 // Key should adhere to the comparator's type assertion, otherwise method panics.
41 func (m *Map[K, V]) Put(key K, value V) {
42 if _, contains := m.table[key]; !contains {
43 m.ordering.Append(key)
44 }
45 m.table[key] = value
46 }
47
48 // Get searches the element in the map by key and returns its value or nil if key is not found in tree.
49 // Second return parameter is true if key was found, otherwise false.
50 // Key should adhere to the comparator's type assertion, otherwise method panics.
51 func (m *Map[K, V]) Get(key K) (value V, found bool) {
52 value, found = m.table[key]
53 return value, found
54 }
55
56 // Remove removes the element from the map by key.
57 // Key should adhere to the comparator's type assertion, otherwise method panics.
58 func (m *Map[K, V]) Remove(key K) {
59 if _, contains := m.table[key]; contains {
60 delete(m.table, key)
61 index := m.ordering.IndexOf(key)
62 m.ordering.Remove(index)
63 }
64 }
65
66 // Empty returns true if map does not contain any elements
67 func (m *Map[K, V]) Empty() bool {
68 return m.Size() == 0
69 }
70
71 // Size returns number of elements in the map.
72 func (m *Map[K, V]) Size() int {
73 return m.ordering.Size()
74 }
75
76 // Keys returns all keys in-order
77 func (m *Map[K, V]) Keys() []K {
78 return m.ordering.Values()
79 }
80
81 // Values returns all values in-order based on the key.
82 func (m *Map[K, V]) Values() []V {
83 values := make([]V, m.Size())
84 count := 0
85 it := m.Iterator()
86 for it.Next() {
87 values[count] = it.Value()
88 count++
89 }
90 return values
91 }
92
93 // Clear removes all elements from the map.
94 func (m *Map[K, V]) Clear() {
95 clear(m.table)
96 m.ordering.Clear()
97 }
98
99 // String returns a string representation of container
100 func (m *Map[K, V]) String() string {
101 str := "LinkedHashMap\nmap["
102 it := m.Iterator()
103 for it.Next() {
104 str += fmt.Sprintf("%v:%v ", it.Key(), it.Value())
105 }
106 return strings.TrimRight(str, " ") + "]"
107
108 }