initial commit

This commit is contained in:
milarin 2023-01-20 21:48:46 +01:00
commit 9f3c498f5b
16 changed files with 426 additions and 0 deletions

58
impl_list_array.go Normal file
View File

@ -0,0 +1,58 @@
package ds
type ArrayList[T any] struct {
values []T
}
var _ List[int] = &ArrayList[int]{}
func NewArrayList[T any](initCap int) *ArrayList[T] {
return &ArrayList[T]{make([]T, 0, initCap)}
}
func (s *ArrayList[T]) init() {
if s.values == nil {
s.values = make([]T, 0)
}
}
func (s *ArrayList[T]) Add(value T) {
s.init()
s.values = append(s.values, value)
}
func (s *ArrayList[T]) AddAll(values Iterable[T]) {
values.Each(s.Add)
}
func (s *ArrayList[T]) Get(index int) T {
s.init()
return s.values[index]
}
func (s *ArrayList[T]) RemoveAt(index int) T {
s.init()
ret := s.values[index]
s.values = append(s.values[:index], s.values[index+1:]...)
return ret
}
func (s *ArrayList[T]) Clear() {
s.values = make([]T, 0)
}
func (s *ArrayList[T]) Size() int {
s.init()
return len(s.values)
}
func (s *ArrayList[T]) Empty() bool {
return s.Size() == 0
}
func (s *ArrayList[T]) Each(f func(value T)) {
s.init()
for _, value := range s.values {
f(value)
}
}

112
impl_list_linked.go Normal file
View File

@ -0,0 +1,112 @@
package ds
type LinkedList[T any] struct {
head *linkedListNode[T]
tail *linkedListNode[T]
length int
}
var _ List[int] = &LinkedList[int]{}
func NewLinkedList[T any]() *ArrayList[T] {
return &ArrayList[T]{}
}
func (l *LinkedList[T]) Add(value T) {
if l.length == 0 {
l.head = &linkedListNode[T]{value: value}
l.tail = l.head
l.length++
return
}
newNode := &linkedListNode[T]{
value: value,
prev: l.tail,
}
l.tail.next = newNode
l.tail = newNode
l.length++
}
func (l *LinkedList[T]) AddAll(values Iterable[T]) {
values.Each(l.Add)
}
func (l *LinkedList[T]) Get(index int) T {
return l.getNode(index).value
}
func (l *LinkedList[T]) RemoveAt(index int) T {
node := l.getNode(index)
if node.prev != nil {
node.prev.next = node.next
} else {
l.head = node.next
}
if node.next != nil {
node.next.prev = node.prev
} else {
l.tail = node.prev
}
l.length--
return node.value
}
func (l *LinkedList[T]) Clear() {
l.head = nil
l.tail = nil
l.length = 0
}
func (l *LinkedList[T]) Each(f func(value T)) {
node := l.head
for node != nil {
f(node.value)
node = node.next
}
}
func (l *LinkedList[T]) Size() int {
return l.length
}
func (l *LinkedList[T]) Empty() bool {
return l.Size() == 0
}
func (l *LinkedList[T]) getNode(index int) *linkedListNode[T] {
if index == 0 {
return l.head
}
if index == l.length-1 {
return l.tail
}
if index > l.length/2 {
index = l.length - index
node := l.tail
for index > 0 {
node = node.prev
index--
}
return node
}
node := l.head
for index > 0 {
node = node.next
index--
}
return node
}
type linkedListNode[T any] struct {
value T
next *linkedListNode[T]
prev *linkedListNode[T]
}

8
impl_queue_array.go Normal file
View File

@ -0,0 +1,8 @@
package ds
type ArrayQueue[T any] ListQueue[T]
func NewArrayQueue[T any]() *ArrayQueue[T] {
stack := NewListQueue[T](new(ArrayList[T]))
return (*ArrayQueue[T])(stack)
}

View File

@ -0,0 +1,8 @@
package ds
type LinkedListQueue[T any] ListQueue[T]
func NewLinkedListQueue[T any]() *LinkedListQueue[T] {
stack := NewListQueue[T](new(LinkedList[T]))
return (*LinkedListQueue[T])(stack)
}

43
impl_queue_list.go Normal file
View File

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

61
impl_set.go Normal file
View File

