Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SList #93

Merged
merged 2 commits into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ Currently implemented containers are:
search and remove, as well as advanced functions such as union, intersection, difference, subset, superset, and disjoint.
- [x] `Vector` is a thin encapsulation based on `slice`. It provides functions such as insertion and deletion in the middle, range deletion, etc.,
and is still compatible with slices.
- [x] `DList` is a doubly linked list.
- [x] `DList` is a doubly linked list, supports push/popup at both ending.
- [x] `SList` is a singly linked list, supports push/popup at the head and push at the tail.
- [x] [SkipList](skiplist.md) is an ordered associative container that fills the gap where Go `map` only supports unordered.
This is currently the fastest skip list I tested in GitHub, see [skiplist-survey](https://github.com/chen3feng/skiplist-survey) for performance comparison
- [x] `Stack`, is a FILO container based on Slice implementation
Expand Down
3 changes: 2 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import "github.com/chen3feng/stl4go"

- [x] `Set` 集合。用 Go 自己的 map 封装了一个 `BuiltinSet`,提供了插入查找删除等基本操作,以及并集、交集、差集、子集、超集、不交集等高级功能。
- [x] `Vector` 是基于 slice 封装的向量。提供了中间插入删除、区间删除等功能,依然与 slice 兼容。
- [x] `DList` 是双链表
- [x] `DList` 是双链表容器,支持两端插入删除。
- [x] `SList` 是单链表容器,支持头部插入删除及尾部插入。
- [x] [跳表(SkipList)](skiplist.md) 是一种有序的关联容器,可以填补 Go `map` 只支持无序的的空白。这是目前全 GitHub 最快的跳表,参见 [skiplist-survey](https://github.com/chen3feng/skiplist-survey)的性能比较
- [x] `Stack`,栈基于 Slice 实现
- [x] `Queue` 双向进出的队列,基于链表实现
Expand Down
12 changes: 9 additions & 3 deletions dlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ func (l *DList[T]) TryPopFront() (T, bool) {
if l.length == 0 {
return val, false
}
val = l.head.next.value
l.head.next = l.head.next.next
node := l.head.next
val = node.value
l.head.next = node.next
l.head.prev = &l.head
node.prev = nil
node.next = nil
l.length--
return val, true
}
Expand All @@ -131,9 +134,12 @@ func (l *DList[T]) TryPopBack() (T, bool) {
if l.length == 0 {
return val, false
}
val = l.head.prev.value
node := l.head.prev
val = node.value
l.head.prev = l.head.prev.prev
l.head.prev.next = &l.head
node.prev = nil
node.next = nil
l.length--
return val, true
}
Expand Down
187 changes: 171 additions & 16 deletions generated_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ Package stl4go is a generic container and algorithm library for go.
- [func (q *Queue[T]) String() string](<#func-queuet-string>)
- [func (q *Queue[T]) TryPopBack() (T, bool)](<#func-queuet-trypopback>)
- [func (q *Queue[T]) TryPopFront() (T, bool)](<#func-queuet-trypopfront>)
- [type SList](<#type-slist>)
- [func SListOf[T any](values ...T) SList[T]](<#func-slistof>)
- [func (l *SList[T]) Back() T](<#func-slistt-back>)
- [func (l *SList[T]) Clear()](<#func-slistt-clear>)
- [func (l *SList[T]) ForEach(cb func(T))](<#func-slistt-foreach>)
- [func (l *SList[T]) ForEachIf(cb func(T) bool)](<#func-slistt-foreachif>)
- [func (l *SList[T]) ForEachMutable(cb func(*T))](<#func-slistt-foreachmutable>)
- [func (l *SList[T]) ForEachMutableIf(cb func(*T) bool)](<#func-slistt-foreachmutableif>)
- [func (l *SList[T]) Front() T](<#func-slistt-front>)
- [func (l *SList[T]) IsEmpty() bool](<#func-slistt-isempty>)
- [func (l *SList[T]) Iterate() MutableIterator[T]](<#func-slistt-iterate>)
- [func (l *SList[T]) Len() int](<#func-slistt-len>)
- [func (l *SList[T]) PopFront() T](<#func-slistt-popfront>)
- [func (l *SList[T]) PushBack(v T)](<#func-slistt-pushback>)
- [func (l *SList[T]) PushFront(v T)](<#func-slistt-pushfront>)
- [func (l *SList[T]) Reverse()](<#func-slistt-reverse>)
- [func (l *SList[T]) Values() []T](<#func-slistt-values>)
- [type Set](<#type-set>)
- [type Signed](<#type-signed>)
- [type SkipList](<#type-skiplist>)
Expand Down Expand Up @@ -1053,31 +1070,31 @@ func (l *DList[T]) Clear()

Clear cleanup the list

### func \(\*DList\[T\]\) [ForEach](<https://github.com/chen3feng/stl4go/blob/master/dlist.go#L142>)
### func \(\*DList\[T\]\) [ForEach](<https://github.com/chen3feng/stl4go/blob/master/dlist.go#L148>)

```go
func (l *DList[T]) ForEach(cb func(val T))
```

ForEach iterate the list, apply each element to the cb callback function.

### func \(\*DList\[T\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/dlist.go#L149>)
### func \(\*DList\[T\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/dlist.go#L155>)

```go
func (l *DList[T]) ForEachIf(cb func(val T) bool)
```

ForEachIf iterate the list, apply each element to the cb callback function, stop if cb returns false.

### func \(\*DList\[T\]\) [ForEachMutable](<https://github.com/chen3feng/stl4go/blob/master/dlist.go#L158>)
### func \(\*DList\[T\]\) [ForEachMutable](<https://github.com/chen3feng/stl4go/blob/master/dlist.go#L164>)

```go
func (l *DList[T]) ForEachMutable(cb func(val *T))
```

ForEachMutable iterate the list, apply pointer of each element to the cb callback function.

### func \(\*DList\[T\]\) [ForEachMutableIf](<https://github.com/chen3feng/stl4go/blob/master/dlist.go#L165>)
### func \(\*DList\[T\]\) [ForEachMutableIf](<https://github.com/chen3feng/stl4go/blob/master/dlist.go#L171>)

```go
func (l *DList[T]) ForEachMutableIf(cb func(val *T) bool)
Expand Down Expand Up @@ -1149,7 +1166,7 @@ func (l *DList[T]) String() string

String convert the list to string

### func \(\*DList\[T\]\) [TryPopBack](<https://github.com/chen3feng/stl4go/blob/master/dlist.go#L129>)
### func \(\*DList\[T\]\) [TryPopBack](<https://github.com/chen3feng/stl4go/blob/master/dlist.go#L132>)

```go
func (l *DList[T]) TryPopBack() (T, bool)
Expand Down Expand Up @@ -1503,6 +1520,144 @@ func (q *Queue[T]) TryPopFront() (T, bool)

TryPopFront tries popuping an element from the front of the queue.

## type [SList](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L4-L8>)

SList is a single linked list.

```go
type SList[T any] struct {
// contains filtered or unexported fields
}
```

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

```go
func SListOf[T any](values ...T) SList[T]
```

SListOf return a SList that contains values.

### func \(\*SList\[T\]\) [Back](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L47>)

```go
func (l *SList[T]) Back() T
```

Back returns the last element in the list.

### func \(\*SList\[T\]\) [Clear](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L35>)

```go
func (l *SList[T]) Clear()
```

Clear erases all elements from the container. After this call, Len\(\) returns zero.

### func \(\*SList\[T\]\) [ForEach](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L126>)

```go
func (l *SList[T]) ForEach(cb func(T))
```

ForEach iterate the list, apply each element to the cb callback function.

### func \(\*SList\[T\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L134>)

```go
func (l *SList[T]) ForEachIf(cb func(T) bool)
```

ForEachIf iterate the container, apply each element to the cb callback function, stop if cb returns false.

### func \(\*SList\[T\]\) [ForEachMutable](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L143>)

```go
func (l *SList[T]) ForEachMutable(cb func(*T))
```

ForEachMutable iterate the container, apply pointer of each element to the cb callback function.

### func \(\*SList\[T\]\) [ForEachMutableIf](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L151>)

```go
func (l *SList[T]) ForEachMutableIf(cb func(*T) bool)
```

ForEachMutableIf iterate the container, apply pointer of each element to the cb callback function, stop if cb returns false.

### func \(\*SList\[T\]\) [Front](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L42>)

```go
func (l *SList[T]) Front() T
```

Front returns the first element in the list.

### func \(\*SList\[T\]\) [IsEmpty](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L25>)

```go
func (l *SList[T]) IsEmpty() bool
```

IsEmpty checks if the container has no elements.

### func \(\*SList\[T\]\) [Iterate](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L160>)

```go
func (l *SList[T]) Iterate() MutableIterator[T]
```

Iterate returns an iterator to the whole container.

### func \(\*SList\[T\]\) [Len](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L30>)

```go
func (l *SList[T]) Len() int
```

Len returns the number of elements in the container.

### func \(\*SList\[T\]\) [PopFront](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L76>)

```go
func (l *SList[T]) PopFront() T
```

PopFront popups an element from the front of the list. The list must be non\-empty\!

### func \(\*SList\[T\]\) [PushBack](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L62>)

```go
func (l *SList[T]) PushBack(v T)
```

PushBack pushed an element to the tail of the list.

### func \(\*SList\[T\]\) [PushFront](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L52>)

```go
func (l *SList[T]) PushFront(v T)
```

PushFront pushed an element to the front of the list.

### func \(\*SList\[T\]\) [Reverse](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L89>)

```go
func (l *SList[T]) Reverse()
```

Reverse reverses the order of all elements in the container.

### func \(\*SList\[T\]\) [Values](<https://github.com/chen3feng/stl4go/blob/master/slist.go#L105>)

```go
func (l *SList[T]) Values() []T
```

Values copies all elements in the container to a slice and return it.

## type [Set](<https://github.com/chen3feng/stl4go/blob/master/container.go#L24-L33>)

Set is a containers that store unique elements.
Expand Down Expand Up @@ -1872,31 +2027,31 @@ Clear erases all elements from the vector. After this call, Len\(\) returns zero
func (v Vector[T]) ForEach(cb func(val T))
```

ForEach iterate the list, apply each element to the cb callback function.
ForEach iterate the container, apply each element to the cb callback function.

### func \(Vector\[T\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L165>)
### func \(Vector\[T\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L166>)

```go
func (v Vector[T]) ForEachIf(cb func(val T) bool)
```

ForEachIf iterate the list, apply each element to the cb callback function, stop if cb returns false.
ForEachIf iterate the container, apply each element to the cb callback function, stop if cb returns false.

### func \(Vector\[T\]\) [ForEachMutable](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L174>)
### func \(Vector\[T\]\) [ForEachMutable](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L175>)

```go
func (v Vector[T]) ForEachMutable(cb func(val *T))
```

ForEachMutable iterate the list, apply pointer of each element to the cb callback function.
ForEachMutable iterate the container, apply pointer of each element to the cb callback function.

### func \(Vector\[T\]\) [ForEachMutableIf](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L181>)
### func \(Vector\[T\]\) [ForEachMutableIf](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L183>)

```go
func (v Vector[T]) ForEachMutableIf(cb func(val *T) bool)
```

ForEachMutableIf iterate the list, apply pointer of each element to the cb callback function, stop if cb returns false.
ForEachMutableIf iterate the container, apply pointer of each element to the cb callback function, stop if cb returns false.

### func \(\*Vector\[T\]\) [Insert](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L123>)

Expand All @@ -1916,21 +2071,21 @@ func (v *Vector[T]) IsEmpty() bool

IsEmpty implements the Container interface.

### func \(Vector\[T\]\) [Iterate](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L190>)
### func \(Vector\[T\]\) [Iterate](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L192>)

```go
func (v Vector[T]) Iterate() MutableIterator[T]
```

Iterate returns an iterator to the whole vector.
Iterate returns an iterator to the whole container.

### func \(Vector\[T\]\) [IterateRange](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L195>)
### func \(Vector\[T\]\) [IterateRange](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L197>)

```go
func (v Vector[T]) IterateRange(i, j int) MutableIterator[T]
```

IterateRange returns an iterator to the range \[i, j\) of the vector.
IterateRange returns an iterator to the range \[i, j\) of the container.

### func \(\*Vector\[T\]\) [Len](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L33>)

Expand Down
Loading