gf: github.com/gogf/gf/container/garray Index | Examples | Files

package garray

import "github.com/gogf/gf/container/garray"

Package garray provides most commonly used array containers which also support concurrent-safe/unsafe switch feature.

Index

Examples

Package Files

garray.go garray_func.go garray_normal_any.go garray_normal_int.go garray_normal_str.go garray_sorted_any.go garray_sorted_int.go garray_sorted_str.go

type Array Uses

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

Array is a golang array with rich features. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.

func New Uses

func New(safe ...bool) *Array

New creates and returns an empty array. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

Code:

// A normal array.
a := garray.New()

// Adding items.
for i := 0; i < 10; i++ {
    a.Append(i)
}

// Print the array length.
fmt.Println(a.Len())

// Print the array items.
fmt.Println(a.Slice())

// Retrieve item by index.
fmt.Println(a.Get(6))

// Check item existence.
fmt.Println(a.Contains(6))
fmt.Println(a.Contains(100))

// Insert item before specified index.
a.InsertAfter(9, 11)
// Insert item after specified index.
a.InsertBefore(10, 10)

fmt.Println(a.Slice())

// Modify item by index.
a.Set(0, 100)
fmt.Println(a.Slice())

// Search item and return its index.
fmt.Println(a.Search(5))

// Remove item by index.
a.Remove(0)
fmt.Println(a.Slice())

// Empty the array, removes all items of it.
fmt.Println(a.Slice())
a.Clear()
fmt.Println(a.Slice())

Output:

10
[0 1 2 3 4 5 6 7 8 9]
6 true
true
false
[0 1 2 3 4 5 6 7 8 9 10 11]
[100 1 2 3 4 5 6 7 8 9 10 11]
5
[1 2 3 4 5 6 7 8 9 10 11]
[1 2 3 4 5 6 7 8 9 10 11]
[]

func NewArray Uses

func NewArray(safe ...bool) *Array

See New.

func NewArrayFrom Uses

func NewArrayFrom(array []interface{}, safe ...bool) *Array

NewArrayFrom creates and returns an array with given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewArrayFromCopy Uses

func NewArrayFromCopy(array []interface{}, safe ...bool) *Array

NewArrayFromCopy creates and returns an array from a copy of given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewArrayRange Uses

func NewArrayRange(start, end, step int, safe ...bool) *Array

NewArrayRange creates and returns a array by a range from <start> to <end> with step value <step>.

func NewArraySize Uses

func NewArraySize(size int, cap int, safe ...bool) *Array

NewArraySize create and returns an array with given size and cap. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewFrom Uses

func NewFrom(array []interface{}, safe ...bool) *Array

See NewArrayFrom.

func NewFromCopy Uses

func NewFromCopy(array []interface{}, safe ...bool) *Array

See NewArrayFromCopy.

func (*Array) Append Uses

func (a *Array) Append(value ...interface{}) *Array

See PushRight.

func (*Array) Chunk Uses

func (a *Array) Chunk(size int) [][]interface{}

Chunk splits an array into multiple arrays, the size of each array is determined by <size>. The last chunk may contain less than size elements.

Code:

array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})

// Chunk splits an array into multiple arrays,
// the size of each array is determined by <size>.
// The last chunk may contain less than size elements.
fmt.Println(array.Chunk(2))

Output:

[[1 2] [3 4] [5 6] [7 8] [9]]

func (*Array) Clear Uses

func (a *Array) Clear() *Array

Clear deletes all items of current array.

func (*Array) Clone Uses

func (a *Array) Clone() (newArray *Array)

Clone returns a new array, which is a copy of current array.

func (*Array) Contains Uses

func (a *Array) Contains(value interface{}) bool

Contains checks whether a value exists in the array.

Code:

var array garray.StrArray
array.Append("a")
fmt.Println(array.Contains("a"))
fmt.Println(array.Contains("A"))
fmt.Println(array.ContainsI("A"))

Output:

true
false
true

func (*Array) CountValues Uses

func (a *Array) CountValues() map[interface{}]int

CountValues counts the number of occurrences of all values in the array.

func (*Array) Fill Uses

func (a *Array) Fill(startIndex int, num int, value interface{}) error

Fill fills an array with num entries of the value <value>, keys starting at the <startIndex> parameter.

func (*Array) FilterEmpty Uses

func (a *Array) FilterEmpty() *Array

FilterEmpty removes all empty value of the array. Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.

Code:

array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
fmt.Printf("%#v\n", array1.FilterNil().Slice())
fmt.Printf("%#v\n", array2.FilterEmpty().Slice())

Output:

[]interface {}{0, 1, 2, "", []interface {}{}, "john"}
[]interface {}{1, 2, "john"}

func (*Array) FilterNil Uses

func (a *Array) FilterNil() *Array

FilterNil removes all nil value of the array.

Code:

array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
fmt.Printf("%#v\n", array1.FilterNil().Slice())
fmt.Printf("%#v\n", array2.FilterEmpty().Slice())

Output:

[]interface {}{0, 1, 2, "", []interface {}{}, "john"}
[]interface {}{1, 2, "john"}

func (*Array) Get Uses

func (a *Array) Get(index int) (value interface{}, found bool)

Get returns the value by the specified index. If the given <index> is out of range of the array, the <found> is false.

func (*Array) InsertAfter Uses

func (a *Array) InsertAfter(index int, value interface{}) error

InsertAfter inserts the <value> to the back of <index>.

func (*Array) InsertBefore Uses

func (a *Array) InsertBefore(index int, value interface{}) error

InsertBefore inserts the <value> to the front of <index>.

func (*Array) Interfaces Uses

func (a *Array) Interfaces() []interface{}

Interfaces returns current array as []interface{}.

func (*Array) IsEmpty Uses

func (a *Array) IsEmpty() bool

IsEmpty checks whether the array is empty.

func (*Array) Iterator Uses

func (a *Array) Iterator(f func(k int, v interface{}) bool)

Iterator is alias of IteratorAsc.

Code:

array := garray.NewArrayFrom(g.Slice{"a", "b", "c"})
// Iterator is alias of IteratorAsc, which iterates the array readonly in ascending order
//  with given callback function <f>.
// If <f> returns true, then it continues iterating; or false to stop.
array.Iterator(func(k int, v interface{}) bool {
    fmt.Println(k, v)
    return true
})
// IteratorDesc iterates the array readonly in descending order with given callback function <f>.
// If <f> returns true, then it continues iterating; or false to stop.
array.IteratorDesc(func(k int, v interface{}) bool {
    fmt.Println(k, v)
    return true
})

Output:

0 a
1 b
2 c
2 c
1 b
0 a

func (*Array) IteratorAsc Uses

func (a *Array) IteratorAsc(f func(k int, v interface{}) bool)

IteratorAsc iterates the array readonly in ascending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*Array) IteratorDesc Uses

func (a *Array) IteratorDesc(f func(k int, v interface{}) bool)

IteratorDesc iterates the array readonly in descending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*Array) Join Uses

func (a *Array) Join(glue string) string

Join joins array elements with a string <glue>.

Code:

array := garray.NewFrom(g.Slice{"a", "b", "c", "d"})
fmt.Println(array.Join(","))

Output:

a,b,c,d

func (*Array) Len Uses

func (a *Array) Len() int

Len returns the length of array.

func (*Array) LockFunc Uses

