-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from emirpasic/development
LinkedHashMap
- Loading branch information
Showing
24 changed files
with
1,199 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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 main | ||
|
||
import "github.com/emirpasic/gods/maps/linkedhashmap" | ||
|
||
// LinkedHashMapExample to demonstrate basic usage of LinkedHashMapExample | ||
func main() { | ||
m := linkedhashmap.New() // empty (keys are of type int) | ||
m.Put(2, "b") // 2->b | ||
m.Put(1, "x") // 2->b, 1->x (insertion-order) | ||
m.Put(1, "a") // 2->b, 1->a (insertion-order) | ||
_, _ = m.Get(2) // b, true | ||
_, _ = m.Get(3) // nil, false | ||
_ = m.Values() // []interface {}{"b", "a"} (insertion-order) | ||
_ = m.Keys() // []interface {}{2, 1} (insertion-order) | ||
m.Remove(1) // 2->b | ||
m.Clear() // empty | ||
m.Empty() // true | ||
m.Size() // 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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 linkedhashmap | ||
|
||
import "github.com/emirpasic/gods/containers" | ||
|
||
func assertEnumerableImplementation() { | ||
var _ containers.EnumerableWithKey = (*Map)(nil) | ||
} | ||
|
||
// Each calls the given function once for each element, passing that element's key and value. | ||
func (m *Map) Each(f func(key interface{}, value interface{})) { | ||
iterator := m.Iterator() | ||
for iterator.Next() { | ||
f(iterator.Key(), iterator.Value()) | ||
} | ||
} | ||
|
||
// Map invokes the given function once for each element and returns a container | ||
// containing the values returned by the given function as key/value pairs. | ||
func (m *Map) Map(f func(key1 interface{}, value1 interface{}) (interface{}, interface{})) *Map { | ||
newMap := New() | ||
iterator := m.Iterator() | ||
for iterator.Next() { | ||
key2, value2 := f(iterator.Key(), iterator.Value()) | ||
newMap.Put(key2, value2) | ||
} | ||
return newMap | ||
} | ||
|
||
// Select returns a new container containing all elements for which the given function returns a true value. | ||
func (m *Map) Select(f func(key interface{}, value interface{}) bool) *Map { | ||
newMap := New() | ||
iterator := m.Iterator() | ||
for iterator.Next() { | ||
if f(iterator.Key(), iterator.Value()) { | ||
newMap.Put(iterator.Key(), iterator.Value()) | ||
} | ||
} | ||
return newMap | ||
} | ||
|
||
// Any passes each element of the container to the given function and | ||
// returns true if the function ever returns true for any element. | ||
func (m *Map) Any(f func(key interface{}, value interface{}) bool) bool { | ||
iterator := m.Iterator() | ||
for iterator.Next() { | ||
if f(iterator.Key(), iterator.Value()) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// All passes each element of the container to the given function and | ||
// returns true if the function returns true for all elements. | ||
func (m *Map) All(f func(key interface{}, value interface{}) bool) bool { | ||
iterator := m.Iterator() | ||
for iterator.Next() { | ||
if !f(iterator.Key(), iterator.Value()) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// Find passes each element of the container to the given function and returns | ||
// the first (key,value) for which the function is true or nil,nil otherwise if no element | ||
// matches the criteria. | ||
func (m *Map) Find(f func(key interface{}, value interface{}) bool) (interface{}, interface{}) { | ||
iterator := m.Iterator() | ||
for iterator.Next() { | ||
if f(iterator.Key(), iterator.Value()) { | ||
return iterator.Key(), iterator.Value() | ||
} | ||
} | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// 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 linkedhashmap | ||
|
||
import ( | ||
"github.com/emirpasic/gods/containers" | ||
"github.com/emirpasic/gods/lists/doublylinkedlist" | ||
) | ||
|
||
func assertIteratorImplementation() { | ||
var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) | ||
} | ||
|
||
// Iterator holding the iterator's state | ||
type Iterator struct { | ||
iterator doublylinkedlist.Iterator | ||
table map[interface{}]interface{} | ||
} | ||
|
||
// Iterator returns a stateful iterator whose elements are key/value pairs. | ||
func (m *Map) Iterator() Iterator { | ||
return Iterator{ | ||
iterator: m.ordering.Iterator(), | ||
table: m.table} | ||
} | ||
|
||
// Next moves the iterator to the next element and returns true if there was a next element in the container. | ||
// If Next() returns true, then next element's key and value can be retrieved by Key() and Value(). | ||
// If Next() was called for the first time, then it will point the iterator to the first element if it exists. | ||
// Modifies the state of the iterator. | ||
func (iterator *Iterator) Next() bool { | ||
return iterator.iterator.Next() | ||
} | ||
|
||
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container. | ||
// If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value(). | ||
// Modifies the state of the iterator. | ||
func (iterator *Iterator) Prev() bool { | ||
return iterator.iterator.Prev() | ||
} | ||
|
||
// Value returns the current element's value. | ||
// Does not modify the state of the iterator. | ||
func (iterator *Iterator) Value() interface{} { | ||
key := iterator.iterator.Value() | ||
return iterator.table[key] | ||
} | ||
|
||
// Key returns the current element's key. | ||
// Does not modify the state of the iterator. | ||
func (iterator *Iterator) Key() interface{} { | ||
return iterator.iterator.Value() | ||
} | ||
|
||
// Begin resets the iterator to its initial state (one-before-first) | ||
// Call Next() to fetch the first element if any. | ||
func (iterator *Iterator) Begin() { | ||
iterator.iterator.Begin() | ||
} | ||
|
||
// End moves the iterator past the last element (one-past-the-end). | ||
// Call Prev() to fetch the last element if any. | ||
func (iterator *Iterator) End() { | ||
iterator.iterator.End() | ||
} | ||
|
||
// First moves the iterator to the first element and returns true if there was a first element in the container. | ||
// If First() returns true, then first element's key and value can be retrieved by Key() and Value(). | ||
// Modifies the state of the iterator | ||
func (iterator *Iterator) First() bool { | ||
return iterator.iterator.First() | ||
} | ||
|
||
// Last moves the iterator to the last element and returns true if there was a last element in the container. | ||
// If Last() returns true, then last element's key and value can be retrieved by Key() and Value(). | ||
// Modifies the state of the iterator. | ||
func (iterator *Iterator) Last() bool { | ||
return iterator.iterator.Last() | ||
} |
Oops, something went wrong.