From 3b3edfc5394cbae2e579161cb3f479b0390410a4 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Wed, 22 Jun 2016 03:09:48 +0200 Subject: [PATCH] - naming conventions (calling interfaces by what they are) --- containers/containers.go | 4 +-- {enumerable => containers}/enumerable.go | 10 +++---- containers/iterator.go | 35 ++++++++++++++++++++++ lists/arraylist/arraylist.go | 32 ++++++++++++++++---- lists/doublylinkedlist/doublylinkedlist.go | 2 +- lists/lists.go | 4 +-- lists/singlylinkedlist/singlylinkedlist.go | 2 +- maps/hashmap/hashmap.go | 2 +- maps/maps.go | 4 +-- maps/treemap/treemap.go | 2 +- sets/hashset/hashset.go | 2 +- sets/sets.go | 4 +-- sets/treeset/treeset.go | 2 +- stacks/arraystack/arraystack.go | 2 +- stacks/linkedliststack/linkedliststack.go | 2 +- stacks/stacks.go | 4 +-- trees/binaryheap/binaryheap.go | 2 +- trees/redblacktree/redblacktree.go | 2 +- trees/trees.go | 4 +-- 19 files changed, 87 insertions(+), 34 deletions(-) rename {enumerable => containers}/enumerable.go (90%) create mode 100644 containers/iterator.go diff --git a/containers/containers.go b/containers/containers.go index 48e9b15..2da6c26 100644 --- a/containers/containers.go +++ b/containers/containers.go @@ -30,7 +30,7 @@ package containers import "github.com/emirpasic/gods/utils" -type Interface interface { +type Container interface { Empty() bool Size() int Clear() @@ -40,7 +40,7 @@ type Interface interface { // Returns sorted container's elements with respect to the passed comparator. // Does not effect the ordering of elements within the container. // Uses timsort. -func GetSortedValues(container Interface, comparator utils.Comparator) []interface{} { +func GetSortedValues(container Container, comparator utils.Comparator) []interface{} { values := container.Values() if len(values) < 2 { return values diff --git a/enumerable/enumerable.go b/containers/enumerable.go similarity index 90% rename from enumerable/enumerable.go rename to containers/enumerable.go index 9e76e43..bb736ab 100644 --- a/enumerable/enumerable.go +++ b/containers/enumerable.go @@ -27,20 +27,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Enumerable functions for ordered containers. // Ruby's enumerable inspired package. -package enumerable +package containers -import "github.com/emirpasic/gods/containers" - -type Interface interface { +type Enumerable interface { // Calls the given function once for each element, passing that element's index(key) and value. Each(func(index interface{}, value interface{})) // Invokes the given function once for each element and returns a // container containing the values returned by the given function. - Map(func(index interface{}, value interface{}) interface{}) containers.Interface + Map(func(index interface{}, value interface{}) interface{}) Container // Returns a new container containing all elements for which the given function returns a true value. - Select(func(index interface{}, value interface{}) bool) containers.Interface + Select(func(index interface{}, value interface{}) bool) Container // Passes each element of the collection to the given function and // returns true if the function ever returns true for any element. diff --git a/containers/iterator.go b/containers/iterator.go new file mode 100644 index 0000000..513a04f --- /dev/null +++ b/containers/iterator.go @@ -0,0 +1,35 @@ +/* +Copyright (c) 2015, Emir Pasic +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// Stateful iterator pattern for ordered containers. + +package containers + +type Iterator interface { + Next() bool + Value() interface{} + Index() interface{} +} diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index 60d25f9..ca01d41 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -33,15 +33,15 @@ package arraylist import ( "fmt" "github.com/emirpasic/gods/containers" - "github.com/emirpasic/gods/enumerable" "github.com/emirpasic/gods/lists" "github.com/emirpasic/gods/utils" "strings" ) func assertInterfaceImplementation() { - var _ lists.Interface = (*List)(nil) - var _ enumerable.Interface = (*List)(nil) + var _ lists.List = (*List)(nil) + var _ containers.Enumerable = (*List)(nil) + var _ containers.Iterator = (*Iterator)(nil) } type List struct { @@ -49,6 +49,27 @@ type List struct { size int } +type Iterator struct { + list *List + current int +} + +func (list *List) Iterator() Iterator { + return Iterator{list: list, current: 0} +} + +func (iterator *Iterator) Next() bool { + return false +} + +func (iterator *Iterator) Value() interface{} { + return nil +} + +func (iterator *Iterator) Index() interface{} { + return nil +} + const ( GROWTH_FACTOR = float32(2.0) // growth by 100% SHRINK_FACTOR = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink) @@ -184,7 +205,7 @@ func (list *List) Each(f func(index interface{}, value interface{})) { } } -func (list *List) Map(f func(index interface{}, value interface{}) interface{}) containers.Interface { +func (list *List) Map(f func(index interface{}, value interface{}) interface{}) containers.Container { newList := &List{} for i := 0; i < list.size; i++ { newList.Add(f(i, list.elements[i])) @@ -192,7 +213,7 @@ func (list *List) Map(f func(index interface{}, value interface{}) interface{}) return newList } -func (list *List) Select(f func(index interface{}, value interface{}) bool) containers.Interface { +func (list *List) Select(f func(index interface{}, value interface{}) bool) containers.Container { newList := &List{} for i := 0; i < list.size; i++ { if f(i, list.elements[i]) { @@ -270,5 +291,4 @@ func (list *List) shrink() { if list.size <= int(float32(currentCapacity)*SHRINK_FACTOR) { list.resize(list.size) } - } diff --git a/lists/doublylinkedlist/doublylinkedlist.go b/lists/doublylinkedlist/doublylinkedlist.go index 2a97756..281bda1 100644 --- a/lists/doublylinkedlist/doublylinkedlist.go +++ b/lists/doublylinkedlist/doublylinkedlist.go @@ -38,7 +38,7 @@ import ( ) func assertInterfaceImplementation() { - var _ lists.Interface = (*List)(nil) + var _ lists.List = (*List)(nil) } type List struct { diff --git a/lists/lists.go b/lists/lists.go index 5a83a4c..8bc0575 100644 --- a/lists/lists.go +++ b/lists/lists.go @@ -23,7 +23,7 @@ import ( "github.com/emirpasic/gods/utils" ) -type Interface interface { +type List interface { Get(index int) (interface{}, bool) Remove(index int) Add(values ...interface{}) @@ -32,7 +32,7 @@ type Interface interface { Swap(index1, index2 int) Insert(index int, values ...interface{}) - containers.Interface + containers.Container // Empty() bool // Size() int // Clear() diff --git a/lists/singlylinkedlist/singlylinkedlist.go b/lists/singlylinkedlist/singlylinkedlist.go index f993c39..47ab9b2 100644 --- a/lists/singlylinkedlist/singlylinkedlist.go +++ b/lists/singlylinkedlist/singlylinkedlist.go @@ -38,7 +38,7 @@ import ( ) func assertInterfaceImplementation() { - var _ lists.Interface = (*List)(nil) + var _ lists.List = (*List)(nil) } type List struct { diff --git a/maps/hashmap/hashmap.go b/maps/hashmap/hashmap.go index 247cc01..3033f6b 100644 --- a/maps/hashmap/hashmap.go +++ b/maps/hashmap/hashmap.go @@ -37,7 +37,7 @@ import ( ) func assertInterfaceImplementation() { - var _ maps.Interface = (*Map)(nil) + var _ maps.Map = (*Map)(nil) } type Map struct { diff --git a/maps/maps.go b/maps/maps.go index eddcc20..22fcf73 100644 --- a/maps/maps.go +++ b/maps/maps.go @@ -28,13 +28,13 @@ package maps import "github.com/emirpasic/gods/containers" -type Interface interface { +type Map interface { Put(key interface{}, value interface{}) Get(key interface{}) (value interface{}, found bool) Remove(key interface{}) Keys() []interface{} - containers.Interface + containers.Container // Empty() bool // Size() int // Clear() diff --git a/maps/treemap/treemap.go b/maps/treemap/treemap.go index 6d228cb..d0fc742 100644 --- a/maps/treemap/treemap.go +++ b/maps/treemap/treemap.go @@ -38,7 +38,7 @@ import ( ) func assertInterfaceImplementation() { - var _ maps.Interface = (*Map)(nil) + var _ maps.Map = (*Map)(nil) } type Map struct { diff --git a/sets/hashset/hashset.go b/sets/hashset/hashset.go index dfd1069..54d219b 100644 --- a/sets/hashset/hashset.go +++ b/sets/hashset/hashset.go @@ -37,7 +37,7 @@ import ( ) func assertInterfaceImplementation() { - var _ sets.Interface = (*Set)(nil) + var _ sets.Set = (*Set)(nil) } type Set struct { diff --git a/sets/sets.go b/sets/sets.go index 2a6e2dc..6ced903 100644 --- a/sets/sets.go +++ b/sets/sets.go @@ -20,12 +20,12 @@ package sets import "github.com/emirpasic/gods/containers" -type Interface interface { +type Set interface { Add(elements ...interface{}) Remove(elements ...interface{}) Contains(elements ...interface{}) bool - containers.Interface + containers.Container // Empty() bool // Size() int // Clear() diff --git a/sets/treeset/treeset.go b/sets/treeset/treeset.go index 9236eb1..da6d7d9 100644 --- a/sets/treeset/treeset.go +++ b/sets/treeset/treeset.go @@ -31,7 +31,7 @@ import ( ) func assertInterfaceImplementation() { - var _ sets.Interface = (*Set)(nil) + var _ sets.Set = (*Set)(nil) } type Set struct { diff --git a/stacks/arraystack/arraystack.go b/stacks/arraystack/arraystack.go index bb3a89d..407cdf0 100644 --- a/stacks/arraystack/arraystack.go +++ b/stacks/arraystack/arraystack.go @@ -38,7 +38,7 @@ import ( ) func assertInterfaceImplementation() { - var _ stacks.Interface = (*Stack)(nil) + var _ stacks.Stack = (*Stack)(nil) } type Stack struct { diff --git a/stacks/linkedliststack/linkedliststack.go b/stacks/linkedliststack/linkedliststack.go index bf96b4c..5cc08e6 100644 --- a/stacks/linkedliststack/linkedliststack.go +++ b/stacks/linkedliststack/linkedliststack.go @@ -39,7 +39,7 @@ import ( ) func assertInterfaceImplementation() { - var _ stacks.Interface = (*Stack)(nil) + var _ stacks.Stack = (*Stack)(nil) } type Stack struct { diff --git a/stacks/stacks.go b/stacks/stacks.go index fe9abbb..1f504be 100644 --- a/stacks/stacks.go +++ b/stacks/stacks.go @@ -28,12 +28,12 @@ package stacks import "github.com/emirpasic/gods/containers" -type Interface interface { +type Stack interface { Push(value interface{}) Pop() (value interface{}, ok bool) Peek() (value interface{}, ok bool) - containers.Interface + containers.Container // Empty() bool // Size() int // Clear() diff --git a/trees/binaryheap/binaryheap.go b/trees/binaryheap/binaryheap.go index 79f91b1..9320fc4 100644 --- a/trees/binaryheap/binaryheap.go +++ b/trees/binaryheap/binaryheap.go @@ -40,7 +40,7 @@ import ( ) func assertInterfaceImplementation() { - var _ trees.Interface = (*Heap)(nil) + var _ trees.Tree = (*Heap)(nil) } type Heap struct { diff --git a/trees/redblacktree/redblacktree.go b/trees/redblacktree/redblacktree.go index 2fe165b..3811fb2 100644 --- a/trees/redblacktree/redblacktree.go +++ b/trees/redblacktree/redblacktree.go @@ -39,7 +39,7 @@ import ( ) func assertInterfaceImplementation() { - var _ trees.Interface = (*Tree)(nil) + var _ trees.Tree = (*Tree)(nil) } type color bool diff --git a/trees/trees.go b/trees/trees.go index 3714bee..0a021dd 100644 --- a/trees/trees.go +++ b/trees/trees.go @@ -28,8 +28,8 @@ package trees import "github.com/emirpasic/gods/containers" -type Interface interface { - containers.Interface +type Tree interface { + containers.Container // Empty() bool // Size() int // Clear()