Skip to content

Commit

Permalink
Merge pull request #23 from emirpasic/bidi_map
Browse files Browse the repository at this point in the history
- bidimap implemention as dual hashmap bidirectional map
  • Loading branch information
emirpasic authored Jul 1, 2016
2 parents e86802a + 52d942a commit 16fd6c0
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 3 deletions.
49 changes: 47 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Implementation of various data structures and algorithms in Go.
- [Maps](#maps)
- [HashMap](#hashmap)
- [TreeMap](#treemap)
- [HashBidiMap](#hashbidimap)
- [Trees](#trees)
- [RedBlackTree](#redblacktree)
- [BinaryHeap](#binaryheap)
Expand Down Expand Up @@ -53,7 +54,7 @@ type Container interface {

Containers are either ordered or unordered. All ordered containers provide [stateful iterators](#iterator) and some of them allow [enumerable functions](#enumerable).

| Container | Ordered | [Iterator](#iterator) | [Enumerable](#enumerable) | Ordered by |
| Container | Ordered | [Iterator](#iterator) | [Enumerable](#enumerable) | Referenced by |
| :--- | :---: | :---: | :---: | :---: |
| [ArrayList](#arraylist) | yes | yes* | yes | index |
| [SinglyLinkedList](#singlylinkedlist) | yes | yes | yes | index |
Expand All @@ -64,9 +65,10 @@ Containers are either ordered or unordered. All ordered containers provide [stat
| [ArrayStack](#arraystack) | yes | yes* | no | index |
| [HashMap](#hashmap) | no | no | no | key |
| [TreeMap](#treemap) | yes | yes* | yes | key |
| [HashBidiMap](#hashbidimap) | no | no | no | key* |
| [RedBlackTree](#redblacktree) | yes | yes* | no | key |
| [BinaryHeap](#binaryheap) | yes | yes* | no | index |
| | | <sub><sup>*reversible</sup></sub> | | |
| | | <sub><sup>*reversible</sup></sub> | | <sub><sup>*bidirectional</sup></sub> |

### Lists

Expand Down Expand Up @@ -372,6 +374,16 @@ type Map interface {
}
```

A BidiMap is an extension to the Map. A bidirectional map (BidiMap), also called a hash bag, is an associative data structure in which the key-value pairs form a one-to-one relation. This relation works in both directions by allow the value to also act as a key to key, e.g. a pair (a,b) thus provides a 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.

```go
type BidiMap interface {
GetKey(value interface{}) (key interface{}, found bool)

Map
}
```

#### HashMap

A [map](#maps) based on hash tables. Keys are unordered.
Expand Down Expand Up @@ -430,6 +442,35 @@ func main() {
}
```

#### HashBidiMap

A [map](#maps) based on two hashmaps. Keys are unordered.

Implements [BidiMap](#maps) interface.

```go
package main

import "github.com/emirpasic/gods/maps/hashbidimap"

func main() {
m := hashbidimap.New() // empty
m.Put(1, "x") // 1->x
m.Put(3, "b") // 1->x, 3->b (random order)
m.Put(1, "a") // 1->a, 3->b (random order)
m.Put(2, "b") // 1->a, 2->b (random order)
_, _ = m.GetKey("a") // 1, true
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"a", "b"} (random order)
_ = m.Keys() // []interface {}{1, 2} (random order)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}
```

### Trees

A tree is a widely used data data structure that simulates a hierarchical tree structure, with a root value and subtrees of children, represented as a set of linked nodes; thus no cyclic links.
Expand Down Expand Up @@ -1057,6 +1098,10 @@ Collections and data structures found in other languages: Java Collections, C++

- Used in production.

**No dependencies**:

- No external imports.

There is often a tug of war between speed and memory when crafting algorithms. We choose to optimize for speed in most cases within reasonable limits on memory consumption.

Thread safety is not a concern of this project, this should be handled at a higher level.
Expand Down
25 changes: 25 additions & 0 deletions examples/hashbidimap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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/hashbidimap"

// HashBidiMapExample to demonstrate basic usage of HashMap
func HashBidiMapExample() {
m := hashbidimap.New() // empty
m.Put(1, "x") // 1->x
m.Put(3, "b") // 1->x, 3->b (random order)
m.Put(1, "a") // 1->a, 3->b (random order)
m.Put(2, "b") // 1->a, 2->b (random order)
_, _ = m.GetKey("a") // 1, true
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"a", "b"} (random order)
_ = m.Keys() // []interface {}{1, 2} (random order)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}
102 changes: 102 additions & 0 deletions maps/hashbidimap/hashbidimap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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 hashbidimap implements a bidirectional map backed by two hashmaps.
//
// 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.
//
// Elements are unordered in the map.
//
// Structure is not thread safe.
//
// Reference: https://en.wikipedia.org/wiki/Bidirectional_map
package hashbidimap

import (
"fmt"
"github.com/emirpasic/gods/maps"
"github.com/emirpasic/gods/maps/hashmap"
)

func assertMapImplementation() {
var _ maps.BidiMap = (*Map)(nil)
}

// Map holds the elements in two hashmaps.
type Map struct {
forwardMap hashmap.Map
inverseMap hashmap.Map
}

// New instantiates a bidirectional map.
func New() *Map {
return &Map{*hashmap.New(), *hashmap.New()}
}

// Put inserts element into the map.
func (m *Map) Put(key interface{}, value interface{}) {
if valueByKey, ok := m.forwardMap.Get(key); ok {
m.inverseMap.Remove(valueByKey)
}
if keyByValue, ok := m.inverseMap.Get(value); ok {
m.forwardMap.Remove(keyByValue)
}
m.forwardMap.Put(key, value)
m.inverseMap.Put(value, key)
}

// 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) {
return m.forwardMap.Get(key)
}

// 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) {
return m.inverseMap.Get(value)
}

// Remove removes the element from the map by key.
func (m *Map) Remove(key interface{}) {
if value, found := m.forwardMap.Get(key); found {
m.forwardMap.Remove(key)
m.inverseMap.Remove(value)
}
}

// 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()
}

// Keys returns all keys (random order).
func (m *Map) Keys() []interface{} {
return m.forwardMap.Keys()
}

// Values returns all values (random order).
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 {
str := "HashMap\n"
str += fmt.Sprintf("%v", m.forwardMap)
return str
}
Loading

0 comments on commit 16fd6c0

Please sign in to comment.