func (a *Array) LockFunc(f func(array []interface{})) *Array

LockFunc locks writing by callback function <f>.

func (Array) MarshalJSON Uses

func (a Array) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.

func (*Array) Merge Uses

func (a *Array) Merge(array interface{}) *Array

Merge merges <array> into current array. The parameter <array> can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.

Code:

array1 := garray.NewFrom(g.Slice{1, 2})
array2 := garray.NewFrom(g.Slice{3, 4})
slice1 := g.Slice{5, 6}
slice2 := []int{7, 8}
slice3 := []string{"9", "0"}
fmt.Println(array1.Slice())
array1.Merge(array1)
array1.Merge(array2)
array1.Merge(slice1)
array1.Merge(slice2)
array1.Merge(slice3)
fmt.Println(array1.Slice())

Output:

[1 2]
[1 2 1 2 3 4 5 6 7 8 9 0]

func (*Array) Pad Uses

func (a *Array) Pad(size int, val interface{}) *Array

Pad pads array to the specified length with <value>. If size is positive then the array is padded on the right, or negative on the left. If the absolute value of <size> is less than or equal to the length of the array then no padding takes place.

func (*Array) PopLeft Uses

func (a *Array) PopLeft() (value interface{}, found bool)

PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the <found> is false.

Code:

array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})

// Any Pop* functions pick, delete and return the item from array.

fmt.Println(array.PopLeft())
fmt.Println(array.PopLefts(2))
fmt.Println(array.PopRight())
fmt.Println(array.PopRights(2))

Output:

1 true
[2 3]
9 true
[7 8]

func (*Array) PopLefts Uses

func (a *Array) PopLefts(size int) []interface{}

PopLefts pops and returns <size> items from the beginning of array.

Code:

array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})

// Any Pop* functions pick, delete and return the item from array.

fmt.Println(array.PopLeft())
fmt.Println(array.PopLefts(2))
fmt.Println(array.PopRight())
fmt.Println(array.PopRights(2))

Output:

1 true
[2 3]
9 true
[7 8]

func (*Array) PopRand Uses

func (a *Array) PopRand() (value interface{}, found bool)

PopRand randomly pops and return an item out of array. Note that if the array is empty, the <found> is false.

Code:

array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})

// Randomly retrieve and return 2 items from the array.
// It does not delete the items from array.
fmt.Println(array.Rands(2))

// Randomly pick and return one item from the array.
// It deletes the picked up item from array.
fmt.Println(array.PopRand())

func (*Array) PopRands Uses

func (a *Array) PopRands(size int) []interface{}

PopRands randomly pops and returns <size> items out of array.

func (*Array) PopRight Uses

func (a *Array) PopRight() (value interface{}, found bool)

PopRight pops and returns an item from the end of array. Note that if the array is empty, the <found> is false.

Code:

array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})

// Any Pop* functions pick, delete and return the item from array.

fmt.Println(array.PopLeft())
fmt.Println(array.PopLefts(2))
fmt.Println(array.PopRight())
fmt.Println(array.PopRights(2))

Output:

1 true
[2 3]
9 true
[7 8]

func (*Array) PopRights Uses

func (a *Array) PopRights(size int) []interface{}

PopRights pops and returns <size> items from the end of array.

Code:

array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})

// Any Pop* functions pick, delete and return the item from array.

fmt.Println(array.PopLeft())
fmt.Println(array.PopLefts(2))
fmt.Println(array.PopRight())
fmt.Println(array.PopRights(2))

Output:

1 true
[2 3]
9 true
[7 8]

func (*Array) PushLeft Uses

func (a *Array) PushLeft(value ...interface{}) *Array

PushLeft pushes one or multiple items to the beginning of array.

func (*Array) PushRight Uses

func (a *Array) PushRight(value ...interface{}) *Array

PushRight pushes one or multiple items to the end of array. It equals to Append.

func (*Array) RLockFunc Uses

func (a *Array) RLockFunc(f func(array []interface{})) *Array

RLockFunc locks reading by callback function <f>.

func (*Array) Rand Uses

func (a *Array) Rand() (value interface{}, found bool)

Rand randomly returns one item from array(no deleting).

func (*Array) Rands Uses

func (a *Array) Rands(size int) []interface{}

Rands randomly returns <size> items from array(no deleting).

Code:

array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})

// Randomly retrieve and return 2 items from the array.
// It does not delete the items from array.
fmt.Println(array.Rands(2))

// Randomly pick and return one item from the array.
// It deletes the picked up item from array.
fmt.Println(array.PopRand())

func (*Array) Range Uses

func (a *Array) Range(start int, end ...int) []interface{}

Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.

If <end> is negative, then the offset will start from the end of array. If <end> is omitted, then the sequence will have everything from start up until the end of the array.

func (*Array) Remove Uses

func (a *Array) Remove(index int) (value interface{}, found bool)

Remove removes an item by index. If the given <index> is out of range of the array, the <found> is false.

func (*Array) RemoveValue Uses

func (a *Array) RemoveValue(value interface{}) bool

RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.

func (*Array) Replace Uses

func (a *Array) Replace(array []interface{}) *Array

Replace replaces the array items by given <array> from the beginning of array.

func (*Array) Reverse Uses

func (a *Array) Reverse() *Array

Reverse makes array with elements in reverse order.

Code:

array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})

// Reverse makes array with elements in reverse order.
fmt.Println(array.Reverse().Slice())

Output:

[9 8 7 6 5 4 3 2 1]

func (*Array) Search Uses

func (a *Array) Search(value interface{}) int

Search searches array by <value>, returns the index of <value>, or returns -1 if not exists.

func (*Array) Set Uses

func (a *Array) Set(index int, value interface{}) error

Set sets value to specified index.

func (*Array) SetArray Uses

func (a *Array) SetArray(array []interface{}) *Array

SetArray sets the underlying slice array with the given <array>.

func (*Array) Shuffle Uses

func (a *Array) Shuffle() *Array

Shuffle randomly shuffles the array.

Code:

array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})

// Shuffle randomly shuffles the array.
fmt.Println(array.Shuffle().Slice())

func (*Array) Slice Uses

func (a *Array) Slice() []interface{}

Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

func (*Array) SortFunc Uses

func (a *Array) SortFunc(less func(v1, v2 interface{}) bool) *Array

SortFunc sorts the array by custom function <less>.

func (*Array) String Uses

func (a *Array) String() string

String returns current array as a string, which implements like json.Marshal does.

func (*Array) SubSlice Uses

func (a *Array) SubSlice(offset int, length ...int) []interface{}

SubSlice returns a slice of elements from the array as specified by the <offset> and <size> parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.

If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.

If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.

Any possibility crossing the left border of array, it will fail.

func (*Array) Sum Uses

func (a *Array) Sum() (sum int)

Sum returns the sum of values in an array.

func (*Array) Unique Uses

func (a *Array) Unique() *Array

Unique uniques the array, clear repeated items. Example: [1,1,2,3,2] -> [1,2,3]

func (*Array) UnmarshalJSON Uses

func (a *Array) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*Array) UnmarshalValue Uses

func (a *Array) UnmarshalValue(value interface{}) error

UnmarshalValue is an interface implement which sets any type of value for array.

func (*Array) Walk Uses

func (a *Array) Walk(f func(value interface{}) interface{}) *Array

Walk applies a user supplied function <f> to every item of array.

type IntArray Uses

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

