|
|
@ -14,7 +14,8 @@ package treemap
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"github.com/emirpasic/gods/maps"
|
|
|
|
"github.com/emirpasic/gods/maps"
|
|
|
|
rbt "github.com/emirpasic/gods/trees/redblacktree"
|
|
|
|
rbt "github.com/spewspews/gods/trees/redblacktree"
|
|
|
|
|
|
|
|
"github.com/spewspews/gods/trees"
|
|
|
|
"github.com/emirpasic/gods/utils"
|
|
|
|
"github.com/emirpasic/gods/utils"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -25,7 +26,7 @@ func assertMapImplementation() {
|
|
|
|
|
|
|
|
|
|
|
|
// Map holds the elements in a red-black tree
|
|
|
|
// Map holds the elements in a red-black tree
|
|
|
|
type Map struct {
|
|
|
|
type Map struct {
|
|
|
|
tree *rbt.Tree
|
|
|
|
tree trees.Tree
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewWith instantiates a tree map with the custom comparator.
|
|
|
|
// NewWith instantiates a tree map with the custom comparator.
|
|
|
@ -43,6 +44,11 @@ func NewWithStringComparator() *Map {
|
|
|
|
return &Map{tree: rbt.NewWithStringComparator()}
|
|
|
|
return &Map{tree: rbt.NewWithStringComparator()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// NewWithTree instantiates a new empty map with given tree
|
|
|
|
|
|
|
|
func NewWithTree(tree trees.Tree) *Map {
|
|
|
|
|
|
|
|
return &Map{tree: tree}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Put inserts key-value pair into the map.
|
|
|
|
// Put inserts key-value pair into the map.
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
|
|
|
func (m *Map) Put(key interface{}, value interface{}) {
|
|
|
|
func (m *Map) Put(key interface{}, value interface{}) {
|
|
|
@ -90,19 +96,13 @@ func (m *Map) Clear() {
|
|
|
|
// Min returns the minimum key and its value from the tree map.
|
|
|
|
// Min returns the minimum key and its value from the tree map.
|
|
|
|
// Returns nil, nil if map is empty.
|
|
|
|
// Returns nil, nil if map is empty.
|
|
|
|
func (m *Map) Min() (key interface{}, value interface{}) {
|
|
|
|
func (m *Map) Min() (key interface{}, value interface{}) {
|
|
|
|
if node := m.tree.Left(); node != nil {
|
|
|
|
return m.tree.Min()
|
|
|
|
return node.Key, node.Value
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Max returns the maximum key and its value from the tree map.
|
|
|
|
// Max returns the maximum key and its value from the tree map.
|
|
|
|
// Returns nil, nil if map is empty.
|
|
|
|
// Returns nil, nil if map is empty.
|
|
|
|
func (m *Map) Max() (key interface{}, value interface{}) {
|
|
|
|
func (m *Map) Max() (key interface{}, value interface{}) {
|
|
|
|
if node := m.tree.Right(); node != nil {
|
|
|
|
return m.tree.Max()
|
|
|
|
return node.Key, node.Value
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// String returns a string representation of container
|
|
|
|
// String returns a string representation of container
|
|
|
|