From 0b5f57e0c2de98de2cd2788100065e6995f9bc03 Mon Sep 17 00:00:00 2001 From: milarin Date: Mon, 10 Jul 2023 17:16:41 +0200 Subject: [PATCH] fixed generic types for data structures implemented via lists --- go.sum | 0 impl_list_array.go | 5 +++++ impl_list_linked.go | 14 ++++++++++++-- impl_queue_array.go | 2 +- impl_queue_linked_list.go | 2 +- impl_queue_list.go | 28 ++++++++++++++-------------- impl_stack_array.go | 2 +- impl_stack_linked_list.go | 2 +- impl_stack_list.go | 28 ++++++++++++++-------------- int_map.go | 3 ++- interfaces.go | 1 + 11 files changed, 52 insertions(+), 35 deletions(-) create mode 100644 go.sum diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/impl_list_array.go b/impl_list_array.go index 954e2a6..a41df33 100644 --- a/impl_list_array.go +++ b/impl_list_array.go @@ -30,6 +30,11 @@ func (s *ArrayList[T]) Get(index int) T { return s.values[index] } +func (s *ArrayList[T]) Set(index int, value T) { + s.init() + s.values[index] = value +} + func (s *ArrayList[T]) RemoveAt(index int) T { s.init() ret := s.values[index] diff --git a/impl_list_linked.go b/impl_list_linked.go index c860aaa..242a3fc 100644 --- a/impl_list_linked.go +++ b/impl_list_linked.go @@ -1,5 +1,7 @@ package ds +import "fmt" + type LinkedList[T any] struct { head *linkedListNode[T] tail *linkedListNode[T] @@ -8,8 +10,8 @@ type LinkedList[T any] struct { var _ List[int] = &LinkedList[int]{} -func NewLinkedList[T any]() *ArrayList[T] { - return &ArrayList[T]{} +func NewLinkedList[T any]() *LinkedList[T] { + return &LinkedList[T]{} } func (l *LinkedList[T]) Add(value T) { @@ -37,6 +39,10 @@ func (l *LinkedList[T]) Get(index int) T { return l.getNode(index).value } +func (l *LinkedList[T]) Set(index int, value T) { + l.getNode(index).value = value +} + func (l *LinkedList[T]) RemoveAt(index int) T { node := l.getNode(index) @@ -79,6 +85,10 @@ func (l *LinkedList[T]) Empty() bool { } func (l *LinkedList[T]) getNode(index int) *linkedListNode[T] { + if index < 0 || index > l.length { + panic(fmt.Sprintf("invalid index %d for list with size %d", index, l.Size())) + } + if index == 0 { return l.head } diff --git a/impl_queue_array.go b/impl_queue_array.go index ed6f605..7d59350 100644 --- a/impl_queue_array.go +++ b/impl_queue_array.go @@ -1,7 +1,7 @@ package ds type ArrayQueue[T any] struct { - *ListQueue[T] + *ListQueue[T, *ArrayList[T]] } var _ Queue[int] = &ArrayQueue[int]{} diff --git a/impl_queue_linked_list.go b/impl_queue_linked_list.go index 9fbb5fa..a9e201c 100644 --- a/impl_queue_linked_list.go +++ b/impl_queue_linked_list.go @@ -1,7 +1,7 @@ package ds type LinkedListQueue[T any] struct { - *ListQueue[T] + *ListQueue[T, *LinkedList[T]] } var _ Queue[int] = &LinkedListQueue[int]{} diff --git a/impl_queue_list.go b/impl_queue_list.go index 9e74c19..627a193 100644 --- a/impl_queue_list.go +++ b/impl_queue_list.go @@ -1,47 +1,47 @@ package ds -type ListQueue[T any] struct { - list List[T] +type ListQueue[T any, L List[T]] struct { + list L } -var _ Queue[int] = &ListQueue[int]{} +var _ Queue[int] = &ListQueue[int, List[int]]{} -func NewListQueue[T any](list List[T]) *ListQueue[T] { - return &ListQueue[T]{list} +func NewListQueue[T any, L List[T]](list L) *ListQueue[T, L] { + return &ListQueue[T, L]{list} } -func (s *ListQueue[T]) Enqueue(value T) { +func (s *ListQueue[T, L]) Enqueue(value T) { s.list.Add(value) } -func (s *ListQueue[T]) Add(value T) { +func (s *ListQueue[T, L]) Add(value T) { s.Enqueue(value) } -func (s *ListQueue[T]) AddAll(values Iterable[T]) { +func (s *ListQueue[T, L]) AddAll(values Iterable[T]) { values.Each(s.Add) } -func (s *ListQueue[T]) Dequeue() T { +func (s *ListQueue[T, L]) Dequeue() T { return s.list.RemoveAt(0) } -func (s *ListQueue[T]) Peek() T { +func (s *ListQueue[T, L]) Peek() T { return s.PeekAt(0) } -func (s *ListQueue[T]) PeekAt(index int) T { +func (s *ListQueue[T, L]) PeekAt(index int) T { return s.list.Get(index) } -func (s *ListQueue[T]) Size() int { +func (s *ListQueue[T, L]) Size() int { return s.list.Size() } -func (s *ListQueue[T]) Empty() bool { +func (s *ListQueue[T, L]) Empty() bool { return s.Size() == 0 } -func (s *ListQueue[T]) Clear() { +func (s *ListQueue[T, L]) Clear() { s.list.Clear() } diff --git a/impl_stack_array.go b/impl_stack_array.go index bdc7745..e3f72c2 100644 --- a/impl_stack_array.go +++ b/impl_stack_array.go @@ -1,7 +1,7 @@ package ds type ArrayStack[T any] struct { - *ListStack[T] + *ListStack[T, *ArrayList[T]] } var _ Stack[int] = &ArrayStack[int]{} diff --git a/impl_stack_linked_list.go b/impl_stack_linked_list.go index cfbe572..777c8e8 100644 --- a/impl_stack_linked_list.go +++ b/impl_stack_linked_list.go @@ -1,7 +1,7 @@ package ds type LinkedListStack[T any] struct { - *ListStack[T] + *ListStack[T, *LinkedList[T]] } var _ Stack[int] = &LinkedListStack[int]{} diff --git a/impl_stack_list.go b/impl_stack_list.go index 09fa46d..f4cd511 100644 --- a/impl_stack_list.go +++ b/impl_stack_list.go @@ -1,47 +1,47 @@ package ds -type ListStack[T any] struct { - list List[T] +type ListStack[T any, L List[T]] struct { + list L } -var _ Stack[int] = &ListStack[int]{} +var _ Stack[int] = &ListStack[int, List[int]]{} -func NewListStack[T any](list List[T]) *ListStack[T] { - return &ListStack[T]{list} +func NewListStack[T any, L List[T]](list L) *ListStack[T, L] { + return &ListStack[T, L]{list} } -func (s *ListStack[T]) Push(value T) { +func (s *ListStack[T, L]) Push(value T) { s.list.Add(value) } -func (s *ListStack[T]) Add(value T) { +func (s *ListStack[T, L]) Add(value T) { s.Push(value) } -func (s *ListStack[T]) AddAll(values Iterable[T]) { +func (s *ListStack[T, L]) AddAll(values Iterable[T]) { values.Each(s.Add) } -func (s *ListStack[T]) Pop() T { +func (s *ListStack[T, L]) Pop() T { return s.list.RemoveAt(s.list.Size() - 1) } -func (s *ListStack[T]) Peek() T { +func (s *ListStack[T, L]) Peek() T { return s.PeekAt(s.Size() - 1) } -func (s *ListStack[T]) PeekAt(index int) T { +func (s *ListStack[T, L]) PeekAt(index int) T { return s.list.Get(index) } -func (s *ListStack[T]) Size() int { +func (s *ListStack[T, L]) Size() int { return s.list.Size() } -func (s *ListStack[T]) Empty() bool { +func (s *ListStack[T, L]) Empty() bool { return s.Size() == 0 } -func (s *ListStack[T]) Clear() { +func (s *ListStack[T, L]) Clear() { s.list.Clear() } diff --git a/int_map.go b/int_map.go index 85ad324..7e61d76 100644 --- a/int_map.go +++ b/int_map.go @@ -1,2 +1,3 @@ package ds -// TODO \ No newline at end of file + +// TODO diff --git a/interfaces.go b/interfaces.go index d959027..b2f090c 100644 --- a/interfaces.go +++ b/interfaces.go @@ -11,6 +11,7 @@ type Retrievable[T comparable] interface { type Indexable[K comparable, T any] interface { Get(index K) T + Set(index K, value T) } type IndexedRemovable[K comparable, T any] interface {