IntArray is a golang int array with rich features. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.

func NewIntArray Uses

func NewIntArray(safe ...bool) *IntArray

NewIntArray creates and returns an empty array. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewIntArrayFrom Uses

func NewIntArrayFrom(array []int, safe ...bool) *IntArray

NewIntArrayFrom creates and returns an array with given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewIntArrayFromCopy Uses

func NewIntArrayFromCopy(array []int, safe ...bool) *IntArray

NewIntArrayFromCopy creates and returns an array from a copy of given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewIntArrayRange Uses

func NewIntArrayRange(start, end, step int, safe ...bool) *IntArray

NewIntArrayRange creates and returns a array by a range from <start> to <end> with step value <step>.

func NewIntArraySize Uses

func NewIntArraySize(size int, cap int, safe ...bool) *IntArray

NewIntArraySize create and returns an array with given size and cap. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func (*IntArray) Append Uses

func (a *IntArray) Append(value ...int) *IntArray

See PushRight.

func (*IntArray) Chunk Uses

func (a *IntArray) Chunk(size int) [][]int

Chunk splits an array into multiple arrays, the size of each array is determined by <size>. The last chunk may contain less than size elements.

func (*IntArray) Clear Uses

func (a *IntArray) Clear() *IntArray

Clear deletes all items of current array.

func (*IntArray) Clone Uses

func (a *IntArray) Clone() (newArray *IntArray)

Clone returns a new array, which is a copy of current array.

func (*IntArray) Contains Uses

func (a *IntArray) Contains(value int) bool

Contains checks whether a value exists in the array.

func (*IntArray) CountValues Uses

func (a *IntArray) CountValues() map[int]int

CountValues counts the number of occurrences of all values in the array.

func (*IntArray) Fill Uses

func (a *IntArray) Fill(startIndex int, num int, value int) error

Fill fills an array with num entries of the value <value>, keys starting at the <startIndex> parameter.

func (*IntArray) FilterEmpty Uses

func (a *IntArray) FilterEmpty() *IntArray

FilterEmpty removes all zero value of the array.

func (*IntArray) Get Uses

func (a *IntArray) Get(index int) (value int, found bool)

Get returns the value by the specified index. If the given <index> is out of range of the array, the <found> is false.

func (*IntArray) InsertAfter Uses

func (a *IntArray) InsertAfter(index int, value int) error

InsertAfter inserts the <value> to the back of <index>.

func (*IntArray) InsertBefore Uses

func (a *IntArray) InsertBefore(index int, value int) error

InsertBefore inserts the <value> to the front of <index>.

func (*IntArray) Interfaces Uses

func (a *IntArray) Interfaces() []interface{}

Interfaces returns current array as []interface{}.

func (*IntArray) IsEmpty Uses

func (a *IntArray) IsEmpty() bool

IsEmpty checks whether the array is empty.

func (*IntArray) Iterator Uses

func (a *IntArray) Iterator(f func(k int, v int) bool)

Iterator is alias of IteratorAsc.

func (*IntArray) IteratorAsc Uses

func (a *IntArray) IteratorAsc(f func(k int, v int) bool)

IteratorAsc iterates the array readonly in ascending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*IntArray) IteratorDesc Uses

func (a *IntArray) IteratorDesc(f func(k int, v int) bool)

IteratorDesc iterates the array readonly in descending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*IntArray) Join Uses

func (a *IntArray) Join(glue string) string

Join joins array elements with a string <glue>.

func (*IntArray) Len Uses

func (a *IntArray) Len() int

Len returns the length of array.

func (*IntArray) LockFunc Uses

func (a *IntArray) LockFunc(f func(array []int)) *IntArray

LockFunc locks writing by callback function <f>.

func (IntArray) MarshalJSON Uses

func (a IntArray) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.

func (*IntArray) Merge Uses

func (a *IntArray) Merge(array interface{}) *IntArray

Merge merges <array> into current array. The parameter <array> can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.

func (*IntArray) Pad Uses

func (a *IntArray) Pad(size int, value int) *IntArray

Pad pads array to the specified length with <value>. If size is positive then the array is padded on the right, or negative on the left. If the absolute value of <size> is less than or equal to the length of the array then no padding takes place.

func (*IntArray) PopLeft Uses

func (a *IntArray) PopLeft() (value int, found bool)

PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the <found> is false.

func (*IntArray) PopLefts Uses

func (a *IntArray) PopLefts(size int) []int

PopLefts pops and returns <size> items from the beginning of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*IntArray) PopRand Uses

func (a *IntArray) PopRand() (value int, found bool)

PopRand randomly pops and return an item out of array. Note that if the array is empty, the <found> is false.

func (*IntArray) PopRands Uses

func (a *IntArray) PopRands(size int) []int

PopRands randomly pops and returns <size> items out of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*IntArray) PopRight Uses

func (a *IntArray) PopRight() (value int, found bool)

PopRight pops and returns an item from the end of array. Note that if the array is empty, the <found> is false.

func (*IntArray) PopRights Uses

func (a *IntArray) PopRights(size int) []int

PopRights pops and returns <size> items from the end of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*IntArray) PushLeft Uses

func (a *IntArray) PushLeft(value ...int) *IntArray

PushLeft pushes one or multiple items to the beginning of array.

func (*IntArray) PushRight Uses

func (a *IntArray) PushRight(value ...int) *IntArray

PushRight pushes one or multiple items to the end of array. It equals to Append.

func (*IntArray) RLockFunc Uses

func (a *IntArray) RLockFunc(f func(array []int)) *IntArray

RLockFunc locks reading by callback function <f>.

func (*IntArray) Rand Uses

func (a *IntArray) Rand() (value int, found bool)

Rand randomly returns one item from array(no deleting).

func (*IntArray) Rands Uses

func (a *IntArray) Rands(size int) []int

Rands randomly returns <size> items from array(no deleting).

func (*IntArray) Range Uses

func (a *IntArray) Range(start int, end ...int) []int

Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.

If <end> is negative, then the offset will start from the end of array. If <end> is omitted, then the sequence will have everything from start up until the end of the array.

func (*IntArray) Remove Uses

func (a *IntArray) Remove(index int) (value int, found bool)

Remove removes an item by index. If the given <index> is out of range of the array, the <found> is false.

func (*IntArray) RemoveValue Uses

func (a *IntArray) RemoveValue(value int) bool

RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.

func (*IntArray) Replace Uses

func (a *IntArray) Replace(array []int) *IntArray

Replace replaces the array items by given <array> from the beginning of array.

func (*IntArray) Reverse Uses

func (a *IntArray) Reverse() *IntArray

Reverse makes array with elements in reverse order.

func (*IntArray) Search Uses

func (a *IntArray) Search(value int) int

Search searches array by <value>, returns the index of <value>, or returns -1 if not exists.

func (*IntArray) Set Uses

func (a *IntArray) Set(index int, value int) error

Set sets value to specified index.

func (*IntArray) SetArray Uses

func (a *IntArray) SetArray(array []int) *IntArray

SetArray sets the underlying slice array with the given <array>.

func (*IntArray) Shuffle Uses

func (a *IntArray) Shuffle() *IntArray

Shuffle randomly shuffles the array.

func (*IntArray) Slice Uses

func (a *IntArray) Slice() []int

Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

func (*IntArray) Sort Uses

func (a *IntArray) Sort(reverse ...bool) *IntArray

