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