Debian Codegraph @

maps/treebidimap/treebidimap.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 treebidimap implements a bidirectional map backed by two red-black tree.
6 //
7 // This structure guarantees that the map will be in both ascending key and value order.
8 //
9 // Other than key and value ordering, the goal with this structure is to avoid duplication of elements, which can be significant if contained elements are large.
10 //
11 // A bidirectional map, or hash bag, is an associative data structure in which the (key,value) pairs form a one-to-one correspondence.
12 // Thus the binary relation is functional in each direction: value can also act as a key to key.
13 // A pair (a,b) thus provides a unique coupling between 'a' and 'b' so that 'b' can be found when 'a' is used as a key and 'a' can be found when 'b' is used as a key.
14 //
15 // Structure is not thread safe.
16 //
17 // Reference: https://en.wikipedia.org/wiki/Bidirectional_map
18 package treebidimap
19
20 import (
21 "cmp"
22 "fmt"
23
24 "strings"
25
26 "github.com/emirpasic/gods/v2/maps"
27 "github.com/emirpasic/gods/v2/trees/redblacktree"
28 "github.com/emirpasic/gods/v2/utils"
29 )
30
31 // Assert Map implementation
32 var _ maps.BidiMap[string, int] = (*Map[string, int])(nil)
33
34 // Map holds the elements in two red-black trees.
35 type Map[K, V comparable] struct {
36 forwardMap redblacktree.Tree[K, V]
37 inverseMap redblacktree.Tree[V, K]
38 }
39
40 // New instantiates a bidirectional map.
41 func New[K, V cmp.Ordered]() *Map[K, V] {
42 return &Map[K, V]{
43 forwardMap: *redblacktree.New[K, V](),
44 inverseMap: *redblacktree.New[V, K](),
45 }
46 }
47
48 // NewWith instantiates a bidirectional map.
49 func NewWith[K, V comparable](keyComparator utils.Comparator[K], valueComparator utils.Comparator[V]) *Map[K, V] {
50 return &Map[K, V]{
51 forwardMap: *redblacktree.NewWith[K, V](keyComparator),
52 inverseMap: *redblacktree.NewWith[V, K](valueComparator),
53 }
54 }
55
56 // Put inserts element into the map.
57 func (m *Map[K, V]) Put(key K, value V) {
58 if v, ok := m.forwardMap.Get(key); ok {
59 m.inverseMap.Remove(v)
60 }
61 if k, ok := m.inverseMap.Get(value); ok {
62 m.forwardMap.Remove(k)
63 }
64 m.forwardMap.Put(key, value)
65 m.inverseMap.Put(value, key)
66 }
67
68 // Get searches the element in the map by key and returns its value or nil if key is not found in map.
69 // Second return parameter is true if key was found, otherwise false.
70 func (m *Map[K, V]) Get(key K) (value V, found bool) {
71 return m.forwardMap.Get(key)
72 }
73
74 // GetKey searches the element in the map by value and returns its key or nil if value is not found in map.
75 // Second return parameter is true if value was found, otherwise false.
76 func (m *Map[K, V]) GetKey(value V) (key K, found bool) {
77 return m.inverseMap.Get(value)
78 }
79
80 // Remove removes the element from the map by key.
81 func (m *Map[K, V]) Remove(key K) {
82 if v, found := m.forwardMap.Get(key); found {
83 m.forwardMap.Remove(key)
84 m.inverseMap.Remove(v)
85 }
86 }
87
88 // Empty returns true if map does not contain any elements
89 func (m *Map[K, V]) Empty() bool {
90 return m.Size() == 0
91 }
92
93 // Size returns number of elements in the map.
94 func (m *Map[K, V]) Size() int {
95 return m.forwardMap.Size()
96 }
97
98 // Keys returns all keys (ordered).
99 func (m *Map[K, V]) Keys() []K {
100 return m.forwardMap.Keys()
101 }
102
103 // Values returns all values (ordered).
104 func (m *Map[K, V]) Values() []V {
105 return m.inverseMap.Keys()
106 }
107
108 // Clear removes all elements from the map.
109 func (m *Map[K, V]) Clear() {
110 m.forwardMap.Clear()
111 m.inverseMap.Clear()
112 }
113
114 // String returns a string representation of container
115 func (m *Map[K, V]) String() string {
116 str := "TreeBidiMap\nmap["
117 it := m.Iterator()
118 for it.Next() {
119 str += fmt.Sprintf("%v:%v ", it.Key(), it.Value())
120 }
121 return strings.TrimRight(str, " ") + "]"
122 }