Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions containers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ type Container interface {
Empty() bool
Size() int
Clear()
Values() []interface{}
Values() []any
String() string
}

// GetSortedValues returns sorted container's elements with respect to the passed comparator.
// Does not affect the ordering of elements within the container.
func GetSortedValues(container Container, comparator utils.Comparator) []interface{} {
func GetSortedValues(container Container, comparator utils.Comparator) []any {
values := container.Values()
if len(values) < 2 {
return values
Expand Down
10 changes: 5 additions & 5 deletions containers/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// For testing purposes
type ContainerTest struct {
values []interface{}
values []any
}

func (container ContainerTest) Empty() bool {
Expand All @@ -27,10 +27,10 @@ func (container ContainerTest) Size() int {
}

func (container ContainerTest) Clear() {
container.values = []interface{}{}
container.values = []any{}
}

func (container ContainerTest) Values() []interface{} {
func (container ContainerTest) Values() []any {
return container.values
}

Expand All @@ -47,7 +47,7 @@ func (container ContainerTest) String() string {
func TestGetSortedValuesInts(t *testing.T) {
container := ContainerTest{}
GetSortedValues(container, utils.IntComparator)
container.values = []interface{}{5, 1, 3, 2, 4}
container.values = []any{5, 1, 3, 2, 4}
values := GetSortedValues(container, utils.IntComparator)
for i := 1; i < container.Size(); i++ {
if values[i-1].(int) > values[i].(int) {
Expand All @@ -59,7 +59,7 @@ func TestGetSortedValuesInts(t *testing.T) {
func TestGetSortedValuesStrings(t *testing.T) {
container := ContainerTest{}
GetSortedValues(container, utils.StringComparator)
container.values = []interface{}{"g", "a", "d", "e", "f", "c", "b"}
container.values = []any{"g", "a", "d", "e", "f", "c", "b"}
values := GetSortedValues(container, utils.StringComparator)
for i := 1; i < container.Size(); i++ {
if values[i-1].(string) > values[i].(string) {
Expand Down
16 changes: 8 additions & 8 deletions containers/enumerable.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package containers
// EnumerableWithIndex provides functions for ordered containers whose values can be fetched by an index.
type EnumerableWithIndex interface {
// Each calls the given function once for each element, passing that element's index and value.
Each(func(index int, value interface{}))
Each(func(index int, value any))

// Map invokes the given function once for each element and returns a
// container containing the values returned by the given function.
Expand All @@ -18,22 +18,22 @@ type EnumerableWithIndex interface {

// Any passes each element of the container to the given function and
// returns true if the function ever returns true for any element.
Any(func(index int, value interface{}) bool) bool
Any(func(index int, value any) bool) bool

// All passes each element of the container to the given function and
// returns true if the function returns true for all elements.
All(func(index int, value interface{}) bool) bool
All(func(index int, value any) bool) bool

// Find passes each element of the container to the given function and returns
// the first (index,value) for which the function is true or -1,nil otherwise
// if no element matches the criteria.
Find(func(index int, value interface{}) bool) (int, interface{})
Find(func(index int, value any) bool) (int, any)
}

// EnumerableWithKey provides functions for ordered containers whose values whose elements are key/value pairs.
type EnumerableWithKey interface {
// Each calls the given function once for each element, passing that element's key and value.
Each(func(key interface{}, value interface{}))
Each(func(key any, value any))

// 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.
Expand All @@ -44,14 +44,14 @@ type EnumerableWithKey interface {

// Any passes each element of the container to the given function and
// returns true if the function ever returns true for any element.
Any(func(key interface{}, value interface{}) bool) bool
Any(func(key any, value any) bool) bool

// All passes each element of the container to the given function and
// returns true if the function returns true for all elements.
All(func(key interface{}, value interface{}) bool) bool
All(func(key any, value any) bool) bool

// 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.
Find(func(key interface{}, value interface{}) bool) (interface{}, interface{})
Find(func(key any, value any) bool) (any, any)
}
18 changes: 9 additions & 9 deletions containers/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type IteratorWithIndex interface {

// Value returns the current element's value.
// Does not modify the state of the iterator.
Value() interface{}
Value() any

// Index returns the current element's index.
// Does not modify the state of the iterator.
Expand All @@ -33,7 +33,7 @@ type IteratorWithIndex interface {
// passed function, and returns true if there was a next element in the container.
// If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
NextTo(func(index int, value interface{}) bool) bool
NextTo(func(index int, value any) bool) bool
}

// IteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.
Expand All @@ -46,11 +46,11 @@ type IteratorWithKey interface {

// Value returns the current element's value.
// Does not modify the state of the iterator.
Value() interface{}
Value() any

// Key returns the current element's key.
// Does not modify the state of the iterator.
Key() interface{}
Key() any

// Begin resets the iterator to its initial state (one-before-first)
// Call Next() to fetch the first element if any.
Expand All @@ -65,14 +65,14 @@ type IteratorWithKey interface {
// passed function, and returns true if there was a next element in the container.
// If NextTo() returns true, then next element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator.
NextTo(func(key interface{}, value interface{}) bool) bool
NextTo(func(key any, value any) bool) bool
}

// ReverseIteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.
//
// Essentially it is the same as IteratorWithIndex, but provides additional:
//
// Prev() function to enable traversal in reverse
// # Prev() function to enable traversal in reverse
//
// Last() function to move the iterator to the last element.
//
Expand All @@ -96,7 +96,7 @@ type ReverseIteratorWithIndex interface {
// passed function, and returns true if there was a next element in the container.
// If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
PrevTo(func(index int, value interface{}) bool) bool
PrevTo(func(index int, value any) bool) bool

IteratorWithIndex
}
Expand All @@ -105,7 +105,7 @@ type ReverseIteratorWithIndex interface {
//
// Essentially it is the same as IteratorWithKey, but provides additional:
//
// Prev() function to enable traversal in reverse
// # Prev() function to enable traversal in reverse
//
// Last() function to move the iterator to the last element.
type ReverseIteratorWithKey interface {
Expand All @@ -127,7 +127,7 @@ type ReverseIteratorWithKey interface {
// passed function, and returns true if there was a next element in the container.
// If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator.
PrevTo(func(key interface{}, value interface{}) bool) bool
PrevTo(func(key any, value any) bool) bool

IteratorWithKey
}
2 changes: 1 addition & 1 deletion examples/binaryheap/binaryheap.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
heap.Size() // 0

// Max-heap
inverseIntComparator := func(a, b interface{}) int {
inverseIntComparator := func(a, b any) int {
return -utils.IntComparator(a, b)
}
heap = binaryheap.NewWith(inverseIntComparator) // empty (min-heap)
Expand Down
2 changes: 1 addition & 1 deletion examples/customcomparator/customcomparator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type User struct {
}

// Comparator function (sort by IDs)
func byID(a, b interface{}) int {
func byID(a, b any) int {

// Type assertion, program will panic if this is not respected
c1 := a.(User)
Expand Down
16 changes: 8 additions & 8 deletions examples/enumerablewithindex/enumerablewithindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func printSet(txt string, set *treeset.Set) {
fmt.Print(txt, "[ ")
set.Each(func(index int, value interface{}) {
set.Each(func(index int, value any) {
fmt.Print(value, " ")
})
fmt.Println("]")
Expand All @@ -23,36 +23,36 @@ func main() {
set.Add(2, 3, 4, 2, 5, 6, 7, 8)
printSet("Initial", set) // [ 2 3 4 5 6 7 8 ]

even := set.Select(func(index int, value interface{}) bool {
even := set.Select(func(index int, value any) bool {
return value.(int)%2 == 0
})
printSet("Even numbers", even) // [ 2 4 6 8 ]

foundIndex, foundValue := set.Find(func(index int, value interface{}) bool {
foundIndex, foundValue := set.Find(func(index int, value any) bool {
return value.(int)%2 == 0 && value.(int)%3 == 0
})
if foundIndex != -1 {
fmt.Println("Number divisible by 2 and 3 found is", foundValue, "at index", foundIndex) // value: 6, index: 4
}

square := set.Map(func(index int, value interface{}) interface{} {
square := set.Map(func(index int, value any) any {
return value.(int) * value.(int)
})
printSet("Numbers squared", square) // [ 4 9 16 25 36 49 64 ]

bigger := set.Any(func(index int, value interface{}) bool {
bigger := set.Any(func(index int, value any) bool {
return value.(int) > 5
})
fmt.Println("Set contains a number bigger than 5 is ", bigger) // true

positive := set.All(func(index int, value interface{}) bool {
positive := set.All(func(index int, value any) bool {
return value.(int) > 0
})
fmt.Println("All numbers are positive is", positive) // true

evenNumbersSquared := set.Select(func(index int, value interface{}) bool {
evenNumbersSquared := set.Select(func(index int, value any) bool {
return value.(int)%2 == 0
}).Map(func(index int, value interface{}) interface{} {
}).Map(func(index int, value any) any {
return value.(int) * value.(int)
})
printSet("Chaining", evenNumbersSquared) // [ 4 16 36 64 ]
Expand Down
16 changes: 8 additions & 8 deletions examples/enumerablewithkey/enumerablewithkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func printMap(txt string, m *treemap.Map) {
fmt.Print(txt, " { ")
m.Each(func(key interface{}, value interface{}) {
m.Each(func(key any, value any) {
fmt.Print(key, ":", value, " ")
})
fmt.Println("}")
Expand All @@ -29,36 +29,36 @@ func main() {
m.Put("a", 1)
printMap("Initial", m) // { a:1 b:2 c:3 d:4 e:5 f:6 g:7 }

even := m.Select(func(key interface{}, value interface{}) bool {
even := m.Select(func(key any, value any) bool {
return value.(int)%2 == 0
})
printMap("Elements with even values", even) // { b:2 d:4 f:6 }

foundKey, foundValue := m.Find(func(key interface{}, value interface{}) bool {
foundKey, foundValue := m.Find(func(key any, value any) bool {
return value.(int)%2 == 0 && value.(int)%3 == 0
})
if foundKey != nil {
fmt.Println("Element with value divisible by 2 and 3 found is", foundValue, "with key", foundKey) // value: 6, index: 4
}

square := m.Map(func(key interface{}, value interface{}) (interface{}, interface{}) {
square := m.Map(func(key any, value any) (any, any) {
return key.(string) + key.(string), value.(int) * value.(int)
})
printMap("Elements' values squared and letters duplicated", square) // { aa:1 bb:4 cc:9 dd:16 ee:25 ff:36 gg:49 }

bigger := m.Any(func(key interface{}, value interface{}) bool {
bigger := m.Any(func(key any, value any) bool {
return value.(int) > 5
})
fmt.Println("Map contains element whose value is bigger than 5 is", bigger) // true

positive := m.All(func(key interface{}, value interface{}) bool {
positive := m.All(func(key any, value any) bool {
return value.(int) > 0
})
fmt.Println("All map's elements have positive values is", positive) // true

evenNumbersSquared := m.Select(func(key interface{}, value interface{}) bool {
evenNumbersSquared := m.Select(func(key any, value any) bool {
return value.(int)%2 == 0
}).Map(func(key interface{}, value interface{}) (interface{}, interface{}) {
}).Map(func(key any, value any) (any, any) {
return key, value.(int) * value.(int)
})
printMap("Chaining", evenNumbersSquared) // { b:4 d:16 f:36 }
Expand Down
2 changes: 1 addition & 1 deletion examples/godsort/godsort.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "github.com/emirpasic/gods/utils"

// SortExample to demonstrate basic usage of basic sort
func main() {
strings := []interface{}{} // []
strings := []any{} // []
strings = append(strings, "d") // ["d"]
strings = append(strings, "a") // ["d","a"]
strings = append(strings, "b") // ["d","a",b"
Expand Down
2 changes: 1 addition & 1 deletion examples/iteratorwithindex/iteratorwithindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func main() {
}

// Seek element starting with "b"
seek := func(index int, value interface{}) bool {
seek := func(index int, value any) bool {
return strings.HasSuffix(value.(string), "b")
}

Expand Down
2 changes: 1 addition & 1 deletion examples/iteratorwithkey/iteratorwithkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
}

// Seek key-value pair whose value starts with "b"
seek := func(key interface{}, value interface{}) bool {
seek := func(key any, value any) bool {
return strings.HasSuffix(value.(string), "b")
}

Expand Down
2 changes: 1 addition & 1 deletion examples/priorityqueue/priorityqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Element struct {
}

// Comparator function (sort by element's priority value in descending order)
func byPriority(a, b interface{}) int {
func byPriority(a, b any) int {
priorityA := a.(Element).priority
priorityB := b.(Element).priority
return -utils.IntComparator(priorityA, priorityB) // "-" descending order
Expand Down
8 changes: 4 additions & 4 deletions examples/redblacktreeextended/redblacktreeextended.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type RedBlackTreeExtended struct {
}

// GetMin gets the min value and flag if found
func (tree *RedBlackTreeExtended) GetMin() (value interface{}, found bool) {
func (tree *RedBlackTreeExtended) GetMin() (value any, found bool) {
node, found := tree.getMinFromNode(tree.Root)
if node != nil {
return node.Value, found
Expand All @@ -24,7 +24,7 @@ func (tree *RedBlackTreeExtended) GetMin() (value interface{}, found bool) {
}

// GetMax gets the max value and flag if found
func (tree *RedBlackTreeExtended) GetMax() (value interface{}, found bool) {
func (tree *RedBlackTreeExtended) GetMax() (value any, found bool) {
node, found := tree.getMaxFromNode(tree.Root)
if node != nil {
return node.Value, found
Expand All @@ -33,7 +33,7 @@ func (tree *RedBlackTreeExtended) GetMax() (value interface{}, found bool) {
}

// RemoveMin removes the min value and flag if found
func (tree *RedBlackTreeExtended) RemoveMin() (value interface{}, deleted bool) {
func (tree *RedBlackTreeExtended) RemoveMin() (value any, deleted bool) {
node, found := tree.getMinFromNode(tree.Root)
if found {
tree.Remove(node.Key)
Expand All @@ -43,7 +43,7 @@ func (tree *RedBlackTreeExtended) RemoveMin() (value interface{}, deleted bool)
}

// RemoveMax removes the max value and flag if found
func (tree *RedBlackTreeExtended) RemoveMax() (value interface{}, deleted bool) {
func (tree *RedBlackTreeExtended) RemoveMax() (value any, deleted bool) {
node, found := tree.getMaxFromNode(tree.Root)
if found {
tree.Remove(node.Key)
Expand Down
Loading