2016-07-02 10:16:38 +00:00
// 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.
2016-07-03 01:18:28 +00:00
// Package treebidimap implements a bidirectional map backed by two red-black tree.
//
2016-07-02 10:16:38 +00:00
// This structure guarantees that the map will be in both ascending key and value order.
2016-07-03 01:18:28 +00:00
//
// Other than key and value ordering, the goal with this structure is to avoid duplication of elements, which can be significant if contained elements are large.
2016-07-02 10:16:38 +00:00
//
// A bidirectional map, or hash bag, is an associative data structure in which the (key,value) pairs form a one-to-one correspondence.
// Thus the binary relation is functional in each direction: value can also act as a key to key.
// A pair (a,b) thus provides a unique coupling between 'a' and 'b' so that 'b' can be found when 'a' is used as a key and 'a' can be found when 'b' is used as a key.
//
// Structure is not thread safe.
//
// Reference: https://en.wikipedia.org/wiki/Bidirectional_map
package treebidimap
import (
"fmt"
"github.com/emirpasic/gods/maps"
2016-07-03 01:18:28 +00:00
"github.com/emirpasic/gods/trees/redblacktree"
"github.com/emirpasic/gods/utils"
2016-07-02 10:16:38 +00:00
)
func assertMapImplementation ( ) {
var _ maps . BidiMap = ( * Map ) ( nil )
}
2016-07-03 01:18:28 +00:00
// Map holds the elements in two red-black trees.
2016-07-02 10:16:38 +00:00
type Map struct {
2016-07-03 01:47:35 +00:00
forwardMap redblacktree . Tree
inverseMap redblacktree . Tree
keyComparator utils . Comparator
valueComparator utils . Comparator
2016-07-03 01:18:28 +00:00
}
type data struct {
key interface { }
value interface { }
}
// NewWith instantiates a bidirectional map.
func NewWith ( keyComparator utils . Comparator , valueComparator utils . Comparator ) * Map {
return & Map {
2016-07-03 01:47:35 +00:00
forwardMap : * redblacktree . NewWith ( keyComparator ) ,
inverseMap : * redblacktree . NewWith ( valueComparator ) ,
keyComparator : keyComparator ,
valueComparator : valueComparator ,
2016-07-03 01:18:28 +00:00
}
}
// NewWithIntComparators instantiates a bidirectional map with the IntComparator for key and value, i.e. keys and values are of type int.
func NewWithIntComparators ( ) * Map {
return NewWith ( utils . IntComparator , utils . IntComparator )
2016-07-02 10:16:38 +00:00
}
2016-07-03 01:18:28 +00:00
// NewWithStringComparators instantiates a bidirectional map with the StringComparator for key and value, i.e. keys and values are of type string.
func NewWithStringComparators ( ) * Map {
return NewWith ( utils . StringComparator , utils . StringComparator )
2016-07-02 10:16:38 +00:00
}
// Put inserts element into the map.
func ( m * Map ) Put ( key interface { } , value interface { } ) {
2016-07-03 01:18:28 +00:00
if d , ok := m . forwardMap . Get ( key ) ; ok {
m . inverseMap . Remove ( d . ( * data ) . value )
2016-07-02 10:16:38 +00:00
}
2016-07-03 01:18:28 +00:00
if d , ok := m . inverseMap . Get ( value ) ; ok {
m . forwardMap . Remove ( d . ( * data ) . key )
2016-07-02 10:16:38 +00:00
}
2016-07-03 01:18:28 +00:00
d := & data { key : key , value : value }
m . forwardMap . Put ( key , d )
m . inverseMap . Put ( value , d )
2016-07-02 10:16:38 +00:00
}
// Get searches the element in the map by key and returns its value or nil if key is not found in map.
// Second return parameter is true if key was found, otherwise false.
func ( m * Map ) Get ( key interface { } ) ( value interface { } , found bool ) {
2016-07-03 01:18:28 +00:00
if d , ok := m . forwardMap . Get ( key ) ; ok {
return d . ( * data ) . value , true
}
return nil , false
2016-07-02 10:16:38 +00:00
}
// GetKey searches the element in the map by value and returns its key or nil if value is not found in map.
// Second return parameter is true if value was found, otherwise false.
func ( m * Map ) GetKey ( value interface { } ) ( key interface { } , found bool ) {
2016-07-03 01:18:28 +00:00
if d , ok := m . inverseMap . Get ( value ) ; ok {
return d . ( * data ) . key , true
}
return nil , false
2016-07-02 10:16:38 +00:00
}
// Remove removes the element from the map by key.
func ( m * Map ) Remove ( key interface { } ) {
2016-07-03 01:18:28 +00:00
if d , found := m . forwardMap . Get ( key ) ; found {
2016-07-02 10:16:38 +00:00
m . forwardMap . Remove ( key )
2016-07-03 01:18:28 +00:00
m . inverseMap . Remove ( d . ( * data ) . value )
2016-07-02 10:16:38 +00:00
}
}
// Empty returns true if map does not contain any elements
func ( m * Map ) Empty ( ) bool {
return m . Size ( ) == 0
}
// Size returns number of elements in the map.
func ( m * Map ) Size ( ) int {
return m . forwardMap . Size ( )
}
2016-07-03 01:18:28 +00:00
// Keys returns all keys (ordered).
2016-07-02 10:16:38 +00:00
func ( m * Map ) Keys ( ) [ ] interface { } {
return m . forwardMap . Keys ( )
}
2016-07-03 01:18:28 +00:00
// Values returns all values (ordered).
2016-07-02 10:16:38 +00:00
func ( m * Map ) Values ( ) [ ] interface { } {
return m . inverseMap . Keys ( )
}
// Clear removes all elements from the map.
func ( m * Map ) Clear ( ) {
m . forwardMap . Clear ( )
m . inverseMap . Clear ( )
}
// String returns a string representation of container
func ( m * Map ) String ( ) string {
2016-07-03 01:18:28 +00:00
str := "TreeBidiMap\n"
2016-07-02 10:16:38 +00:00
str += fmt . Sprintf ( "%v" , m . forwardMap )
return str
}