Skip to content

Commit

Permalink
Add Fill & FillPattern, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng committed Sep 9, 2022
1 parent f71dd55 commit c30740d
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 55 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ indicating that a custom comparison function can be passed.
- `Range` returns a Slice of contains integers in the range of `[begin, end)`
- `Generate` generates a sequence with the given function to fill the Slice

#### Data manipulation

- `Copy` return a copies of specified slice
- `Fill` repeatedly fills a slice with the specified value
- `FillPattern` repeatedly fills a slice with the specified pattern
- `Transform` passes the value at each position of the slice to the specified function and sets it back with its return value
- `TransformTo` passes the value at each position of slice `a` to the specified function,
sets its return value to the corresponding position in slice `b`, and returns a slice of corresponding length of slice `b`
- `TransformCopy` passes the value at each position of the slice to the specified function,
sets its return value to the corresponding position in a new slice and returns
- `Unique` removes adjacent duplicate elements from a slice and returns a slice with new length containing the remaining elements,
`UniqueCopy` returns a copy without modifying the original slice
- `Remove` removes all elements in the slice equal to the specified value, `RemoveCopy` returns a copy without modifying the original slice
- `RemoveIf` removes all elements in the slice that are equivalent to making the specified function return `true`,
`RemoveIfCopy` does not modify the original slice but returns a copy
- `Shuffle` random shuffle elements in the slice
- `Reverse` reverses a slice, `ReverseCopy` returns a copy without modifying the original slice

#### Compute

- `Sum` Sum
Expand All @@ -199,9 +217,9 @@ indicating that a custom comparison function can be passed.

See C++ STL.

- BinarySearch
- LowerBound
- UpperBound
- `BinarySearch`
- `LowerBound`
- `UpperBound`

#### Sort

