| 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 treemap implements a map backed by red-black tree. |
| 6 | // |
| 7 | // Elements are ordered by key in the map. |
| 8 | // |
| 9 | // Structure is not thread safe. |
| 10 | // |
| 11 | // Reference: http://en.wikipedia.org/wiki/Associative_array |
| 12 | package treemap |
| 13 | |
| 14 | import ( |
| 15 | "cmp" |
| 16 | "fmt" |
| 17 | "strings" |
| 18 | |
| 19 | "github.com/emirpasic/gods/v2/maps" |
| 20 | rbt "github.com/emirpasic/gods/v2/trees/redblacktree" |
| 21 | "github.com/emirpasic/gods/v2/utils" |
| 22 | ) |
| 23 | |
| 24 | // Assert Map implementation |
| 25 | var _ maps.Map[string, int] = (*Map[string, int])(nil) |
| 26 | |
| 27 | // Map holds the elements in a red-black tree |
| 28 | type Map[K comparable, V any] struct { |
| 29 | tree *rbt.Tree[K, V] |
| 30 | } |
| 31 | |
| 32 | // New instantiates a tree map with the built-in comparator for K |
| 33 | func New[K cmp.Ordered, V any]() *Map[K, V] { |
| 34 | return &Map[K, V]{tree: rbt.New[K, V]()} |
| 35 | } |
| 36 | |
| 37 | // NewWith instantiates a tree map with the custom comparator. |
| 38 | func NewWith[K comparable, V any](comparator utils.Comparator[K]) *Map[K, V] { |
| 39 | return &Map[K, V]{tree: rbt.NewWith[K, V](comparator)} |
| 40 | } |
| 41 | |
| 42 | // Put inserts key-value pair into the map. |
| 43 | // Key should adhere to the comparator's type assertion, otherwise method panics. |
| 44 | func (m *Map[K, V]) Put(key K, value V) { |
| 45 | m.tree.Put(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 | return m.tree.Get(key) |
| 53 | } |
| 54 | |
| 55 | // Remove removes the element from the map by key. |
| 56 | // Key should adhere to the comparator's type assertion, otherwise method panics. |
| 57 | func (m *Map[K, V]) Remove(key K) { |
| 58 | m.tree.Remove(key) |
| 59 | } |
| 60 | |
| 61 | // Empty returns true if map does not contain any elements |
| 62 | func (m *Map[K, V]) Empty() bool { |
| 63 | return m.tree.Empty() |
| 64 | } |
| 65 | |
| 66 | // Size returns number of elements in the map. |
| 67 | func (m *Map[K, V]) Size() int { |
| 68 | return m.tree.Size() |
| 69 | } |
| 70 | |
| 71 | // Keys returns all keys in-order |
| 72 | func (m *Map[K, V]) Keys() []K { |
| 73 | return m.tree.Keys() |
| 74 | } |
| 75 | |
| 76 | // Values returns all values in-order based on the key. |
| 77 | func (m *Map[K, V]) Values() []V { |
| 78 | return m.tree.Values() |
| 79 | } |
| 80 | |
| 81 | // Clear removes all elements from the map. |
| 82 | func (m *Map[K, V]) Clear() { |
| 83 | m.tree.Clear() |
| 84 | } |
| 85 | |
| 86 | // Min returns the minimum key and its value from the tree map. |
| 87 | // Returns 0-value, 0-value, false if map is empty. |
| 88 | func (m *Map[K, V]) Min() (key K, value V, ok bool) { |
| 89 | if node := m.tree.Left(); node != nil { |
| 90 | return node.Key, node.Value, true |
| 91 | } |
| 92 | return key, value, false |
| 93 | } |
| 94 | |
| 95 | // Max returns the maximum key and its value from the tree map. |
| 96 | // Returns 0-value, 0-value, false if map is empty. |
| 97 | func (m *Map[K, V]) Max() (key K, value V, ok bool) { |
| 98 | if node := m.tree.Right(); node != nil { |
| 99 | return node.Key, node.Value, true |
| 100 | } |
| 101 | return key, value, false |
| 102 | } |
| 103 | |
| 104 | // Floor finds the floor key-value pair for the input key. |
| 105 | // In case that no floor is found, then both returned values will be nil. |
| 106 | // It's generally enough to check the first value (key) for nil, which determines if floor was found. |
| 107 | // |
| 108 | // Floor key is defined as the largest key that is smaller than or equal to the given key. |
| 109 | // A floor key may not be found, either because the map is empty, or because |
| 110 | // all keys in the map are larger than the given key. |
| 111 | // |
| 112 | // Key should adhere to the comparator's type assertion, otherwise method panics. |
| 113 | func (m *Map[K, V]) Floor(key K) (foundKey K, foundValue V, ok bool) { |
| 114 | node, found := m.tree.Floor(key) |
| 115 | if found { |
| 116 | return node.Key, node.Value, true |
| 117 | } |
| 118 | return foundKey, foundValue, false |
| 119 | } |
| 120 | |
| 121 | // Ceiling finds the ceiling key-value pair for the input key. |
| 122 | // In case that no ceiling is found, then both returned values will be nil. |
| 123 | // It's generally enough to check the first value (key) for nil, which determines if ceiling was found. |
| 124 | // |
| 125 | // Ceiling key is defined as the smallest key that is larger than or equal to the given key. |
| 126 | // A ceiling key may not be found, either because the map is empty, or because |
| 127 | // all keys in the map are smaller than the given key. |
| 128 | // |
| 129 | // Key should adhere to the comparator's type assertion, otherwise method panics. |
| 130 | func (m *Map[K, V]) Ceiling(key K) (foundKey K, foundValue V, ok bool) { |
| 131 | node, found := m.tree.Ceiling(key) |
| 132 | if found { |
| 133 | return node.Key, node.Value, true |
| 134 | } |
| 135 | return foundKey, foundValue, false |
| 136 | } |
| 137 | |
| 138 | // String returns a string representation of container |
| 139 | func (m *Map[K, V]) String() string { |
| 140 | str := "TreeMap\nmap[" |
| 141 | it := m.Iterator() |
| 142 | for it.Next() { |
| 143 | str += fmt.Sprintf("%v:%v ", it.Key(), it.Value()) |
| 144 | } |
| 145 | return strings.TrimRight(str, " ") + "]" |
| 146 | |
| 147 | } |