Skip to content

Commit

Permalink
feat(ptr): add ValueOr, ValueOrZero, Compare, Equal
Browse files Browse the repository at this point in the history
  • Loading branch information
leaxoy committed Jun 30, 2023
1 parent 4fd2696 commit 12307c4
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 150 deletions.
81 changes: 0 additions & 81 deletions collections/collection.go

This file was deleted.

67 changes: 67 additions & 0 deletions collections/linked/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package linked

import (
"github.com/go-board/std/iter"
"github.com/go-board/std/optional"
)

type List[T any] struct {
head *listNode[T]
tail *listNode[T]
}

var _ iter.Iterable[any] = (*List[any])(nil)

type listNode[T any] struct {
data T
next *listNode[T]
}

func NewList[T any]() *List[T] {
return &List[T]{}
}

func FromSlice[T any](elems ...T) *List[T] {
list := NewList[T]()
for _, ele := range elems {
list.Append(ele)
}
return list
}

func FromIterator[T any](iter iter.Iter[T]) *List[T] {
list := NewList[T]()
for e := iter.Next(); e.IsSome(); e = iter.Next() {
list.Append(e.Value())
}
return list
}

func (self *List[T]) Iter() iter.Iter[T] {
return &listIter[T]{nextNode: self.head, prevNode: self.tail}
}

func (self *List[T]) Append(data T) {
listNode := &listNode[T]{data: data, next: nil}
if self.tail != nil {
self.tail = listNode
}
self.head = listNode
self.tail = listNode
}

type listIter[T any] struct {
nextNode *listNode[T]
prevNode *listNode[T]
}

var _ iter.Iter[any] = (*listIter[any])(nil)

func (self *listIter[T]) Next() optional.Optional[T] {
if self.nextNode != nil {
value := self.nextNode.data
self.nextNode = self.nextNode.next
return optional.Some(value)
}
return optional.None[T]()
}
67 changes: 0 additions & 67 deletions collections/linkedlist/linked_list.go

This file was deleted.

46 changes: 44 additions & 2 deletions ptr/ptr.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
package ptr

import "github.com/go-board/std/cmp"

func zero[T any]() (v T) { return }

// Ref return reference of value
func Ref[T any](t T) *T { return &t }

// Default return default value of type
func Default[T any]() *T {
var data T
return Ref(data)
return Ref(zero[T]())
}

// ValueOr return value of pointer if not nil, else return default value.
func ValueOr[T any](v *T, d T) T {
if v == nil {
return d
}
return *v
}

// ValueOrZero return value of pointer if not nil, else return zero value.
func ValueOrZero[T any](v *T) T {
return ValueOr(v, zero[T]())
}

// Compare compares two pointer. If both non-nil, compare underlying data,
// if both nil, return 0, non-nil pointer is always greater than nil pointer.
func Compare[T cmp.Ordered](l, r *T) int {
if l != nil && r != nil {
return cmp.Compare(*l, *r)
}
if l == nil && r == nil {
return 0
}
if l != nil {
return +1
}
return -1
}

// Equal test whether two pointer are equal. If both non-nil, test underlying data,
// if both nil, return true, else return false
func Equal[T comparable](l, r *T) bool {
if l != nil && r != nil {
return *l == *r
} else if l == nil && r == nil {
return true
}
return false
}

0 comments on commit 12307c4

Please sign in to comment.