Expand Down
64 changes: 39 additions & 25 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,44 +155,58 @@ func TestSkipList_ForEachMutable(t *testing.T) {
- Range 返回一个 [begin, end) 的整数构成的 Slice
- Generate 用给定的函数生成一个序列填充到 Slice

#### 数据操作

- `Copy` 返回切片的拷贝
- `Fill` 用指定的值重复填充一个切片
- `FillPattern` 用指定的模式重复填充一个切片
- `Transform` 把切片的每个位置的值传给指定的函数,用其返回值设置回去
- `TransformTo` 把切片 a 的每个位置的值传给指定的函数,将其返回值设置到切片 b 中相应的位置,并返回 b 的相应长度的切片
- `TransformCopy` 把切片的每个位置的值传给指定的函数,将其返回值设置到一个新的切片中相应的位置并返回
- `Unique` 去除切片中相邻的重复元素,返回包含剩余元素的新长度的切片,`UniqueCopy` 则不修改原切片而是返回一个拷贝
- `Remove` 去除切片中等于指定值的所有元素,`RemoveCopy` 则不修改原切片而是返回一个拷贝
- `RemoveIf` 去除切片中等于让指定函数返回 `true` 的所有元素,`RemoveIfCopy` 则不修改原切片而是返回一个拷贝
- `Shuffle` 随机洗牌
- `Reverse` 反转一个切片,`ReverseCopy` 则不修改原切片而是返回一个拷贝

#### 计算型

- Sum 求和
- SumAs 求和并以另一种类型的结果返回(比如 `int64` 返回 `[]int32` 的和)
- Average 求平均值。
- AverageAs 求平均值并以另一种类型的结果返回(比如 `float64` 返回 `[]int` 的和)
- Count 返回和指定值相当的个数
- CountIf 返回让指定函数返回 `true` 的元素的个数
- `Sum` 求和
- `SumAs` 求和并以另一种类型的结果返回(比如以 `int64` 类型返回 `[]int32` 的和)
- `Average` 求平均值。
- `AverageAs` 求平均值并以另一种类型的结果返回(比如 `float64` 返回 `[]int` 的和)
- `Count` 返回和指定值相当的个数
- `CountIf` 返回让指定函数返回 `true` 的元素的个数

#### 比较

- Equal 判断两个序列是否相等
- Compare 比较两个序列,按字典序返回 -101 分别表示起大小关系
- `Equal` 判断两个序列是否相等
- `Compare` 比较两个序列,按字典序返回 -101 分别表示起大小关系

#### 查找

- Min, Max 求最大最小值
- MinN、MaxN、MinMax 返回 slice 中的最大和最小值
- Find 线性查找第一个指定的值,返回其下标
- FindIf 线性查找指定函数返回 `true` 的值,返回其下标
- AllOf、AnyOf、NoneOf 返回区间中是否全部、任何一个、没有一个元素能使传入的函数返回 `true`

#### 排序

- Sort 排序
- DescSort 降序排序
- StableSort 稳定排序
- DescStableSort 降序稳定排序
- IsSorted 是否是排序的
- IsDescSorted 是否是降序排序的
- `Min`, `Max` 求最大最小值
- `MinN``MaxN``MinMax` 返回 slice 中的最大和最小值
- `Find` 线性查找第一个指定的值,返回其下标
- `FindIf` 线性查找指定函数返回 `true` 的值,返回其下标
- `AllOf``AnyOf``NoneOf` 返回区间中是否全部、任何一个、没有一个元素能使传入的函数返回 `true`

#### 二分查找

参考 C++STL。

- BinarySearch
- LowerBound
- UpperBound
- `BinarySearch`
- `LowerBound`
- `UpperBound`

#### 排序

- `Sort` 升序排序
- `DescSort` 降序排序
- `StableSort` 升序稳定排序
- `DescStableSort` 降序稳定排序
- `IsSorted` 是否是升序排序的
- `IsDescSorted` 是否是降序排序的

#### 堆

Expand Down
58 changes: 40 additions & 18 deletions generated_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Package stl4go is a generic container and algorithm library for go.
- [func DescSort[T Ordered](a []T)](<#func-descsort>)
- [func DescStableSort[T Ordered](a []T)](<#func-descstablesort>)
- [func Equal[T comparable](a, b []T) bool](<#func-equal>)
- [func Fill[T any](a []T, v T)](<#func-fill>)
- [func FillPattern[T any](a []T, pattern []T)](<#func-fillpattern>)
- [func Find[T comparable](a []T, x T) (index int, ok bool)](<#func-find>)
- [func FindIf[T any](a []T, cond func(T) bool) (index int, ok bool)](<#func-findif>)
- [func Generate[T any](a []T, gen func() T)](<#func-generate>)
Expand Down Expand Up @@ -70,7 +72,7 @@ Package stl4go is a generic container and algorithm library for go.
- [func SumAs[R, T Numeric](a []T) R](<#func-sumas>)
- [func Transform[T any](a []T, op func(T) T)](<#func-transform>)
- [func TransformCopy[R any, T any](a []T, op func(T) R) []R](<#func-transformcopy>)
- [func TransformTo[R any, T any](a []T, op func(T) R, b []R)](<#func-transformto>)
- [func TransformTo[R any, T any](a []T, op func(T) R, b []R) []R](<#func-transformto>)
- [func Unique[T comparable](a []T) []T](<#func-unique>)
- [func UniqueCopy[T comparable](a []T) []T](<#func-uniquecopy>)
- [func UpperBound[T Ordered](a []T, value T) int](<#func-upperbound>)
Expand Down Expand Up @@ -359,6 +361,26 @@ Equal returns whether two slices are equal. Return true if they are the same len

Complexity: O\(min\(len\(a\), len\(b\)\)\).

## func [Fill](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L16>)

```go
func Fill[T any](a []T, v T)
```

Fill fills each element in slice a with new value v.

Complexity: O\(len\(a\)\).

## func [FillPattern](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L34>)

```go
func FillPattern[T any](a []T, pattern []T)
```

FillPattern fills elements in slice a with specified pattern.

Complexity: O\(len\(a\)\).

## func [Find](<https://github.com/chen3feng/stl4go/blob/master/lookup.go#L90>)

```go
Expand Down Expand Up @@ -627,23 +649,23 @@ Range make a \[\]T filled with values in the \`\[first, last\)\` sequence. NOTE:

Complexity: O\(last\-first\).

## func [Remove](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L92>)
## func [Remove](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L126>)

```go
func Remove[T comparable](a []T, x T) []T
```

Remove remove the elements which equals to x from the input slice. return the processed slice, and the content of the input slice is also changed.
Remove remove the elements which equals to x from the input slice. return the processed slice with new length.

Complexity: O\(len\(a\)\).

## func [RemoveCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L107>)
## func [RemoveCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L141>)

```go
func RemoveCopy[T comparable](a []T, x T) []T
```

RemoveCopy remove all elements which equals to x from the input slice. return the processed slice, and the content of the input slice is also changed.
RemoveCopy remove all elements which equals to x from the input slice. return a new slice with processed results. The input slice is kept unchanged.

Complexity: O\(len\(a\)\).

Expand All @@ -657,17 +679,17 @@ RemoveHeapFunc removes and returns the element at index i from the heap.

Complexity: is O\(log\(n\)\) where n = len\(\*heap\).

## func [RemoveIf](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L121>)
## func [RemoveIf](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L155>)

```go
func RemoveIf[T any](a []T, cond func(T) bool) []T
```

RemoveIf remove each element which make cond\(x\) returns true from the input slice, copy other elements to a new slice and return it. The input slice is kept unchanged.
RemoveIf remove each element which make cond\(x\) returns true from the input slice, copy other elements to a new slice and return it.

Complexity: O\(len\(a\)\).

## func [RemoveIfCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L136>)
## func [RemoveIfCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L170>)

```go
func RemoveIfCopy[T any](a []T, cond func(T) bool) []T
Expand All @@ -687,7 +709,7 @@ RemoveMinHeap removes and returns the element at index i from the min heap.

Complexity: is O\(log\(n\)\) where n = len\(\*heap\).

## func [Reverse](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L158>)
## func [Reverse](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L192>)

```go
func Reverse[T any](a []T)
Expand All @@ -697,7 +719,7 @@ Reverse reverses the order of the elements in the slice a.

Complexity: O\(len\(a\)\).

## func [ReverseCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L167>)
## func [ReverseCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L201>)

```go
func ReverseCopy[T any](a []T) []T
Expand All @@ -707,7 +729,7 @@ ReverseCopy returns a reversed copy of slice a.

Complexity: O\(len\(a\)\).

## func [Shuffle](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L149>)
## func [Shuffle](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L183>)

```go
func Shuffle[T any](a []T)
Expand Down Expand Up @@ -773,7 +795,7 @@ func SumAs[R, T Numeric](a []T) R

SumAs summarize all elements in a. returns the result as type R, this is useful when T is too small to hold the result. Complexity: O\(len\(a\)\).

## func [Transform](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L30>)
## func [Transform](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L65>)

```go
func Transform[T any](a []T, op func(T) T)
Expand All @@ -783,7 +805,7 @@ Transform applies the function op to each element in slice a and set it back to

Complexity: O\(len\(a\)\).

## func [TransformCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L40>)
## func [TransformCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L74>)

```go
func TransformCopy[R any, T any](a []T, op func(T) R) []R
Expand All @@ -793,10 +815,10 @@ TransformCopy applies the function op to each element in slice a and return all

Complexity: O\(len\(a\)\).

## func [TransformTo](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L18>)
## func [TransformTo](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L52>)

```go
func TransformTo[R any, T any](a []T, op func(T) R, b []R)
func TransformTo[R any, T any](a []T, op func(T) R, b []R) []R
```

TransformTo applies the function op to each element in slice a and fill it to slice b.
Expand All @@ -805,17 +827,17 @@ The len\(b\) must not lesser than len\(a\).

Complexity: O\(len\(a\)\).

## func [Unique](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L52>)
## func [Unique](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L86>)

```go
func Unique[T comparable](a []T) []T
```

Unique remove adjacent repeated elements from the input slice. return the processed slice, and the content of the input slice is also changed.
Unique remove adjacent repeated elements from the input slice. return the processed slice with new length.

Complexity: O\(len\(a\)\).

## func [UniqueCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L72>)
## func [UniqueCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L106>)

```go
func UniqueCopy[T comparable](a []T) []T
Expand Down
52 changes: 43 additions & 9 deletions transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,53 @@ func Copy[T any](a []T) []T {
return b
}

// Fill fills each element in slice a with new value v.
//
// Complexity: O(len(a)).
func Fill[T any](a []T, v T) {
if len(a) == 0 {
return
}
// Preload the first value into the array/slice.
a[0] = v

// Incrementally duplicate the value into the rest of the container.
// About 2~3 times faster than naive fill.
// https://gist.github.com/taylorza/df2f89d5f9ab3ffd06865062a4cf015d
for j := 1; j < len(a); j *= 2 {
copy(a[j:], a[:j])
}
}

// FillPattern fills elements in slice a with specified pattern.
//
// Complexity: O(len(a)).
func FillPattern[T any](a []T, pattern []T) {
if len(pattern) == 0 {
panic("pattern can't be empty")
}
// Copy the pattern into the start of the container
copy(a, pattern)

// Incrementally duplicate the pattern throughout the container
for j := len(pattern); j < len(a); j *= 2 {
copy(a[j:], a[:j])
}
}

// TransformTo applies the function op to each element in slice a and fill it to slice b.
//
// The len(b) must not lesser than len(a).
//
// Complexity: O(len(a)).
func TransformTo[R any, T any](a []T, op func(T) R, b []R) {
func TransformTo[R any, T any](a []T, op func(T) R, b []R) []R {
if len(b) < len(a) {
panic("TransformTo: len(b) < len(a)")
}
for i, v := range a {
b[i] = op(v)
}
return b[:len(a)]
}

// Transform applies the function op to each element in slice a and set it back to the same place in a.
Expand All @@ -36,17 +71,16 @@ func Transform[T any](a []T, op func(T) T) {
// TransformCopy applies the function op to each element in slice a and return all the result as a slice.
//
// Complexity: O(len(a)).
//
func TransformCopy[R any, T any](a []T, op func(T) R) []R {
r := make([]R, 0, len(a))
for _, v := range a {
r = append(r, op(v))
r := make([]R, len(a))
for i, v := range a {
r[i] = op(v)
}
return r
}

// Unique remove adjacent repeated elements from the input slice.
// return the processed slice, and the content of the input slice is also changed.
// return the processed slice with new length.
//
// Complexity: O(len(a)).
func Unique[T comparable](a []T) []T {
Expand Down Expand Up @@ -86,7 +120,7 @@ func UniqueCopy[T comparable](a []T) []T {
}

// Remove remove the elements which equals to x from the input slice.
// return the processed slice, and the content of the input slice is also changed.
// return the processed slice with new length.
//
// Complexity: O(len(a)).
func Remove[T comparable](a []T, x T) []T {
Expand All @@ -101,7 +135,7 @@ func Remove[T comparable](a []T, x T) []T {
}

// RemoveCopy remove all elements which equals to x from the input slice.
// return the processed slice, and the content of the input slice is also changed.
// return a new slice with processed results. The input slice is kept unchanged.
//
// Complexity: O(len(a)).
func RemoveCopy[T comparable](a []T, x T) []T {
Expand All @@ -115,7 +149,7 @@ func RemoveCopy[T comparable](a []T, x T) []T {
}

// RemoveIf remove each element which make cond(x) returns true from the input slice,
// copy other elements to a new slice and return it. The input slice is kept unchanged.
// copy other elements to a new slice and return it.
//
// Complexity: O(len(a)).
func RemoveIf[T any](a []T, cond func(T) bool) []T {
Expand Down
Loading

0 comments on commit c30740d

Please sign in to comment.