Skip to content

Commit

Permalink
Merge pull request #7 from 246859/feat-linkedlist
Browse files Browse the repository at this point in the history
Feat linkedlist
  • Loading branch information
246859 authored Dec 27, 2023
2 parents 0e12693 + cc579fc commit c720ac9
Show file tree
Hide file tree
Showing 8 changed files with 647 additions and 54 deletions.
3 changes: 1 addition & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

- list
- ArrayList ✔️
- LinkedList
- SinglyList
- LinkedList ✔️

- map
- HashMap
Expand Down
4 changes: 1 addition & 3 deletions heaps/binary_heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
var _ Heap[any] = (*BinaryHeap[any])(nil)

func NewBinaryHeap[T any](capacity int, compare containers.Compare[T]) *BinaryHeap[T] {
list := lists.NewArrayList[T](capacity, func(a, b T) bool {
return compare(a, b) == containers.EqualTo
})
list := lists.NewArrayList[T](capacity)

return &BinaryHeap[T]{
list: list,
Expand Down
55 changes: 26 additions & 29 deletions lists/arrary_list_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package lists

import (
"cmp"
"github.com/246859/containers"
"github.com/stretchr/testify/assert"
"testing"
)

func TestInitial(t *testing.T) {
_ = NewArrayList[int](32, containers.OrderedEqual[int])
_ = NewArrayList[string](32, containers.OrderedEqual[string])
_ = NewArrayList[int](32)
_ = NewArrayList[string](32)
type p struct {
age int
}
_ = NewArrayList[p](32, func(a, b p) bool {
return cmp.Compare(a.age, b.age) == containers.EqualTo
})
_ = NewArrayList[p](32)
}

func TestArrayList_Add(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

for i, num := range data {
Expand All @@ -32,32 +29,32 @@ func TestArrayList_Add(t *testing.T) {

func TestArrayList_IndexOf(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

for i, num := range data {
index := list.IndexOf(num)
index := list.IndexOf(num, containers.OrderedEqual[int])
assert.Equal(t, i, index)
}

assert.Equal(t, -1, list.IndexOf(100))
assert.Equal(t, -1, list.IndexOf(100, containers.OrderedEqual[int]))
}

func TestArrayList_Contains(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

for _, num := range data {
assert.True(t, list.Contains(num))
assert.True(t, list.Contains(num, containers.OrderedEqual[int]))
}

assert.False(t, list.Contains(100))
assert.False(t, list.Contains(100, containers.OrderedEqual[int]))
}

func TestArrayList_Size(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

assert.Equal(t, len(data), list.Size())
Expand All @@ -68,7 +65,7 @@ func TestArrayList_Size(t *testing.T) {

func TestArrayList_Set(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

{
Expand All @@ -88,7 +85,7 @@ func TestArrayList_Set(t *testing.T) {

func TestArrayList_Insert(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

{
Expand All @@ -105,26 +102,26 @@ func TestArrayList_Insert(t *testing.T) {

func TestArrayList_Remove(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

list.Remove(0)
get, b := list.Get(0)
assert.True(t, b)
assert.NotEqual(t, 1, get)

list.RemoveElem(0)
list.RemoveElem(2)
list.RemoveElem(3)
list.RemoveElem(0, containers.OrderedEqual[int])
list.RemoveElem(2, containers.OrderedEqual[int])
list.RemoveElem(3, containers.OrderedEqual[int])

assert.False(t, list.Contains(0))
assert.False(t, list.Contains(2))
assert.False(t, list.Contains(3))
assert.False(t, list.Contains(0, containers.OrderedEqual[int]))
assert.False(t, list.Contains(2, containers.OrderedEqual[int]))
assert.False(t, list.Contains(3, containers.OrderedEqual[int]))
}

func TestArrayList_Clone(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

coneList := list.Clone()
Expand All @@ -134,11 +131,11 @@ func TestArrayList_Clone(t *testing.T) {

func TestArrayList_Join(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

data1 := []int{10, 11, 12, 13, 14, 15}
list1 := NewArrayList[int](32, containers.OrderedEqual[int])
list1 := NewArrayList[int](32)
list1.Add(data1...)

list.Join(list1)
Expand All @@ -152,7 +149,7 @@ func TestArrayList_Join(t *testing.T) {

func TestArrayList_Clear(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

assert.Equal(t, len(data), list.Size())
Expand All @@ -162,7 +159,7 @@ func TestArrayList_Clear(t *testing.T) {

func TestArrayList_Values(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

values := list.Values()
Expand All @@ -174,7 +171,7 @@ func TestArrayList_Values(t *testing.T) {

func TestArrayList_Iterator(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
list := NewArrayList[int](32, containers.OrderedEqual[int])
list := NewArrayList[int](32)
list.Add(data...)

it := list.Iterator()
Expand Down
21 changes: 10 additions & 11 deletions lists/array_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ import (
var _ List[any] = (*ArrayList[any])(nil)

// NewArrayList returns a new ArrayList with the given capacity
func NewArrayList[T any](capacity int, equal containers.Equal[T]) *ArrayList[T] {
func NewArrayList[T any](capacity int) *ArrayList[T] {
list := &ArrayList[T]{
elems: make([]T, 0, capacity),
equal: equal,
}
return list
}

type ArrayList[T any] struct {
elems []T
equal containers.Equal[T]
}

func (a *ArrayList[T]) Get(i int) (_ T, _ bool) {
Expand All @@ -36,9 +34,9 @@ func (a *ArrayList[T]) Set(i int, elem T) {
a.elems[i] = elem
}

func (a *ArrayList[T]) IndexOf(elem T) int {
func (a *ArrayList[T]) IndexOf(elem T, equal containers.Equal[T]) int {
for i, e := range a.elems {
if a.equal(e, elem) {
if equal(e, elem) {
return i
}
}
Expand All @@ -65,15 +63,15 @@ func (a *ArrayList[T]) Remove(i int) {
}
}

func (a *ArrayList[T]) RemoveElem(elem T) {
i := a.IndexOf(elem)
func (a *ArrayList[T]) RemoveElem(elem T, equal containers.Equal[T]) {
i := a.IndexOf(elem, equal)
if i > -1 {
a.Remove(i)
}
}

func (a *ArrayList[T]) Contains(elem T) bool {
return a.IndexOf(elem) > -1
func (a *ArrayList[T]) Contains(elem T, equal containers.Equal[T]) bool {
return a.IndexOf(elem, equal) > -1
}

func (a *ArrayList[T]) Iterator() containers.IndexIterator[T] {
Expand Down Expand Up @@ -105,7 +103,6 @@ func (a *ArrayList[T]) Clone() List[T] {
snapshot := make([]T, size)
copy(snapshot, a.elems[:size])
return &ArrayList[T]{
equal: a.equal,
elems: snapshot,
}
}
Expand All @@ -115,9 +112,11 @@ func (a *ArrayList[T]) Join(list List[T]) {
return
}

var elems []T
for it := list.Iterator(); it.Valid(); it.Next() {
a.Add(it.Value())
elems = append(elems, it.Value())
}
a.Add(elems...)
}

func (a *ArrayList[T]) String() string {
Expand Down
Loading

0 comments on commit c720ac9

Please sign in to comment.