实现了一个由两棵红黑树支持的双向映射。
这个结构保证了映射的键和值顺序都是升序的。 除了键和值的排序,这种结构的目标是避免元素的重复,如果所包含的元素很大,重复是很重要的。
type Iterator
func (iterator *Iterator) Begin() // 将迭代器重置为初始状态,然后调用Next获取第一个元素func (iterator *Iterator) End() // 将迭代器移过最后一个元素,然后调用Prev获取最后一个元素// 将迭代器移动到第一个元素,如果容器中有第一个元素则返回true。// 如果First()返回true,则可以通过index()和value()检索第一个元素的索引和值。修改迭代器的状态func (iterator *Iterator) First() bool// 将迭代器移动到最后元素,如果容器中有第一个元素则返回true。// 如果Last()返回true,则可以通过index()和value()检索第一个元素的索引和值。修改迭代器的状态func (iterator *Iterator) Last() boolfunc (iterator *Iterator) Next() boolfunc (iterator *Iterator) Prev() boolfunc (iterator *Iterator) Index() intfunc (iterator *Iterator) Value() interface{}
type Map
func NewWith(keyComparator utils.Comparator, valueComparator utils.Comparator) *Map// 实例化一个双向映射,使用IntComparator的键和值,即键和值的类型是int。func NewWithIntComparators() *Map// 实例化一个双向映射,使用StringComparator的键和值,即键和值的类型是stringfunc NewWithStringComparators() *Map// 将集合中的每个元素传递给给定的函数,如果函数对所有元素都返回true,则返回truefunc (m *Map) All(f func(key interface{}, value interface{}) bool) bool// 将集合中的每个元素传递给给定的函数,如果函数对任何元素返回true,则返回truefunc (m *Map) Any(f func(key interface{}, value interface{}) bool) boolfunc (m *Map) Clear()func (m *Map) Each(f func(key interface{}, value interface{}))func (m *Map) Empty() bool// 将容器中的每个元素传递给给定的函数,并返回函数为true或-1的第一个元素(索引,值)// 如果没有元素符合条件,则返回nilfunc (m *Map) Find(f func(key interface{}, value interface{}) bool) (interface{}, interface{})func (m *Map) FromJSON(data []byte) errorfunc (m *Map) Get(key interface{}) (value interface{}, found bool)func (m *Map) GetKey(value interface{}) (key interface{}, found bool)func (m *Map) Iterator() Iteratorfunc (m *Map) Keys() []interface{}// 为每个元素调用给定函数一次,并返回一个包含给定函数返回值的容器func (m *Map) Map(f func(key1 interface{}, value1 interface{}) (interface{}, interface{})) *Mapfunc (m *Map) Put(key interface{}, value interface{})func (m *Map) Remove(key interface{})// 返回一个新容器,其中包含给定函数返回真值的所有元素func (m *Map) Select(f func(key interface{}, value interface{}) bool) *Mapfunc (m *Map) Size() intfunc (m *Map) String() stringfunc (m *Map) ToJSON() ([]byte, error)func (m *Map) Values() []interface{}
例子
package mainimport ("github.com/emirpasic/gods/maps/treebidimap""github.com/emirpasic/gods/utils")// TreeBidiMapExample to demonstrate basic usage of TreeBidiMapfunc main() {m := treebidimap.NewWith(utils.IntComparator, utils.StringComparator)m.Put(1, "x") // 1->xm.Put(3, "b") // 1->x, 3->b (ordered)m.Put(1, "a") // 1->a, 3->b (ordered)m.Put(2, "b") // 1->a, 2->b (ordered)_, _ = m.GetKey("a") // 1, true_, _ = m.Get(2) // b, true_, _ = m.Get(3) // nil, false_ = m.Values() // []interface {}{"a", "b"} (ordered)_ = m.Keys() // []interface {}{1, 2} (ordered)m.Remove(1) // 2->bm.Clear() // emptym.Empty() // truem.Size() // 0}
