-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ptr): add ValueOr, ValueOrZero, Compare, Equal
- Loading branch information
Showing
4 changed files
with
111 additions
and
150 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |