Skip to content

Commit db85782

Browse files
merge: replaced 'interface{}' with 'any' using new go1.18 alias (#506)
1 parent d696a49 commit db85782

22 files changed

+90
-90
lines changed

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ import (
9393
// Fprint formats using the default formats for its operands and writes to w.
9494
// Spaces are added between operands when neither is a string.
9595
// It returns the number of bytes written, and any write error encountered.
96-
func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
96+
func Fprint(w io.Writer, a ...any) (n int, err error) {
9797
...
9898
}
9999

graph/dijkstra.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type Item struct {
77
dist int
88
}
99

10-
func (a Item) More(b interface{}) bool {
10+
func (a Item) More(b any) bool {
1111
// reverse direction for minheap
1212
return a.dist < b.(Item).dist
1313
}

sort/heapsort.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ func (h MaxHeap) heapifyDown(i int) {
107107

108108
type Comparable interface {
109109
Idx() int
110-
More(interface{}) bool
110+
More(any) bool
111111
}
112112
type Int int
113113

114-
func (a Int) More(b interface{}) bool {
114+
func (a Int) More(b any) bool {
115115
return a > b.(Int)
116116
}
117117
func (a Int) Idx() int {

structure/dynamicarray/dynamicarray.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ var defaultCapacity = 10
2121
type DynamicArray struct {
2222
Size int
2323
Capacity int
24-
ElementData []interface{}
24+
ElementData []any
2525
}
2626

2727
// Put function is change/update the value in array with the index and new value
28-
func (da *DynamicArray) Put(index int, element interface{}) error {
28+
func (da *DynamicArray) Put(index int, element any) error {
2929
err := da.CheckRangeFromIndex(index)
3030

3131
if err != nil {
@@ -38,7 +38,7 @@ func (da *DynamicArray) Put(index int, element interface{}) error {
3838
}
3939

4040
// Add function is add new element to our array
41-
func (da *DynamicArray) Add(element interface{}) {
41+
func (da *DynamicArray) Add(element any) {
4242
if da.Size == da.Capacity {
4343
da.NewCapacity()
4444
}
@@ -64,7 +64,7 @@ func (da *DynamicArray) Remove(index int) error {
6464
}
6565

6666
// Get function is return one element with the index of array
67-
func (da *DynamicArray) Get(index int) (interface{}, error) {
67+
func (da *DynamicArray) Get(index int) (any, error) {
6868
err := da.CheckRangeFromIndex(index)
6969

7070
if err != nil {
@@ -80,7 +80,7 @@ func (da *DynamicArray) IsEmpty() bool {
8080
}
8181

8282
// GetData function return all value of array
83-
func (da *DynamicArray) GetData() []interface{} {
83+
func (da *DynamicArray) GetData() []any {
8484
return da.ElementData[:da.Size]
8585
}
8686

@@ -100,7 +100,7 @@ func (da *DynamicArray) NewCapacity() {
100100
da.Capacity = da.Capacity << 1
101101
}
102102

103-
newDataElement := make([]interface{}, da.Capacity)
103+
newDataElement := make([]any, da.Capacity)
104104

105105
copy(newDataElement, da.ElementData)
106106

structure/dynamicarray/dynamicarray_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestDynamicArray(t *testing.T) {
2626
if numbers.IsEmpty() != false {
2727
t.Errorf("Expected be false but got %v", numbers.IsEmpty())
2828
}
29-
var res []interface{}
29+
var res []any
3030
res = append(res, 10)
3131
res = append(res, 20)
3232
res = append(res, 30)
@@ -43,7 +43,7 @@ func TestDynamicArray(t *testing.T) {
4343
if numbers.IsEmpty() != false {
4444
t.Errorf("Expected be false but got %v", numbers.IsEmpty())
4545
}
46-
var res []interface{}
46+
var res []any
4747
res = append(res, 10)
4848
res = append(res, 30)
4949
res = append(res, 40)

structure/hashmap/hashmap.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
var defaultCapacity uint64 = 1 << 10
99

1010
type node struct {
11-
key interface{}
12-
value interface{}
11+
key any
12+
value any
1313
next *node
1414
}
1515

@@ -38,7 +38,7 @@ func Make(size, capacity uint64) HashMap {
3838
}
3939

4040
// Get returns value associated with given key
41-
func (hm *HashMap) Get(key interface{}) interface{} {
41+
func (hm *HashMap) Get(key any) any {
4242
node := hm.getNodeByHash(hm.hash(key))
4343

4444
if node != nil {
@@ -49,17 +49,17 @@ func (hm *HashMap) Get(key interface{}) interface{} {
4949
}
5050

5151
// Put puts new key value in hashmap
52-
func (hm *HashMap) Put(key interface{}, value interface{}) interface{} {
52+
func (hm *HashMap) Put(key any, value any) any {
5353
return hm.putValue(hm.hash(key), key, value)
5454
}
5555

5656
// Contains checks if given key is stored in hashmap
57-
func (hm *HashMap) Contains(key interface{}) bool {
57+
func (hm *HashMap) Contains(key any) bool {
5858
node := hm.getNodeByHash(hm.hash(key))
5959
return node != nil
6060
}
6161

62-
func (hm *HashMap) putValue(hash uint64, key interface{}, value interface{}) interface{} {
62+
func (hm *HashMap) putValue(hash uint64, key any, value any) any {
6363
if hm.capacity == 0 {
6464
hm.capacity = defaultCapacity
6565
hm.table = make([]*node, defaultCapacity)
@@ -106,22 +106,22 @@ func (hm *HashMap) resize() {
106106
}
107107
}
108108

109-
func newNode(key interface{}, value interface{}) *node {
109+
func newNode(key any, value any) *node {
110110
return &node{
111111
key: key,
112112
value: value,
113113
}
114114
}
115115

116-
func newNodeWithNext(key interface{}, value interface{}, next *node) *node {
116+
func newNodeWithNext(key any, value any, next *node) *node {
117117
return &node{
118118
key: key,
119119
value: value,
120120
next: next,
121121
}
122122
}
123123

124-
func (hm *HashMap) hash(key interface{}) uint64 {
124+
func (hm *HashMap) hash(key any) uint64 {
125125
h := fnv.New64a()
126126
_, _ = h.Write([]byte(fmt.Sprintf("%v", key)))
127127

structure/hashmap/hashmap_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestHashMap(t *testing.T) {
5959
})
6060

6161
t.Run("Test 7: Checking if the key does not exist Get func returns nil", func(t *testing.T) {
62-
want := interface{}(nil)
62+
want := any(nil)
6363
got := mp.Get(2)
6464
if got != want {
6565
t.Errorf("Key '2' does not exists in the map but it says otherwise")

structure/linkedlist/cyclic.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func NewCyclic() *Cyclic {
1717
// point to itself. For other cases, the node will be added
1818
// to the end of the list. End of the list is Prev field of
1919
// current item. Complexity O(1).
20-
func (cl *Cyclic) Add(val interface{}) {
20+
func (cl *Cyclic) Add(val any) {
2121
n := NewNode(val)
2222
cl.Size++
2323
if cl.Head == nil {

structure/linkedlist/cyclic_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ func TestAdd(t *testing.T) {
1515
list := NewCyclic()
1616
fillList(list, 3)
1717

18-
want := []interface{}{1, 2, 3}
19-
var got []interface{}
18+
want := []any{1, 2, 3}
19+
var got []any
2020
var start *Node
2121
start = list.Head
2222

structure/linkedlist/doubly.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewDoubly() *Doubly {
2424
}
2525

2626
// AddAtBeg Add a node to the beginning of the linkedlist
27-
func (ll *Doubly) AddAtBeg(val interface{}) {
27+
func (ll *Doubly) AddAtBeg(val any) {
2828
n := NewNode(val)
2929
n.Next = ll.Head
3030

@@ -37,7 +37,7 @@ func (ll *Doubly) AddAtBeg(val interface{}) {
3737
}
3838

3939
// AddAtEnd Add a node at the end of the linkedlist
40-
func (ll *Doubly) AddAtEnd(val interface{}) {
40+
func (ll *Doubly) AddAtEnd(val any) {
4141
n := NewNode(val)
4242

4343
if ll.Head == nil {
@@ -53,7 +53,7 @@ func (ll *Doubly) AddAtEnd(val interface{}) {
5353
}
5454

5555
// DelAtBeg Delete the node at the beginning of the linkedlist
56-
func (ll *Doubly) DelAtBeg() interface{} {
56+
func (ll *Doubly) DelAtBeg() any {
5757
if ll.Head == nil {
5858
return -1
5959
}
@@ -68,7 +68,7 @@ func (ll *Doubly) DelAtBeg() interface{} {
6868
}
6969

7070
// DetAtEnd Delete a node at the end of the linkedlist
71-
func (ll *Doubly) DelAtEnd() interface{} {
71+
func (ll *Doubly) DelAtEnd() any {
7272
// no item
7373
if ll.Head == nil {
7474
return -1
@@ -90,7 +90,7 @@ func (ll *Doubly) DelAtEnd() interface{} {
9090
}
9191

9292
// Count Number of nodes in the linkedlist
93-
func (ll *Doubly) Count() interface{} {
93+
func (ll *Doubly) Count() any {
9494
var ctr int = 0
9595

9696
for cur := ll.Head; cur != nil; cur = cur.Next {

structure/linkedlist/shared.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package linkedlist
33
// Node Structure representing the linkedlist node.
44
// This node is shared across different implementations.
55
type Node struct {
6-
Val interface{}
6+
Val any
77
Prev *Node
88
Next *Node
99
}
1010

1111
// Create new node.
12-
func NewNode(val interface{}) *Node {
12+
func NewNode(val any) *Node {
1313
return &Node{val, nil, nil}
1414
}

structure/linkedlist/singlylinkedlist.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ func NewSingly() *Singly {
2121
}
2222

2323
// AddAtBeg adds a new snode with given value at the beginning of the list.
24-
func (ll *Singly) AddAtBeg(val interface{}) {
24+
func (ll *Singly) AddAtBeg(val any) {
2525
n := NewNode(val)
2626
n.Next = ll.Head
2727
ll.Head = n
2828
ll.length++
2929
}
3030

3131
// AddAtEnd adds a new snode with given value at the end of the list.
32-
func (ll *Singly) AddAtEnd(val interface{}) {
32+
func (ll *Singly) AddAtEnd(val any) {
3333
n := NewNode(val)
3434

3535
if ll.Head == nil {
@@ -46,7 +46,7 @@ func (ll *Singly) AddAtEnd(val interface{}) {
4646
}
4747

4848
// DelAtBeg deletes the snode at the head(beginning) of the list and returns its value. Returns -1 if the list is empty.
49-
func (ll *Singly) DelAtBeg() interface{} {
49+
func (ll *Singly) DelAtBeg() any {
5050
if ll.Head == nil {
5151
return -1
5252
}
@@ -59,7 +59,7 @@ func (ll *Singly) DelAtBeg() interface{} {
5959
}
6060

6161
// DelAtEnd deletes the snode at the tail(end) of the list and returns its value. Returns -1 if the list is empty.
62-
func (ll *Singly) DelAtEnd() interface{} {
62+
func (ll *Singly) DelAtEnd() any {
6363
if ll.Head == nil {
6464
return -1
6565
}

structure/linkedlist/singlylinkedlist_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ func TestSingly(t *testing.T) {
1212
list.AddAtBeg(3)
1313

1414
t.Run("Test AddAtBeg()", func(t *testing.T) {
15-
want := []interface{}{3, 2, 1}
16-
got := []interface{}{}
15+
want := []any{3, 2, 1}
16+
got := []any{}
1717
current := list.Head
1818
got = append(got, current.Val)
1919
for current.Next != nil {
@@ -28,8 +28,8 @@ func TestSingly(t *testing.T) {
2828
list.AddAtEnd(4)
2929

3030
t.Run("Test AddAtEnd()", func(t *testing.T) {
31-
want := []interface{}{3, 2, 1, 4}
32-
got := []interface{}{}
31+
want := []any{3, 2, 1, 4}
32+
got := []any{}
3333
current := list.Head
3434
got = append(got, current.Val)
3535
for current.Next != nil {
@@ -42,15 +42,15 @@ func TestSingly(t *testing.T) {
4242
})
4343

4444
t.Run("Test DelAtBeg()", func(t *testing.T) {
45-
want := interface{}(3)
45+
want := any(3)
4646
got := list.DelAtBeg()
4747
if got != want {
4848
t.Errorf("got: %v, want: %v", got, want)
4949
}
5050
})
5151

5252
t.Run("Test DelAtEnd()", func(t *testing.T) {
53-
want := interface{}(4)
53+
want := any(4)
5454
got := list.DelAtEnd()
5555
if got != want {
5656
t.Errorf("got: %v, want: %v", got, want)
@@ -74,8 +74,8 @@ func TestSingly(t *testing.T) {
7474
list2.AddAtBeg(6)
7575

7676
t.Run("Test Reverse()", func(t *testing.T) {
77-
want := []interface{}{1, 2, 3, 4, 5, 6}
78-
got := []interface{}{}
77+
want := []any{1, 2, 3, 4, 5, 6}
78+
got := []any{}
7979
list2.Reverse()
8080
current := list2.Head
8181
got = append(got, current.Val)
@@ -89,8 +89,8 @@ func TestSingly(t *testing.T) {
8989
})
9090

9191
t.Run("Test ReversePartition()", func(t *testing.T) {
92-
want := []interface{}{1, 5, 4, 3, 2, 6}
93-
got := []interface{}{}
92+
want := []any{1, 5, 4, 3, 2, 6}
93+
got := []any{}
9494
err := list2.ReversePartition(2, 5)
9595

9696
if err != nil {

structure/queue/queue_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestQueue(t *testing.T) {
109109
}
110110
})
111111

112-
ListQueue = []interface{}{}
112+
ListQueue = []any{}
113113

114114
t.Run("Test Queue isEmpty", func(t *testing.T) {
115115

@@ -125,7 +125,7 @@ func TestQueue(t *testing.T) {
125125
}
126126
})
127127

128-
ListQueue = []interface{}{}
128+
ListQueue = []any{}
129129
t.Run("Test Queue Length", func(t *testing.T) {
130130
if LenQueue() != 0 {
131131
t.Errorf("Test Queue Length is wrong the result must be %v but got %v", 0, LenQueue())

0 commit comments

Comments
 (0)