@ -0,0 +1,61 @@
package ds
type Set[T comparable] struct {
values map[T]struct{}
}
var _ Coll[int] = &Set[int]{}
func NewSet[T comparable]() *Set[T] {
return &Set[T]{}
}
func (s *Set[T]) init() {
if s.values == nil {
s.values = map[T]struct{}{}
}
}
func (s *Set[T]) Add(value T) {
s.init()
s.values[value] = struct{}{}
}
func (s *Set[T]) AddAll(values Iterable[T]) {
s.init()
values.Each(s.Add)
}
func (s *Set[T]) Remove(value T) {
s.init()
delete(s.values, value)
}
func (s *Set[T]) RemoveAll(values Iterable[T]) {
values.Each(s.Remove)
}
func (s *Set[T]) Clear() {
s.values = map[T]struct{}{}
}
func (s *Set[T]) Has(value T) bool {
s.init()
_, ok := s.values[value]
return ok
}
func (s *Set[T]) Size() int {
s.init()
return len(s.values)
}
func (s *Set[T]) Empty() bool {
return s.Size() == 0
}
func (s *Set[T]) Each(f func(value T)) {
for value := range s.values {
f(value)
}
}

8
impl_stack_array.go Normal file
View File

@ -0,0 +1,8 @@
package ds
type ArrayStack[T any] ListStack[T]
func NewArrayStack[T any]() *ArrayStack[T] {
stack := NewListStack[T](new(ArrayList[T]))
return (*ArrayStack[T])(stack)
}

View File

@ -0,0 +1,8 @@
package ds
type LinkedListStack[T any] ListStack[T]
func NewLinkedListStack[T any]() *ArrayStack[T] {
stack := NewListStack[T](new(LinkedList[T]))
return (*ArrayStack[T])(stack)
}

43
impl_stack_list.go Normal file
View File

@ -0,0 +1,43 @@
package ds
type ListStack[T any] struct {
list List[T]
}
var _ Stack[int] = &ListStack[int]{}
func NewListStack[T any](list List[T]) *ListStack[T] {
return &ListStack[T]{list}
}
func (s *ListStack[T]) Push(value T) {
s.list.Add(value)
}
func (s *ListStack[T]) Add(value T) {
s.Push(value)
}
func (s *ListStack[T]) AddAll(values Iterable[T]) {
values.Each(s.Add)
}
func (s *ListStack[T]) Pop() T {
return s.list.RemoveAt(s.list.Size() - 1)
}
func (s *ListStack[T]) Peek() T {
return s.PeekAt(s.Size() - 1)
}
func (s *ListStack[T]) PeekAt(index int) T {
return s.list.Get(index)
}
func (s *ListStack[T]) Size() int {
return s.list.Size()
}
func (s *ListStack[T]) Empty() bool {
return s.Size() == 0
}

8
int_coll.go Normal file
View File

@ -0,0 +1,8 @@
package ds
type Coll[T any] interface {
Addable[T]
Iterable[T]
Sized
Clearable
}

7
int_list.go Normal file
View File

@ -0,0 +1,7 @@
package ds
type List[T any] interface {
Coll[T]
Indexable[int, T]
IndexedRemovable[int, T]
}

1
int_map.go Normal file
View File

@ -0,0 +1 @@
// TODO

12
int_queue.go Normal file
View File

@ -0,0 +1,12 @@
package ds
type Queue[T any] interface {
Addable[T]
Sized
Enqueue(value T)
Dequeue() T
Peek() T
PeekAt(index int) T
}

12
int_stack.go Normal file
View File

@ -0,0 +1,12 @@
package ds
type Stack[T any] interface {
Addable[T]
Sized
Push(value T)
Pop() T
Peek() T
PeekAt(index int) T
}

1
int_tree.go Normal file
View File

@ -0,0 +1 @@
// TODO

36
interfaces.go Normal file
View File

@ -0,0 +1,36 @@
package ds
type Addable[T any] interface {
Add(value T)
AddAll(values Iterable[T])
}
type Retrievable[T comparable] interface {
Has(value T) bool
}
type Indexable[K comparable, T any] interface {
Get(index K) T
}
type IndexedRemovable[K comparable, T any] interface {
RemoveAt(index K) T
}
type ComparingRemovable[T any] interface {
Remove(value T)
RemoveAll(values Iterable[T])
}
type Clearable interface {
Clear()
}
type Sized interface {
Size() int
Empty() bool
}
type Iterable[T any] interface {
Each(f func(value T))
}