Sort sorts the array in increasing order. The parameter <reverse> controls whether sort in increasing order(default) or decreasing order.

func (*IntArray) SortFunc Uses

func (a *IntArray) SortFunc(less func(v1, v2 int) bool) *IntArray

SortFunc sorts the array by custom function <less>.

func (*IntArray) String Uses

func (a *IntArray) String() string

String returns current array as a string, which implements like json.Marshal does.

func (*IntArray) SubSlice Uses

func (a *IntArray) SubSlice(offset int, length ...int) []int

SubSlice returns a slice of elements from the array as specified by the <offset> and <size> parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.

If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.

If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.

Any possibility crossing the left border of array, it will fail.

func (*IntArray) Sum Uses

func (a *IntArray) Sum() (sum int)

Sum returns the sum of values in an array.

func (*IntArray) Unique Uses

func (a *IntArray) Unique() *IntArray

Unique uniques the array, clear repeated items. Example: [1,1,2,3,2] -> [1,2,3]

func (*IntArray) UnmarshalJSON Uses

func (a *IntArray) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*IntArray) UnmarshalValue Uses

func (a *IntArray) UnmarshalValue(value interface{}) error

UnmarshalValue is an interface implement which sets any type of value for array.

func (*IntArray) Walk Uses

func (a *IntArray) Walk(f func(value int) int) *IntArray

Walk applies a user supplied function <f> to every item of array.

type SortedArray Uses

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

SortedArray is a golang sorted array with rich features. It is using increasing order in default, which can be changed by setting it a custom comparator. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.

func NewSortedArray Uses

func NewSortedArray(comparator func(a, b interface{}) int, safe ...bool) *SortedArray

NewSortedArray creates and returns an empty sorted array. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default. The parameter <comparator> used to compare values to sort in array, if it returns value < 0, means v1 < v2; the v1 will be inserted before v2; if it returns value = 0, means v1 = v2; the v1 will be replaced by v2; if it returns value > 0, means v1 > v2; the v1 will be inserted after v2;

func NewSortedArrayFrom Uses

func NewSortedArrayFrom(array []interface{}, comparator func(a, b interface{}) int, safe ...bool) *SortedArray

NewSortedArrayFrom creates and returns an sorted array with given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewSortedArrayFromCopy Uses

func NewSortedArrayFromCopy(array []interface{}, comparator func(a, b interface{}) int, safe ...bool) *SortedArray

NewSortedArrayFromCopy creates and returns an sorted array from a copy of given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewSortedArrayRange Uses

func NewSortedArrayRange(start, end, step int, comparator func(a, b interface{}) int, safe ...bool) *SortedArray

NewSortedArrayRange creates and returns a array by a range from <start> to <end> with step value <step>.

func NewSortedArraySize Uses

func NewSortedArraySize(cap int, comparator func(a, b interface{}) int, safe ...bool) *SortedArray

NewSortedArraySize create and returns an sorted array with given size and cap. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func (*SortedArray) Add Uses

func (a *SortedArray) Add(values ...interface{}) *SortedArray

Add adds one or multiple values to sorted array, the array always keeps sorted. It's alias of function Append, see Append.

func (*SortedArray) Append Uses

func (a *SortedArray) Append(values ...interface{}) *SortedArray

Append adds one or multiple values to sorted array, the array always keeps sorted.

func (*SortedArray) Chunk Uses

func (a *SortedArray) Chunk(size int) [][]interface{}

Chunk splits an array into multiple arrays, the size of each array is determined by <size>. The last chunk may contain less than size elements.

func (*SortedArray) Clear Uses

func (a *SortedArray) Clear() *SortedArray

Clear deletes all items of current array.

func (*SortedArray) Clone Uses

func (a *SortedArray) Clone() (newArray *SortedArray)

Clone returns a new array, which is a copy of current array.

func (*SortedArray) Contains Uses

func (a *SortedArray) Contains(value interface{}) bool

Contains checks whether a value exists in the array.

func (*SortedArray) CountValues Uses

func (a *SortedArray) CountValues() map[interface{}]int

CountValues counts the number of occurrences of all values in the array.

func (*SortedArray) FilterEmpty Uses

func (a *SortedArray) FilterEmpty() *SortedArray

FilterEmpty removes all empty value of the array. Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.

func (*SortedArray) FilterNil Uses

func (a *SortedArray) FilterNil() *SortedArray

FilterNil removes all nil value of the array.

func (*SortedArray) Get Uses

func (a *SortedArray) Get(index int) (value interface{}, found bool)

Get returns the value by the specified index. If the given <index> is out of range of the array, the <found> is false.

func (*SortedArray) Interfaces Uses

func (a *SortedArray) Interfaces() []interface{}

Interfaces returns current array as []interface{}.

func (*SortedArray) IsEmpty Uses

func (a *SortedArray) IsEmpty() bool

IsEmpty checks whether the array is empty.

func (*SortedArray) Iterator Uses

func (a *SortedArray) Iterator(f func(k int, v interface{}) bool)

Iterator is alias of IteratorAsc.

func (*SortedArray) IteratorAsc Uses

func (a *SortedArray) IteratorAsc(f func(k int, v interface{}) bool)

IteratorAsc iterates the array readonly in ascending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*SortedArray) IteratorDesc Uses

func (a *SortedArray) IteratorDesc(f func(k int, v interface{}) bool)

IteratorDesc iterates the array readonly in descending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*SortedArray) Join Uses

func (a *SortedArray) Join(glue string) string

Join joins array elements with a string <glue>.

func (*SortedArray) Len Uses

func (a *SortedArray) Len() int

Len returns the length of array.

func (*SortedArray) LockFunc Uses

func (a *SortedArray) LockFunc(f func(array []interface{})) *SortedArray

LockFunc locks writing by callback function <f>.

func (SortedArray) MarshalJSON Uses

func (a SortedArray) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.

func (*SortedArray) Merge Uses

func (a *SortedArray) Merge(array interface{}) *SortedArray

Merge merges <array> into current array. The parameter <array> can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.

func (*SortedArray) PopLeft Uses

func (a *SortedArray) PopLeft() (value interface{}, found bool)

PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the <found> is false.

func (*SortedArray) PopLefts Uses

func (a *SortedArray) PopLefts(size int) []interface{}

PopLefts pops and returns <size> items from the beginning of array.

func (*SortedArray) PopRand Uses

func (a *SortedArray) PopRand() (value interface{}, found bool)

PopRand randomly pops and return an item out of array. Note that if the array is empty, the <found> is false.

func (*SortedArray) PopRands Uses

func (a *SortedArray) PopRands(size int) []interface{}

PopRands randomly pops and returns <size> items out of array.

func (*SortedArray) PopRight Uses

func (a *SortedArray) PopRight() (value interface{}, found bool)

PopRight pops and returns an item from the end of array. Note that if the array is empty, the <found> is false.

func (*SortedArray) PopRights Uses

func (a *SortedArray) PopRights(size int) []interface{}

PopRights pops and returns <size> items from the end of array.

func (*SortedArray) RLockFunc Uses

func (a *SortedArray) RLockFunc(f func(array []interface{})) *SortedArray

RLockFunc locks reading by callback function <f>.

func (*SortedArray) Rand Uses

func (a *SortedArray) Rand() (value interface{}, found bool)

Rand randomly returns one item from array(no deleting).

