segmentedSlice

package module
v0.0.0-...-da9dc9f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2017 License: Apache-2.0 Imports: 4 Imported by: 0

README

SegmentedSlice GoDoc Build Status

A fast, index-able, sort-able, grow-only Slice.

FAQ

Why?
  • Appending to a normal slice can get slow and very memory heavy as the slice grows, and for a lot of work loads it's usually append-only with some sorting.

Benchmarks

➤ go18 test -bench=. -benchmem -benchtime=5s
BenchmarkAppendSegmentedSlice-8         200000000               42.4 ns/op            25 B/op          1 allocs/op
BenchmarkAppendNormalSlice-8            20000000               252 ns/op              88 B/op          1 allocs/op
PASS
ok      github.com/OneOfOne/segmentedSlice      20.752s

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultSegmentLen = 128

DefaultSegmentLen is used if segLen is 0, mostly during an auto-constructed slice from JSON.

Functions

This section is empty.

Types

type Iterator

type Iterator struct {
	// contains filtered or unexported fields
}

Iterator is a SegmentedSlice iterator.

func (*Iterator) More

func (it *Iterator) More() bool

More returns true if the iterator have more items/

func (*Iterator) Next

func (it *Iterator) Next() (val interface{})

Next returns the next item.

func (*Iterator) NextIndex

func (it *Iterator) NextIndex() (idx int, val interface{})

NextIndex returns the next item and index.

type Slice

type Slice struct {
	// contains filtered or unexported fields
}

Slice is a special slice-of-slices, when it grows it creates a new internal slice rather than growing and copying data.

func New

func New(segLen int) *Slice

New returns a new Slice with the specified segment length. Length must be a power of two or 0, if it is 0 it will use the DefaultSegmentLen.

func NewSortable

func NewSortable(segLen int, lessFn func(a, b interface{}) bool) *Slice

NewSortable returns a Slice that supports the sort.Interface Length must be a power of two or 0, if it is 0 it will use the DefaultSegmentLen.

func (*Slice) Append

func (ss *Slice) Append(vals ...interface{})

Append appends vals to the slice. If used on a sub-slice, it turns into an independent slice.

func (*Slice) AppendTo

func (ss *Slice) AppendTo(oss *Slice) *Slice

AppendTo appends all the data in the current slice to `other` and returns `other`.

func (*Slice) Cap

func (ss *Slice) Cap() int

Cap returns the max number of elements the slice can hold before growinging

func (*Slice) Copy

func (ss *Slice) Copy() *Slice

Copy returns an exact copy of the slice that could be used independently. Copy is internally used if you call Append, Pop or Grow on a sub-slice.

func (*Slice) ForEach

func (ss *Slice) ForEach(fn func(i int, v interface{}) (breakNow bool)) bool

ForEach is an alias for ForEachAt(0, fn).

func (*Slice) ForEachAt

func (ss *Slice) ForEachAt(i int, fn func(i int, v interface{}) (breakNow bool)) bool

ForEachAt loops over the slice and calls fn for each element. If fn returns true, it breaks early and returns true otherwise returns false.

func (*Slice) Get

func (ss *Slice) Get(i int) interface{}

Get returns the item at the specified index, if i > Cap(), it panics.

func (*Slice) GoString

func (ss *Slice) GoString() string

GoString implements fmt.GoStringer

func (*Slice) Grow

func (ss *Slice) Grow(sz int) int

Grow grows internal data structure to fit `sz` amount of new items. If used on a sub-slice, it turns into an independent slice.

func (*Slice) Iter

func (ss *Slice) Iter() *Iterator

Iter is an alias for IterAt(0, ss.Len()).

func (*Slice) IterAt

func (ss *Slice) IterAt(start, end int) *Iterator

IterAt returns an Iterator object Example:

for it := ss.IterAt(0, ss.Len()); it.More(); {
	log.Println(it.Next())
}

func (*Slice) Len

func (ss *Slice) Len() int

Len returns the number of elements in the slice.

func (*Slice) Less

func (ss *Slice) Less(i, j int) bool

Less adds support for sort.Interface

func (*Slice) MarshalJSON

func (ss *Slice) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*Slice) Pop

func (ss *Slice) Pop() (v interface{})

Pop deletes and returns the last item in the slice. If used on a sub-slice, it turns into an independent slice.

func (*Slice) Segments

func (ss *Slice) Segments() int

Segments returns the number of segments.

func (*Slice) Set

func (ss *Slice) Set(i int, v interface{})

Set sets the value at the specified index, if i > Cap(), it panics.

func (*Slice) SetUnmarshalType

func (ss *Slice) SetUnmarshalType(val interface{})

SetUnmarshalType sets the internal type used for UnmarshalJSON. Example:

ss.SetUnmarshalType(&DataStruct{})
ss.SetUnmarshalType(reflect.TypeOf(&DataStruct{}))

func (*Slice) Slice

func (ss *Slice) Slice(start, end int) *Slice

Slice returns a sub-slice, the equivalent of ss[start:end], modifying any data in the returned slice modifies the parent.

func (*Slice) String

func (ss *Slice) String() string

String implements fmt.Stringer

func (*Slice) Swap

func (ss *Slice) Swap(i, j int)

Swap adds support for sort.Interface

func (*Slice) UnmarshalJSON

func (ss *Slice) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON implements json.Unmarshaler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL