fixed generic types for data structures implemented via lists

This commit is contained in:
milarin 2023-07-10 17:16:41 +02:00
parent f8635b5cf2
commit 0b5f57e0c2
11 changed files with 52 additions and 35 deletions

0
go.sum Normal file
View File

View File

@ -30,6 +30,11 @@ func (s *ArrayList[T]) Get(index int) T {
return s.values[index] 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 { func (s *ArrayList[T]) RemoveAt(index int) T {
s.init() s.init()
ret := s.values[index] ret := s.values[index]

View File

@ -1,5 +1,7 @@
package ds package ds
import "fmt"
type LinkedList[T any] struct { type LinkedList[T any] struct {
head *linkedListNode[T] head *linkedListNode[T]
tail *linkedListNode[T] tail *linkedListNode[T]
@ -8,8 +10,8 @@ type LinkedList[T any] struct {
var _ List[int] = &LinkedList[int]{} var _ List[int] = &LinkedList[int]{}
func NewLinkedList[T any]() *ArrayList[T] { func NewLinkedList[T any]() *LinkedList[T] {
return &ArrayList[T]{} return &LinkedList[T]{}
} }
func (l *LinkedList[T]) Add(value 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 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 { func (l *LinkedList[T]) RemoveAt(index int) T {
node := l.getNode(index) node := l.getNode(index)
@ -79,6 +85,10 @@ func (l *LinkedList[T]) Empty() bool {
} }
func (l *LinkedList[T]) getNode(index int) *linkedListNode[T] { 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 { if index == 0 {
return l.head return l.head
} }

View File

@ -1,7 +1,7 @@
package ds package ds
type ArrayQueue[T any] struct { type ArrayQueue[T any] struct {
*ListQueue[T] *ListQueue[T, *ArrayList[T]]
} }
var _ Queue[int] = &ArrayQueue[int]{} var _ Queue[int] = &ArrayQueue[int]{}

View File

@ -1,7 +1,7 @@
package ds package ds
type LinkedListQueue[T any] struct { type LinkedListQueue[T any] struct {
*ListQueue[T] *ListQueue[T, *LinkedList[T]]
} }
var _ Queue[int] = &LinkedListQueue[int]{} var _ Queue[int] = &LinkedListQueue[int]{}

View File

@ -1,47 +1,47 @@
package ds package ds
type ListQueue[T any] struct { type ListQueue[T any, L List[T]] struct {
list List[T] list L
} }
var _ Queue[int] = &ListQueue[int]{} var _ Queue[int] = &ListQueue[int, List[int]]{}
func NewListQueue[T any](list List[T]) *ListQueue[T] { func NewListQueue[T any, L List[T]](list L) *ListQueue[T, L] {
return &ListQueue[T]{list} return &ListQueue[T, L]{list}
} }
func (s *ListQueue[T]) Enqueue(value T) { func (s *ListQueue[T, L]) Enqueue(value T) {
s.list.Add(value) s.list.Add(value)
} }
func (s *ListQueue[T]) Add(value T) { func (s *ListQueue[T, L]) Add(value T) {
s.Enqueue(value) s.Enqueue(value)
} }
func (s *ListQueue[T]) AddAll(values Iterable[T]) { func (s *ListQueue[T, L]) AddAll(values Iterable[T]) {
values.Each(s.Add) values.Each(s.Add)
} }
func (s *ListQueue[T]) Dequeue() T { func (s *ListQueue[T, L]) Dequeue() T {
return s.list.RemoveAt(0) return s.list.RemoveAt(0)
} }
func (s *ListQueue[T]) Peek() T { func (s *ListQueue[T, L]) Peek() T {
return s.PeekAt(0) 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) return s.list.Get(index)
} }
func (s *ListQueue[T]) Size() int { func (s *ListQueue[T, L]) Size() int {
return s.list.Size() return s.list.Size()
} }
func (s *ListQueue[T]) Empty() bool { func (s *ListQueue[T, L]) Empty() bool {
return s.Size() == 0 return s.Size() == 0
} }
func (s *ListQueue[T]) Clear() { func (s *ListQueue[T, L]) Clear() {
s.list.Clear() s.list.Clear()
} }

View File

@ -1,7 +1,7 @@
package ds package ds
type ArrayStack[T any] struct { type ArrayStack[T any] struct {
*ListStack[T] *ListStack[T, *ArrayList[T]]
} }
var _ Stack[int] = &ArrayStack[int]{} var _ Stack[int] = &ArrayStack[int]{}

View File

@ -1,7 +1,7 @@
package ds package ds
type LinkedListStack[T any] struct { type LinkedListStack[T any] struct {
*ListStack[T] *ListStack[T, *LinkedList[T]]
} }
var _ Stack[int] = &LinkedListStack[int]{} var _ Stack[int] = &LinkedListStack[int]{}

View File

@ -1,47 +1,47 @@
package ds package ds
type ListStack[T any] struct { type ListStack[T any, L List[T]] struct {
list List[T] list L
} }
var _ Stack[int] = &ListStack[int]{} var _ Stack[int] = &ListStack[int, List[int]]{}
func NewListStack[T any](list List[T]) *ListStack[T] { func NewListStack[T any, L List[T]](list L) *ListStack[T, L] {
return &ListStack[T]{list} return &ListStack[T, L]{list}
} }
func (s *ListStack[T]) Push(value T) { func (s *ListStack[T, L]) Push(value T) {
s.list.Add(value) s.list.Add(value)
} }
func (s *ListStack[T]) Add(value T) { func (s *ListStack[T, L]) Add(value T) {
s.Push(value) s.Push(value)
} }
func (s *ListStack[T]) AddAll(values Iterable[T]) { func (s *ListStack[T, L]) AddAll(values Iterable[T]) {
values.Each(s.Add) 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) 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) 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) return s.list.Get(index)
} }
func (s *ListStack[T]) Size() int { func (s *ListStack[T, L]) Size() int {
return s.list.Size() return s.list.Size()
} }
func (s *ListStack[T]) Empty() bool { func (s *ListStack[T, L]) Empty() bool {
return s.Size() == 0 return s.Size() == 0
} }
func (s *ListStack[T]) Clear() { func (s *ListStack[T, L]) Clear() {
s.list.Clear() s.list.Clear()
} }

View File

@ -1,2 +1,3 @@
package ds package ds
// TODO
// TODO

View File

@ -11,6 +11,7 @@ type Retrievable[T comparable] interface {
type Indexable[K comparable, T any] interface { type Indexable[K comparable, T any] interface {
Get(index K) T Get(index K) T
Set(index K, value T)
} }
type IndexedRemovable[K comparable, T any] interface { type IndexedRemovable[K comparable, T any] interface {