func (*SortedArray) Rands Uses

func (a *SortedArray) Rands(size int) []interface{}

Rands randomly returns <size> items from array(no deleting).

func (*SortedArray) Range Uses

func (a *SortedArray) Range(start int, end ...int) []interface{}

Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.

If <end> is negative, then the offset will start from the end of array. If <end> is omitted, then the sequence will have everything from start up until the end of the array.

func (*SortedArray) Remove Uses

func (a *SortedArray) Remove(index int) (value interface{}, found bool)

Remove removes an item by index. If the given <index> is out of range of the array, the <found> is false.

func (*SortedArray) RemoveValue Uses

func (a *SortedArray) RemoveValue(value interface{}) bool

RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.

func (*SortedArray) Search Uses

func (a *SortedArray) Search(value interface{}) (index int)

Search searches array by <value>, returns the index of <value>, or returns -1 if not exists.

func (*SortedArray) SetArray Uses

func (a *SortedArray) SetArray(array []interface{}) *SortedArray

SetArray sets the underlying slice array with the given <array>.

func (*SortedArray) SetComparator Uses

func (a *SortedArray) SetComparator(comparator func(a, b interface{}) int)

SetComparator sets/changes the comparator for sorting. It resorts the array as the comparator is changed.

func (*SortedArray) SetUnique Uses

func (a *SortedArray) SetUnique(unique bool) *SortedArray

SetUnique sets unique mark to the array, which means it does not contain any repeated items. It also do unique check, remove all repeated items.

func (*SortedArray) Slice Uses

func (a *SortedArray) Slice() []interface{}

Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

func (*SortedArray) Sort Uses

func (a *SortedArray) Sort() *SortedArray

Sort sorts the array in increasing order. The parameter <reverse> controls whether sort in increasing order(default) or decreasing order

func (*SortedArray) String Uses

func (a *SortedArray) String() string

String returns current array as a string, which implements like json.Marshal does.

func (*SortedArray) SubSlice Uses

func (a *SortedArray) SubSlice(offset int, length ...int) []interface{}

SubSlice returns a slice of elements from the array as specified by the <offset> and <size> parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.

If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.

If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.

Any possibility crossing the left border of array, it will fail.

func (*SortedArray) Sum Uses

func (a *SortedArray) Sum() (sum int)

Sum returns the sum of values in an array.

func (*SortedArray) Unique Uses

func (a *SortedArray) Unique() *SortedArray

Unique uniques the array, clear repeated items.

func (*SortedArray) UnmarshalJSON Uses

func (a *SortedArray) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. Note that the comparator is set as string comparator in default.

func (*SortedArray) UnmarshalValue Uses

func (a *SortedArray) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for array. Note that the comparator is set as string comparator in default.

func (*SortedArray) Walk Uses

func (a *SortedArray) Walk(f func(value interface{}) interface{}) *SortedArray

Walk applies a user supplied function <f> to every item of array.

type SortedIntArray Uses

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

SortedIntArray is a golang sorted int array with rich features. It is using increasing order in default, which can be changed by setting it a custom comparator. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.

func NewSortedIntArray Uses

func NewSortedIntArray(safe ...bool) *SortedIntArray

NewSortedIntArray creates and returns an empty sorted array. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewSortedIntArrayComparator Uses

func NewSortedIntArrayComparator(comparator func(a, b int) int, safe ...bool) *SortedIntArray

NewSortedIntArrayComparator creates and returns an empty sorted array with specified comparator. The parameter <safe> is used to specify whether using array in concurrent-safety which is false in default.

func NewSortedIntArrayFrom Uses

func NewSortedIntArrayFrom(array []int, safe ...bool) *SortedIntArray

NewIntArrayFrom creates and returns an sorted array with given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewSortedIntArrayFromCopy Uses

func NewSortedIntArrayFromCopy(array []int, safe ...bool) *SortedIntArray

NewSortedIntArrayFromCopy creates and returns an sorted array from a copy of given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewSortedIntArrayRange Uses

func NewSortedIntArrayRange(start, end, step int, safe ...bool) *SortedIntArray

NewSortedIntArrayRange creates and returns a array by a range from <start> to <end> with step value <step>.

func NewSortedIntArraySize Uses

func NewSortedIntArraySize(cap int, safe ...bool) *SortedIntArray

NewSortedIntArraySize create and returns an sorted array with given size and cap. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func (*SortedIntArray) Add Uses

func (a *SortedIntArray) Add(values ...int) *SortedIntArray

Add adds one or multiple values to sorted array, the array always keeps sorted. It's alias of function Append, see Append.

func (*SortedIntArray) Append Uses

func (a *SortedIntArray) Append(values ...int) *SortedIntArray

Append adds one or multiple values to sorted array, the array always keeps sorted.

func (*SortedIntArray) Chunk Uses

func (a *SortedIntArray) Chunk(size int) [][]int

Chunk splits an array into multiple arrays, the size of each array is determined by <size>. The last chunk may contain less than size elements.

func (*SortedIntArray) Clear Uses

func (a *SortedIntArray) Clear() *SortedIntArray

Clear deletes all items of current array.

func (*SortedIntArray) Clone Uses

func (a *SortedIntArray) Clone() (newArray *SortedIntArray)

Clone returns a new array, which is a copy of current array.

func (*SortedIntArray) Contains Uses

func (a *SortedIntArray) Contains(value int) bool

Contains checks whether a value exists in the array.

func (*SortedIntArray) CountValues Uses

func (a *SortedIntArray) CountValues() map[int]int

CountValues counts the number of occurrences of all values in the array.

func (*SortedIntArray) FilterEmpty Uses

func (a *SortedIntArray) FilterEmpty() *SortedIntArray

FilterEmpty removes all zero value of the array.

func (*SortedIntArray) Get Uses

func (a *SortedIntArray) Get(index int) (value int, found bool)

Get returns the value by the specified index. If the given <index> is out of range of the array, the <found> is false.

func (*SortedIntArray) Interfaces Uses

func (a *SortedIntArray) Interfaces() []interface{}

Interfaces returns current array as []interface{}.

func (*SortedIntArray) IsEmpty Uses

func (a *SortedIntArray) IsEmpty() bool

IsEmpty checks whether the array is empty.

func (*SortedIntArray) Iterator Uses

func (a *SortedIntArray) Iterator(f func(k int, v int) bool)

Iterator is alias of IteratorAsc.

func (*SortedIntArray) IteratorAsc Uses

func (a *SortedIntArray) IteratorAsc(f func(k int, v int) bool)

IteratorAsc iterates the array readonly in ascending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*SortedIntArray) IteratorDesc Uses

func (a *SortedIntArray) IteratorDesc(f func(k int, v int) bool)

IteratorDesc iterates the array readonly in descending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*SortedIntArray) Join Uses

func (a *SortedIntArray) Join(glue string) string

Join joins array elements with a string <glue>.

func (*SortedIntArray) Len Uses

func (a *SortedIntArray) Len() int

Len returns the length of array.

func (*SortedIntArray) LockFunc Uses

func (a *SortedIntArray) LockFunc(f func(array []int)) *SortedIntArray

LockFunc locks writing by callback function <f>.

func (SortedIntArray) MarshalJSON Uses

func (a SortedIntArray) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.

func (*SortedIntArray) Merge Uses

func (a *SortedIntArray) Merge(array interface{}) *SortedIntArray

