Compare commits

..

No commits in common. "fb3012448d773e657c88f06fa7398e388db64cff" and "f8635b5cf2d6ecc9ee6d143ed9b40f4a7a7f7813" have entirely different histories.

16 changed files with 35 additions and 171 deletions

0
go.sum
View File

View File

@ -30,11 +30,6 @@ 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]

View File

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

View File

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

View File

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

View File

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

View File

@ -1,13 +0,0 @@
package ds
type ArrayRing[T any] struct {
*ListRing[T, *ArrayList[*T]]
}
var _ Ring[int] = &ArrayRing[int]{}
func NewArrayRing[T any](size int) *ArrayRing[T] {
return &ArrayRing[T]{
ListRing: NewListRing[T](NewArrayList[*T](size), size),
}
}

View File

@ -1,13 +0,0 @@
package ds
type LinkedListRing[T any] struct {
*ListRing[T, *LinkedList[*T]]
}
var _ Ring[int] = &LinkedListRing[int]{}
func NewLinkedListRing[T any](size int) *LinkedListRing[T] {
return &LinkedListRing[T]{
ListRing: NewListRing[T](NewLinkedList[*T](), size),
}
}

View File

@ -1,65 +0,0 @@
package ds
type ListRing[T any, L List[*T]] struct {
list L
current int
}
var _ Ring[int] = &ListRing[int, List[*int]]{}
func NewListRing[T any, L List[*T]](list L, size int) *ListRing[T, L] {
preAllocList[*T, List[*T]](list, nil, size)
return &ListRing[T, L]{
list: list,
current: 0,
}
}
func (r *ListRing[T, L]) Next() Ring[T] {
return &ListRing[T, L]{
list: r.list,
current: modAbs(r.current+1, r.list.Size()),
}
}
func (r *ListRing[T, L]) Prev() Ring[T] {
return &ListRing[T, L]{
list: r.list,
current: modAbs(r.current-1, r.list.Size()),
}
}
func (r *ListRing[T, L]) Set(v T) {
r.list.Set(r.current, &v)
}
func (r *ListRing[T, L]) Has() bool {
return r.list.Get(r.current) != nil
}
func (r *ListRing[T, L]) Get() (T, bool) {
if !r.Has() {
return *new(T), false
}
return *r.list.Get(r.current), true
}
func (r *ListRing[T, L]) GetOrDefault(defaultValue T) T {
value, ok := r.Get()
if !ok {
return defaultValue
}
return value
}
func (r *ListRing[T, L]) Each(f func(value *T)) {
var t Ring[T] = r
for i := 0; i < r.list.Size(); i++ {
if value, ok := t.Get(); ok {
f(&value)
} else {
f(nil)
}
t = t.Next()
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -1,13 +0,0 @@
package ds
type Ring[T any] interface {
Iterable[*T]
Next() Ring[T]
Prev() Ring[T]
Set(T)
Has() bool
Get() (T, bool)
GetOrDefault(defaultValue T) T
}

View File

@ -11,7 +11,6 @@ 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 {

View File

@ -1,15 +0,0 @@
package ds
func modAbs(a, b int) int {
v := a % b
if v < 0 {
return b + v
}
return v
}
func preAllocList[T any, L List[T]](list L, value T, size int) {
for list.Size() < size {
list.Add(value)
}
}