diff --git a/examples/treebidimap.go b/examples/treebidimap.go new file mode 100644 index 0000000..fc87c80 --- /dev/null +++ b/examples/treebidimap.go @@ -0,0 +1,28 @@ +// Copyright (c) 2015, Emir Pasic. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package examples + +import ( + "github.com/emirpasic/gods/maps/treebidimap" + "github.com/emirpasic/gods/utils" +) + +// TreeBidiMapExample to demonstrate basic usage of TreeBidiMap +func TreeBidiMapExample() { + m := treebidimap.NewWith(utils.IntComparator, utils.StringComparator) // empty + m.Put(1, "x") // 1->x + m.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->b + m.Clear() // empty + m.Empty() // true + m.Size() // 0 +} diff --git a/maps/treebidimap/treebidimap.go b/maps/treebidimap/treebidimap.go index 342e2cf..87eff9f 100644 --- a/maps/treebidimap/treebidimap.go +++ b/maps/treebidimap/treebidimap.go @@ -22,6 +22,7 @@ import ( "github.com/emirpasic/gods/maps" "github.com/emirpasic/gods/trees/redblacktree" "github.com/emirpasic/gods/utils" + "strings" ) func assertMapImplementation() { @@ -128,7 +129,10 @@ func (m *Map) Clear() { // String returns a string representation of container func (m *Map) String() string { - str := "TreeBidiMap\n" - str += fmt.Sprintf("%v", m.forwardMap) - return str + str := "TreeBidiMap\nmap[" + it := m.Iterator() + for it.Next() { + str += fmt.Sprintf("%v:%v ", it.Key(), it.Value()) + } + return strings.TrimRight(str, " ") + "]" } diff --git a/maps/treemap/treemap.go b/maps/treemap/treemap.go index e2b4233..a1e58ad 100644 --- a/maps/treemap/treemap.go +++ b/maps/treemap/treemap.go @@ -12,9 +12,11 @@ package treemap import ( + "fmt" "github.com/emirpasic/gods/maps" rbt "github.com/emirpasic/gods/trees/redblacktree" "github.com/emirpasic/gods/utils" + "strings" ) func assertMapImplementation() { @@ -105,7 +107,11 @@ func (m *Map) Max() (key interface{}, value interface{}) { // String returns a string representation of container func (m *Map) String() string { - str := "TreeMap\n" - str += m.tree.String() - return str + str := "TreeMap\nmap[" + it := m.Iterator() + for it.Next() { + str += fmt.Sprintf("%v:%v ", it.Key(), it.Value()) + } + return strings.TrimRight(str, " ") + "]" + }