Merge merges <array> into current array. The parameter <array> can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.

func (*SortedIntArray) PopLeft Uses

func (a *SortedIntArray) PopLeft() (value int, found bool)

PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the <found> is false.

func (*SortedIntArray) PopLefts Uses

func (a *SortedIntArray) PopLefts(size int) []int

PopLefts pops and returns <size> items from the beginning of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*SortedIntArray) PopRand Uses

func (a *SortedIntArray) PopRand() (value int, found bool)

PopRand randomly pops and return an item out of array. Note that if the array is empty, the <found> is false.

func (*SortedIntArray) PopRands Uses

func (a *SortedIntArray) PopRands(size int) []int

PopRands randomly pops and returns <size> items out of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*SortedIntArray) PopRight Uses

func (a *SortedIntArray) PopRight() (value int, found bool)

PopRight pops and returns an item from the end of array. Note that if the array is empty, the <found> is false.

func (*SortedIntArray) PopRights Uses

func (a *SortedIntArray) PopRights(size int) []int

PopRights pops and returns <size> items from the end of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*SortedIntArray) RLockFunc Uses

func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray

RLockFunc locks reading by callback function <f>.

func (*SortedIntArray) Rand Uses

func (a *SortedIntArray) Rand() (value int, found bool)

Rand randomly returns one item from array(no deleting).

func (*SortedIntArray) Rands Uses

func (a *SortedIntArray) Rands(size int) []int

Rands randomly returns <size> items from array(no deleting).

func (*SortedIntArray) Range Uses

func (a *SortedIntArray) Range(start int, end ...int) []int

Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.

If <end> is negative, then the offset will start from the end of array. If <end> is omitted, then the sequence will have everything from start up until the end of the array.

func (*SortedIntArray) Remove Uses

func (a *SortedIntArray) Remove(index int) (value int, found bool)

Remove removes an item by index. If the given <index> is out of range of the array, the <found> is false.

func (*SortedIntArray) RemoveValue Uses

func (a *SortedIntArray) RemoveValue(value int) bool

RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.

func (*SortedIntArray) Search Uses

func (a *SortedIntArray) Search(value int) (index int)

Search searches array by <value>, returns the index of <value>, or returns -1 if not exists.

func (*SortedIntArray) SetArray Uses

func (a *SortedIntArray) SetArray(array []int) *SortedIntArray

SetArray sets the underlying slice array with the given <array>.

func (*SortedIntArray) SetUnique Uses

func (a *SortedIntArray) SetUnique(unique bool) *SortedIntArray

SetUnique sets unique mark to the array, which means it does not contain any repeated items. It also do unique check, remove all repeated items.

func (*SortedIntArray) Slice Uses

func (a *SortedIntArray) Slice() []int

Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

func (*SortedIntArray) Sort Uses

func (a *SortedIntArray) Sort() *SortedIntArray

Sort sorts the array in increasing order. The parameter <reverse> controls whether sort in increasing order(default) or decreasing order.

func (*SortedIntArray) String Uses

func (a *SortedIntArray) String() string

String returns current array as a string, which implements like json.Marshal does.

func (*SortedIntArray) SubSlice Uses

func (a *SortedIntArray) SubSlice(offset int, length ...int) []int

SubSlice returns a slice of elements from the array as specified by the <offset> and <size> parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.

If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.

If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.

Any possibility crossing the left border of array, it will fail.

func (*SortedIntArray) Sum Uses

func (a *SortedIntArray) Sum() (sum int)

Sum returns the sum of values in an array.

func (*SortedIntArray) Unique Uses

func (a *SortedIntArray) Unique() *SortedIntArray

Unique uniques the array, clear repeated items.

func (*SortedIntArray) UnmarshalJSON Uses

func (a *SortedIntArray) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*SortedIntArray) UnmarshalValue Uses

func (a *SortedIntArray) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for array.

func (*SortedIntArray) Walk Uses

func (a *SortedIntArray) Walk(f func(value int) int) *SortedIntArray

Walk applies a user supplied function <f> to every item of array.

type SortedStrArray Uses

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

SortedStrArray is a golang sorted string array with rich features. It is using increasing order in default, which can be changed by setting it a custom comparator. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.

func NewSortedStrArray Uses

func NewSortedStrArray(safe ...bool) *SortedStrArray

NewSortedStrArray creates and returns an empty sorted array. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewSortedStrArrayComparator Uses

func NewSortedStrArrayComparator(comparator func(a, b string) int, safe ...bool) *SortedStrArray

NewSortedStrArrayComparator creates and returns an empty sorted array with specified comparator. The parameter <safe> is used to specify whether using array in concurrent-safety which is false in default.

func NewSortedStrArrayFrom Uses

func NewSortedStrArrayFrom(array []string, safe ...bool) *SortedStrArray

NewSortedStrArrayFrom creates and returns an sorted array with given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewSortedStrArrayFromCopy Uses

func NewSortedStrArrayFromCopy(array []string, safe ...bool) *SortedStrArray

NewSortedStrArrayFromCopy creates and returns an sorted array from a copy of given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewSortedStrArraySize Uses

func NewSortedStrArraySize(cap int, safe ...bool) *SortedStrArray

NewSortedStrArraySize create and returns an sorted array with given size and cap. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func (*SortedStrArray) Add Uses

func (a *SortedStrArray) Add(values ...string) *SortedStrArray

Add adds one or multiple values to sorted array, the array always keeps sorted. It's alias of function Append, see Append.

func (*SortedStrArray) Append Uses

func (a *SortedStrArray) Append(values ...string) *SortedStrArray

Append adds one or multiple values to sorted array, the array always keeps sorted.

func (*SortedStrArray) Chunk Uses

func (a *SortedStrArray) Chunk(size int) [][]string

Chunk splits an array into multiple arrays, the size of each array is determined by <size>. The last chunk may contain less than size elements.

func (*SortedStrArray) Clear Uses

func (a *SortedStrArray) Clear() *SortedStrArray

Clear deletes all items of current array.

func (*SortedStrArray) Clone Uses

func (a *SortedStrArray) Clone() (newArray *SortedStrArray)

Clone returns a new array, which is a copy of current array.

func (*SortedStrArray) Contains Uses

func (a *SortedStrArray) Contains(value string) bool

Contains checks whether a value exists in the array.

func (*SortedStrArray) ContainsI Uses

func (a *SortedStrArray) ContainsI(value string) bool

ContainsI checks whether a value exists in the array with case-insensitively. Note that it internally iterates the whole array to do the comparison with case-insensitively.

func (*SortedStrArray) CountValues Uses

func (a *SortedStrArray) CountValues() map[string]int

CountValues counts the number of occurrences of all values in the array.

func (*SortedStrArray) FilterEmpty Uses

func (a *SortedStrArray) FilterEmpty() *SortedStrArray

FilterEmpty removes all empty string value of the array.

func (*SortedStrArray) Get Uses

func (a *SortedStrArray) Get(index int) (value string, found bool)

Get returns the value by the specified index. If the given <index> is out of range of the array, the <found> is false.

func (*SortedStrArray) Interfaces Uses

func (a *SortedStrArray) Interfaces() []interface{}

Interfaces returns current array as []interface{}.

func (*SortedStrArray) IsEmpty Uses

func (a *SortedStrArray) IsEmpty() bool

IsEmpty checks whether the array is empty.

