Skip to content

Commit

Permalink
Doc
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrr committed Sep 28, 2022
1 parent def61b6 commit f52eb2c
Show file tree
Hide file tree
Showing 23 changed files with 282 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
55 changes: 45 additions & 10 deletions bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ import (
"unsafe"
)

// Bytes wraps the behavior of Vec as Vec[byte] adding new methods on top.
type Bytes Vec[byte]

var (
_ io.WriterTo = Bytes{}
_ io.ReaderFrom = Bytes{}
)

// NewBytes returns a Bytes instance with size `size` and capacity `capacity`.
func NewBytes(size, capacity int) Bytes {
return BytesFrom(make([]byte, size, capacity))
}

// BytesFrom creates a Bytes instance from `bts` bytes.
func BytesFrom(bts []byte) Bytes {
return Bytes(bts)
}

// Slice returns a sliced Bytes using indexes starting from `low` and ending in `max`.
func (b Bytes) Slice(low, max int) Bytes {
if max <= 0 {
return b[low:]
Expand All @@ -29,73 +33,104 @@ func (b Bytes) Slice(low, max int) Bytes {
return b[low:max]
}

func (b *Bytes) CopyFrom(b2 Bytes) {
b.Reserve(b2.Len())
copy(*b, b2)
// CopyFrom copies the bytes from `other` to `b`.
func (b *Bytes) CopyFrom(other Bytes) {
b.Reserve(other.Len())
copy(*b, other)
}

// WriteTo writes the bytes to an io.Writer.
//
// Implements the io.WriterTo interface.
func (b Bytes) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(b)

return int64(n), err
}

// ReadFrom reads bytes from an io.Reader into `b`.
//
// This function does NOT append bytes, it uses the existing buffer.
//
// Implements the io.ReaderFrom interface.
func (b Bytes) ReadFrom(r io.Reader) (int64, error) {
n, err := r.Read(b)

return int64(n), err
}

// LimitReadFrom is like ReadFrom but it is limited to `n` bytes.
func (b *Bytes) LimitReadFrom(r io.Reader, n int) (int64, error) {
b.Reserve(n)
return b.Slice(0, n).ReadFrom(r)
}

// LimitWriteTo writes a limited amount from `b` to `w`.
// LimitWriteTo writes a limited amount of `n` bytes to an io.Writer.
func (b Bytes) LimitWriteTo(w io.Writer, n int) (int64, error) {
return b.Slice(0, Min(b.Len(), n)).WriteTo(w)
}

// Len returns the length of `b`.
//
// This function is equivalent to `len(b))`.
func (b Bytes) Len() int {
return len(b)
}

// Cap returns the capacity of `b`.
//
// This function is equivalent to `cap(b))`.
func (b Bytes) Cap() int {
return cap(b)
}

// String returns the `Bytes`' string representation.
// String converts `b` to a string.
//
// This function produces an allocation. To avoid the allocation use UnsafeString.
func (b Bytes) String() string {
return string(b)
}

// UnsafeString converts `b` to a string without producing allocations.
func (b Bytes) UnsafeString() string {
return *(*string)(unsafe.Pointer(&b))
}

func (b Bytes) Index(c byte) int {
// IndexByte returns the index of `c` in `b`.
//
// This function is equivalent to bytes.IndexByte(b, c).
func (b Bytes) IndexByte(c byte) int {
return bytes.IndexByte(b, c)
}

func (b Bytes) Contains(c byte) bool {
return bytes.IndexByte(b, c) != -1
// Index returns the index of `other` in `b`.
//
// This function is equivalent to bytes.Index(b, other).
func (b Bytes) Index(other Bytes) int {
return bytes.Index(b, other)
}

// Resize increases or decreases the size of Bytes.
func (b *Bytes) Resize(n int) {
vc := (*Vec[byte])(b)
vc.Resize(n)
}

// Reserve increases the size of Bytes if needed.
//
// The call doesn't produce any allocation if `n` < cap(b).
func (b *Bytes) Reserve(n int) {
vc := (*Vec[byte])(b)
vc.Reserve(n)
}

func (b *Bytes) Append(bts ...byte) {
// Append appends bytes at the end of Bytes.
func (b *Bytes) Append(others ...byte) {
vc := (*Vec[byte])(b)
vc.Append(bts...)
vc.Append(others...)
}

// Push pushes the `bts` to the beginning of Bytes.
func (b *Bytes) Push(bts ...byte) {
vc := (*Vec[byte])(b)
vc.Push(bts...)
Expand Down
15 changes: 13 additions & 2 deletions iter.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
package tl

// Iter defines an iterator interface.
type Iter[T any] interface {
// Next is called to iterate to the next element.
//
// Returns true if a next element is available, false otherwise.
Next() bool
// Get returns the current element.
Get() T
// GetPtr returns a pointer to the current element.
GetPtr() *T
}

// IterDrop implements an iterator which current element can be dropped.
type IterDrop[T any] interface {
Iter[T]

// Drop removes the current element from the iterator.
Drop()
}

// IterBidir defines a bidirectional iterator.
type IterBidir[T any] interface {
Iter[T]

// Back is like next but for going backwards. It moves the iterator to the previous position.
// For some implementations that might mean going in reverse mode (not going backwards).
Back() bool
}

// IterDropBidir merges IterBidir and IterDrop in one interface.
type IterDropBidir[T any] interface {
IterDrop[T]
IterBidir[T]
}

// Advance advances iter a number of `count` positions.
func Advance[T any](iter Iter[T], count int) {
for i := 0; i < count && iter.Next(); i++ {
}
Expand Down
1 change: 1 addition & 0 deletions iter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (iter *iterFilter[T]) GetPtr() *T {
return iter.inner.GetPtr()
}

// Filter filters the values of iterators.
func Filter[T any](inner tl.Iter[T], fn tl.CompareFunc[T]) tl.Iter[T] {
return &iterFilter[T]{
inner: inner,
Expand Down
1 change: 1 addition & 0 deletions iter/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (iter *iterFlatten[T]) GetPtr() *T {
return &iter.current
}

// Flatten flattens the elements of `inner`.
func Flatten[T any](inner tl.Iter[[]T]) tl.Iter[T] {
return &iterFlatten[T]{
inner: inner,
Expand Down
2 changes: 2 additions & 0 deletions iter/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iter

import "github.com/dgrr/tl"

// Index indexes an element inside an iterator.
func Index[T any](iter tl.Iter[T], cmpFn tl.CompareFunc[T]) int {
i := 0
for ; iter.Next(); i++ {
Expand All @@ -13,6 +14,7 @@ func Index[T any](iter tl.Iter[T], cmpFn tl.CompareFunc[T]) int {
return -1
}

// Search searches for an element inside an iterator.
func Search[T any](iter tl.Iter[T], cmpFn tl.CompareFunc[T]) tl.Iter[T] {
for iter.Next() {
if cmpFn(iter.Get()) {
Expand Down
1 change: 1 addition & 0 deletions iter/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (iter *iterMap[T, V]) GetPtr() *V {
return &iter.current
}

// Map maps the values of an iterator using `conv`.
func Map[T, V any](inner tl.Iter[T], conv func(T) V) tl.Iter[V] {
return &iterMap[T, V]{
inner: inner,
Expand Down
1 change: 1 addition & 0 deletions iter/nth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (iter *iterNth[T]) GetPtr() *T {
return iter.val
}

// Nth takes the nth element of an iterator.
func Nth[T any](inner tl.Iter[T], nth int) tl.Iter[T] {
for nth != 0 {
nth--
Expand Down
1 change: 1 addition & 0 deletions iter/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (iter *iterSum[T]) GetPtr() *T {
return &iter.result
}

// Sum sums up all the elements inside an iterator.
func Sum[T constraints.Integer | constraints.Float](inner tl.Iter[T]) tl.Iter[T] {
return &iterSum[T]{
inner: inner,
Expand Down
1 change: 1 addition & 0 deletions iter/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (iter *iterRange[T]) GetPtr() *T {
return &iter.value
}

// Range creates an iterator over a range.
func Range[T constraints.Integer](start, stop, step T) tl.Iter[T] {
return &iterRange[T]{
start: start, stop: stop, step: step,
Expand Down
1 change: 1 addition & 0 deletions iter/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func AppendTo[T any](vs []T, iter tl.Iter[T]) []T {
return vs
}

// ToSlice converts a iterator to a slice.
func ToSlice[T any](iter tl.Iter[T]) []T {
newSlice := make([]T, 0)

Expand Down
3 changes: 3 additions & 0 deletions iter/unique.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,22 @@ func (iter *iterUnique[T]) GetPtr() *T {
return iter.inner.GetPtr()
}

// UniqueFn removes any duplicates from the iterator using `eq` as comparator.
func UniqueFn[T any](inner tl.Iter[T], eq func(a, b T) bool) tl.Iter[T] {
return &iterUnique[T]{
inner: inner,
eq: eq,
}
}

// Unique removes any duplicates from the iterator.
func Unique[T comparable](inner tl.Iter[T]) tl.Iter[T] {
return UniqueFn(inner, func(a, b T) bool {
return a == b
})
}

// Get returns the last element of the iterator.
func Get[T any](iter tl.Iter[T]) T {
for iter.Next() {
}
Expand Down
1 change: 1 addition & 0 deletions iter/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (iter *iterWindow[T]) GetPtr() *[]T {
return &iter.win
}

// Window returns an iterator containing the last `n` values.
func Window[T any](inner tl.Iter[T], n int) tl.Iter[[]T] {
return &iterWindow[T]{
inner: inner,
Expand Down
1 change: 1 addition & 0 deletions iter/windowCopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (iter *iterWindowCopy[T]) GetPtr() *[]T {
return &iter.win
}

// WindowCopy operates like Window but copying the values.
func WindowCopy[T any](inner tl.Iter[T], n int) tl.Iter[[]T] {
return &iterWindowCopy[T]{
inner: inner,
Expand Down
Loading

0 comments on commit f52eb2c

Please sign in to comment.