- naming conventions (calling interfaces by what they are)

pull/12/head
Emir Pasic 9 years ago
parent 98bde950bc
commit 3b3edfc539

@ -30,7 +30,7 @@ package containers
import "github.com/emirpasic/gods/utils" import "github.com/emirpasic/gods/utils"
type Interface interface { type Container interface {
Empty() bool Empty() bool
Size() int Size() int
Clear() Clear()
@ -40,7 +40,7 @@ type Interface interface {
// Returns sorted container's elements with respect to the passed comparator. // Returns sorted container's elements with respect to the passed comparator.
// Does not effect the ordering of elements within the container. // Does not effect the ordering of elements within the container.
// Uses timsort. // Uses timsort.
func GetSortedValues(container Interface, comparator utils.Comparator) []interface{} { func GetSortedValues(container Container, comparator utils.Comparator) []interface{} {
values := container.Values() values := container.Values()
if len(values) < 2 { if len(values) < 2 {
return values return values

@ -27,20 +27,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Enumerable functions for ordered containers. // Enumerable functions for ordered containers.
// Ruby's enumerable inspired package. // Ruby's enumerable inspired package.
package enumerable package containers
import "github.com/emirpasic/gods/containers" type Enumerable interface {
type Interface interface {
// Calls the given function once for each element, passing that element's index(key) and value. // Calls the given function once for each element, passing that element's index(key) and value.
Each(func(index interface{}, value interface{})) Each(func(index interface{}, value interface{}))
// Invokes the given function once for each element and returns a // Invokes the given function once for each element and returns a
// container containing the values returned by the given function. // 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. // 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 // Passes each element of the collection to the given function and
// returns true if the function ever returns true for any element. // returns true if the function ever returns true for any element.

@ -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{}
}

@ -33,15 +33,15 @@ package arraylist
import ( import (
"fmt" "fmt"
"github.com/emirpasic/gods/containers" "github.com/emirpasic/gods/containers"
"github.com/emirpasic/gods/enumerable"
"github.com/emirpasic/gods/lists" "github.com/emirpasic/gods/lists"
"github.com/emirpasic/gods/utils" "github.com/emirpasic/gods/utils"
"strings" "strings"
) )
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ lists.Interface = (*List)(nil) var _ lists.List = (*List)(nil)
var _ enumerable.Interface = (*List)(nil) var _ containers.Enumerable = (*List)(nil)
var _ containers.Iterator = (*Iterator)(nil)
} }
type List struct { type List struct {
@ -49,6 +49,27 @@ type List struct {
size int 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 ( const (
GROWTH_FACTOR = float32(2.0) // growth by 100% GROWTH_FACTOR = float32(2.0) // growth by 100%
SHRINK_FACTOR = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink) 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{} newList := &List{}
for i := 0; i < list.size; i++ { for i := 0; i < list.size; i++ {
newList.Add(f(i, list.elements[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 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{} newList := &List{}
for i := 0; i < list.size; i++ { for i := 0; i < list.size; i++ {
if f(i, list.elements[i]) { if f(i, list.elements[i]) {
@ -270,5 +291,4 @@ func (list *List) shrink() {
if list.size <= int(float32(currentCapacity)*SHRINK_FACTOR) { if list.size <= int(float32(currentCapacity)*SHRINK_FACTOR) {
list.resize(list.size) list.resize(list.size)
} }
} }

@ -38,7 +38,7 @@ import (
) )
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ lists.Interface = (*List)(nil) var _ lists.List = (*List)(nil)
} }
type List struct { type List struct {

@ -23,7 +23,7 @@ import (
"github.com/emirpasic/gods/utils" "github.com/emirpasic/gods/utils"
) )
type Interface interface { type List interface {
Get(index int) (interface{}, bool) Get(index int) (interface{}, bool)
Remove(index int) Remove(index int)
Add(values ...interface{}) Add(values ...interface{})
@ -32,7 +32,7 @@ type Interface interface {
Swap(index1, index2 int) Swap(index1, index2 int)
Insert(index int, values ...interface{}) Insert(index int, values ...interface{})
containers.Interface containers.Container
// Empty() bool // Empty() bool
// Size() int // Size() int
// Clear() // Clear()

@ -38,7 +38,7 @@ import (
) )
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ lists.Interface = (*List)(nil) var _ lists.List = (*List)(nil)
} }
type List struct { type List struct {

@ -37,7 +37,7 @@ import (
) )
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ maps.Interface = (*Map)(nil) var _ maps.Map = (*Map)(nil)
} }
type Map struct { type Map struct {

@ -28,13 +28,13 @@ package maps
import "github.com/emirpasic/gods/containers" import "github.com/emirpasic/gods/containers"
type Interface interface { type Map interface {
Put(key interface{}, value interface{}) Put(key interface{}, value interface{})
Get(key interface{}) (value interface{}, found bool) Get(key interface{}) (value interface{}, found bool)
Remove(key interface{}) Remove(key interface{})
Keys() []interface{} Keys() []interface{}
containers.Interface containers.Container
// Empty() bool // Empty() bool
// Size() int // Size() int
// Clear() // Clear()

@ -38,7 +38,7 @@ import (
) )
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ maps.Interface = (*Map)(nil) var _ maps.Map = (*Map)(nil)
} }
type Map struct { type Map struct {

@ -37,7 +37,7 @@ import (
) )
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ sets.Interface = (*Set)(nil) var _ sets.Set = (*Set)(nil)
} }
type Set struct { type Set struct {

@ -20,12 +20,12 @@ package sets
import "github.com/emirpasic/gods/containers" import "github.com/emirpasic/gods/containers"
type Interface interface { type Set interface {
Add(elements ...interface{}) Add(elements ...interface{})
Remove(elements ...interface{}) Remove(elements ...interface{})
Contains(elements ...interface{}) bool Contains(elements ...interface{}) bool
containers.Interface containers.Container
// Empty() bool // Empty() bool
// Size() int // Size() int
// Clear() // Clear()

@ -31,7 +31,7 @@ import (
) )
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ sets.Interface = (*Set)(nil) var _ sets.Set = (*Set)(nil)
} }
type Set struct { type Set struct {

@ -38,7 +38,7 @@ import (
) )
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ stacks.Interface = (*Stack)(nil) var _ stacks.Stack = (*Stack)(nil)
} }
type Stack struct { type Stack struct {

@ -39,7 +39,7 @@ import (
) )
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ stacks.Interface = (*Stack)(nil) var _ stacks.Stack = (*Stack)(nil)
} }
type Stack struct { type Stack struct {

@ -28,12 +28,12 @@ package stacks
import "github.com/emirpasic/gods/containers" import "github.com/emirpasic/gods/containers"
type Interface interface { type Stack interface {
Push(value interface{}) Push(value interface{})
Pop() (value interface{}, ok bool) Pop() (value interface{}, ok bool)
Peek() (value interface{}, ok bool) Peek() (value interface{}, ok bool)
containers.Interface containers.Container
// Empty() bool // Empty() bool
// Size() int // Size() int
// Clear() // Clear()

@ -40,7 +40,7 @@ import (
) )
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ trees.Interface = (*Heap)(nil) var _ trees.Tree = (*Heap)(nil)
} }
type Heap struct { type Heap struct {

@ -39,7 +39,7 @@ import (
) )
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ trees.Interface = (*Tree)(nil) var _ trees.Tree = (*Tree)(nil)
} }
type color bool type color bool

@ -28,8 +28,8 @@ package trees
import "github.com/emirpasic/gods/containers" import "github.com/emirpasic/gods/containers"
type Interface interface { type Tree interface {
containers.Interface containers.Container
// Empty() bool // Empty() bool
// Size() int // Size() int
// Clear() // Clear()

Loading…
Cancel
Save