func (*SortedStrArray) Iterator Uses

func (a *SortedStrArray) Iterator(f func(k int, v string) bool)

Iterator is alias of IteratorAsc.

func (*SortedStrArray) IteratorAsc Uses

func (a *SortedStrArray) IteratorAsc(f func(k int, v string) bool)

IteratorAsc iterates the array readonly in ascending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*SortedStrArray) IteratorDesc Uses

func (a *SortedStrArray) IteratorDesc(f func(k int, v string) bool)

IteratorDesc iterates the array readonly in descending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*SortedStrArray) Join Uses

func (a *SortedStrArray) Join(glue string) string

Join joins array elements with a string <glue>.

func (*SortedStrArray) Len Uses

func (a *SortedStrArray) Len() int

Len returns the length of array.

func (*SortedStrArray) LockFunc Uses

func (a *SortedStrArray) LockFunc(f func(array []string)) *SortedStrArray

LockFunc locks writing by callback function <f>.

func (SortedStrArray) MarshalJSON Uses

func (a SortedStrArray) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.

func (*SortedStrArray) Merge Uses

func (a *SortedStrArray) Merge(array interface{}) *SortedStrArray

Merge merges <array> into current array. The parameter <array> can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.

func (*SortedStrArray) PopLeft Uses

func (a *SortedStrArray) PopLeft() (value string, found bool)

PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the <found> is false.

func (*SortedStrArray) PopLefts Uses

func (a *SortedStrArray) PopLefts(size int) []string

PopLefts pops and returns <size> items from the beginning of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*SortedStrArray) PopRand Uses

func (a *SortedStrArray) PopRand() (value string, found bool)

PopRand randomly pops and return an item out of array. Note that if the array is empty, the <found> is false.

func (*SortedStrArray) PopRands Uses

func (a *SortedStrArray) PopRands(size int) []string

PopRands randomly pops and returns <size> items out of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*SortedStrArray) PopRight Uses

func (a *SortedStrArray) PopRight() (value string, found bool)

PopRight pops and returns an item from the end of array. Note that if the array is empty, the <found> is false.

func (*SortedStrArray) PopRights Uses

func (a *SortedStrArray) PopRights(size int) []string

PopRights pops and returns <size> items from the end of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*SortedStrArray) RLockFunc Uses

func (a *SortedStrArray) RLockFunc(f func(array []string)) *SortedStrArray

RLockFunc locks reading by callback function <f>.

func (*SortedStrArray) Rand Uses

func (a *SortedStrArray) Rand() (value string, found bool)

Rand randomly returns one item from array(no deleting).

func (*SortedStrArray) Rands Uses

func (a *SortedStrArray) Rands(size int) []string

Rands randomly returns <size> items from array(no deleting).

func (*SortedStrArray) Range Uses

func (a *SortedStrArray) Range(start int, end ...int) []string

Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.

If <end> is negative, then the offset will start from the end of array. If <end> is omitted, then the sequence will have everything from start up until the end of the array.

func (*SortedStrArray) Remove Uses

func (a *SortedStrArray) Remove(index int) (value string, found bool)

Remove removes an item by index. If the given <index> is out of range of the array, the <found> is false.

func (*SortedStrArray) RemoveValue Uses

func (a *SortedStrArray) RemoveValue(value string) bool

RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.

func (*SortedStrArray) Search Uses

func (a *SortedStrArray) Search(value string) (index int)

Search searches array by <value>, returns the index of <value>, or returns -1 if not exists.

func (*SortedStrArray) SetArray Uses

func (a *SortedStrArray) SetArray(array []string) *SortedStrArray

SetArray sets the underlying slice array with the given <array>.

func (*SortedStrArray) SetUnique Uses

func (a *SortedStrArray) SetUnique(unique bool) *SortedStrArray

SetUnique sets unique mark to the array, which means it does not contain any repeated items. It also do unique check, remove all repeated items.

func (*SortedStrArray) Slice Uses

func (a *SortedStrArray) Slice() []string

Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

func (*SortedStrArray) Sort Uses

func (a *SortedStrArray) Sort() *SortedStrArray

Sort sorts the array in increasing order. The parameter <reverse> controls whether sort in increasing order(default) or decreasing order.

func (*SortedStrArray) String Uses

func (a *SortedStrArray) String() string

String returns current array as a string, which implements like json.Marshal does.

func (*SortedStrArray) SubSlice Uses

func (a *SortedStrArray) SubSlice(offset int, length ...int) []string

SubSlice returns a slice of elements from the array as specified by the <offset> and <size> parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.

If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.

If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.

Any possibility crossing the left border of array, it will fail.

func (*SortedStrArray) Sum Uses

func (a *SortedStrArray) Sum() (sum int)

Sum returns the sum of values in an array.

func (*SortedStrArray) Unique Uses

func (a *SortedStrArray) Unique() *SortedStrArray

Unique uniques the array, clear repeated items.

func (*SortedStrArray) UnmarshalJSON Uses

func (a *SortedStrArray) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*SortedStrArray) UnmarshalValue Uses

func (a *SortedStrArray) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for array.

func (*SortedStrArray) Walk Uses

func (a *SortedStrArray) Walk(f func(value string) string) *SortedStrArray

Walk applies a user supplied function <f> to every item of array.

type StrArray Uses

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

StrArray is a golang string array with rich features. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.

func NewStrArray Uses

func NewStrArray(safe ...bool) *StrArray

NewStrArray creates and returns an empty array. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewStrArrayFrom Uses

func NewStrArrayFrom(array []string, safe ...bool) *StrArray

NewStrArrayFrom creates and returns an array with given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewStrArrayFromCopy Uses

func NewStrArrayFromCopy(array []string, safe ...bool) *StrArray

NewStrArrayFromCopy creates and returns an array from a copy of given slice <array>. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func NewStrArraySize Uses

func NewStrArraySize(size int, cap int, safe ...bool) *StrArray

NewStrArraySize create and returns an array with given size and cap. The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.

func (*StrArray) Append Uses

func (a *StrArray) Append(value ...string) *StrArray

See PushRight.

func (*StrArray) Chunk Uses

func (a *StrArray) Chunk(size int) [][]string

Chunk splits an array into multiple arrays, the size of each array is determined by <size>. The last chunk may contain less than size elements.

func (*StrArray) Clear Uses

func (a *StrArray) Clear() *StrArray

Clear deletes all items of current array.

func (*StrArray) Clone Uses

func (a *StrArray) Clone() (newArray *StrArray)

Clone returns a new array, which is a copy of current array.

func (*StrArray) Contains Uses

func (a *StrArray) Contains(value string) bool

Contains checks whether a value exists in the array.

func (*StrArray) ContainsI Uses

func (a *StrArray) ContainsI(value string) bool

ContainsI checks whether a value exists in the array with case-insensitively. Note that it internally iterates the whole array to do the comparison with case-insensitively.

func (*StrArray) CountValues Uses

func (a *StrArray) CountValues() map[string]int

CountValues counts the number of occurrences of all values in the array.

func (*StrArray) Fill Uses

func (a *StrArray) Fill(startIndex int, num int, value string) error

Fill fills an array with num entries of the value <value>, keys starting at the <startIndex> parameter.

func (*StrArray) FilterEmpty Uses

func (a *StrArray) FilterEmpty() *StrArray

FilterEmpty removes all empty string value of the array.

func (*StrArray) Get Uses

func (a *StrArray) Get(index int) (value string, found bool)

Get returns the value by the specified index. If the given <index> is out of range of the array, the <found> is false.

func (*StrArray) InsertAfter Uses

func (a *StrArray) InsertAfter(index int, value string) error

InsertAfter inserts the <value> to the back of <index>.

func (*StrArray) InsertBefore Uses

func (a *StrArray) InsertBefore(index int, value string) error

InsertBefore inserts the <value> to the front of <index>.

func (*StrArray) Interfaces Uses

func (a *StrArray) Interfaces() []interface{}

Interfaces returns current array as []interface{}.

func (*StrArray) IsEmpty Uses

func (a *StrArray) IsEmpty() bool

IsEmpty checks whether the array is empty.

func (*StrArray) Iterator Uses

func (a *StrArray) Iterator(f func(k int, v string) bool)

Iterator is alias of IteratorAsc.

func (*StrArray) IteratorAsc Uses

func (a *StrArray) IteratorAsc(f func(k int, v string) bool)

IteratorAsc iterates the array readonly in ascending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*StrArray) IteratorDesc Uses

func (a *StrArray) IteratorDesc(f func(k int, v string) bool)

IteratorDesc iterates the array readonly in descending order with given callback function <f>. If <f> returns true, then it continues iterating; or false to stop.

func (*StrArray) Join Uses

func (a *StrArray) Join(glue string) string

Join joins array elements with a string <glue>.

func (*StrArray) Len Uses

func (a *StrArray) Len() int

Len returns the length of array.

func (*StrArray) LockFunc Uses

func (a *StrArray) LockFunc(f func(array []string)) *StrArray

LockFunc locks writing by callback function <f>.

func (StrArray) MarshalJSON Uses

func (a StrArray) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.

func (*StrArray) Merge Uses

func (a *StrArray) Merge(array interface{}) *StrArray

Merge merges <array> into current array. The parameter <array> can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.

func (*StrArray) Pad Uses

func (a *StrArray) Pad(size int, value string) *StrArray

Pad pads array to the specified length with <value>. If size is positive then the array is padded on the right, or negative on the left. If the absolute value of <size> is less than or equal to the length of the array then no padding takes place.

func (*StrArray) PopLeft Uses

func (a *StrArray) PopLeft() (value string, found bool)

PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the <found> is false.

func (*StrArray) PopLefts Uses

func (a *StrArray) PopLefts(size int) []string

PopLefts pops and returns <size> items from the beginning of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*StrArray) PopRand Uses

func (a *StrArray) PopRand() (value string, found bool)

PopRand randomly pops and return an item out of array. Note that if the array is empty, the <found> is false.

func (*StrArray) PopRands Uses

func (a *StrArray) PopRands(size int) []string

PopRands randomly pops and returns <size> items out of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*StrArray) PopRight Uses

func (a *StrArray) PopRight() (value string, found bool)

PopRight pops and returns an item from the end of array. Note that if the array is empty, the <found> is false.

func (*StrArray) PopRights Uses

func (a *StrArray) PopRights(size int) []string

PopRights pops and returns <size> items from the end of array. If the given <size> is greater than size of the array, it returns all elements of the array. Note that if given <size> <= 0 or the array is empty, it returns nil.

func (*StrArray) PushLeft Uses

func (a *StrArray) PushLeft(value ...string) *StrArray

PushLeft pushes one or multiple items to the beginning of array.

func (*StrArray) PushRight Uses

func (a *StrArray) PushRight(value ...string) *StrArray

PushRight pushes one or multiple items to the end of array. It equals to Append.

func (*StrArray) RLockFunc Uses

func (a *StrArray) RLockFunc(f func(array []string)) *StrArray

RLockFunc locks reading by callback function <f>.

func (*StrArray) Rand Uses

func (a *StrArray) Rand() (value string, found bool)

Rand randomly returns one item from array(no deleting).

func (*StrArray) Rands Uses

func (a *StrArray) Rands(size int) []string

Rands randomly returns <size> items from array(no deleting).

func (*StrArray) Range Uses

func (a *StrArray) Range(start int, end ...int) []string

Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.

If <end> is negative, then the offset will start from the end of array. If <end> is omitted, then the sequence will have everything from start up until the end of the array.

func (*StrArray) Remove Uses

func (a *StrArray) Remove(index int) (value string, found bool)

Remove removes an item by index. If the given <index> is out of range of the array, the <found> is false.

func (*StrArray) RemoveValue Uses

func (a *StrArray) RemoveValue(value string) bool

RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.

func (*StrArray) Replace Uses

func (a *StrArray) Replace(array []string) *StrArray

Replace replaces the array items by given <array> from the beginning of array.

func (*StrArray) Reverse Uses

func (a *StrArray) Reverse() *StrArray

Reverse makes array with elements in reverse order.

func (*StrArray) Search Uses

func (a *StrArray) Search(value string) int

Search searches array by <value>, returns the index of <value>, or returns -1 if not exists.

func (*StrArray) Set Uses

func (a *StrArray) Set(index int, value string) error

Set sets value to specified index.

func (*StrArray) SetArray Uses

func (a *StrArray) SetArray(array []string) *StrArray

SetArray sets the underlying slice array with the given <array>.

func (*StrArray) Shuffle Uses

func (a *StrArray) Shuffle() *StrArray

Shuffle randomly shuffles the array.

func (*StrArray) Slice Uses

func (a *StrArray) Slice() []string

Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

func (*StrArray) Sort Uses

func (a *StrArray) Sort(reverse ...bool) *StrArray

Sort sorts the array in increasing order. The parameter <reverse> controls whether sort in increasing order(default) or decreasing order

func (*StrArray) SortFunc Uses

func (a *StrArray) SortFunc(less func(v1, v2 string) bool) *StrArray

SortFunc sorts the array by custom function <less>.

func (*StrArray) String Uses

func (a *StrArray) String() string

String returns current array as a string, which implements like json.Marshal does.

func (*StrArray) SubSlice Uses

func (a *StrArray) SubSlice(offset int, length ...int) []string

SubSlice returns a slice of elements from the array as specified by the <offset> and <size> parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.

If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.

If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.

Any possibility crossing the left border of array, it will fail.

func (*StrArray) Sum Uses

func (a *StrArray) Sum() (sum int)

Sum returns the sum of values in an array.

func (*StrArray) Unique Uses

func (a *StrArray) Unique() *StrArray

Unique uniques the array, clear repeated items. Example: [1,1,2,3,2] -> [1,2,3]

func (*StrArray) UnmarshalJSON Uses

func (a *StrArray) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*StrArray) UnmarshalValue Uses

func (a *StrArray) UnmarshalValue(value interface{}) error

UnmarshalValue is an interface implement which sets any type of value for array.

func (*StrArray) Walk Uses

func (a *StrArray) Walk(f func(value string) string) *StrArray

Walk applies a user supplied function <f> to every item of array.

Code:

var array garray.StrArray
tables := g.SliceStr{"user", "user_detail"}
prefix := "gf_"
array.Append(tables...)
// Add prefix for given table names.
array.Walk(func(value string) string {
    return prefix + value
})
fmt.Println(array.Slice())

Output:

[gf_user gf_user_detail]

Package garray imports 13 packages (graph) and is imported by 9 packages. Updated 2020-06-18. Refresh now. Tools for package owners.