Skip to content

Commit

Permalink
Add heap and priority queue (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng authored Aug 28, 2022
1 parent ca2ebab commit 3b87e2f
Show file tree
Hide file tree
Showing 9 changed files with 871 additions and 21 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Currently implemented containers are:
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
- [x] `Queue` is a bidirectional FIFO queue, implemented based on linked list.
- [x] `PriorityQuque` is a priority queue based on heap. Much easier to use and faster than [container/heap](https://pkg.go.dev/container/heap).

Different containers support different methods. The following are the methods supported by all containers:

Expand Down Expand Up @@ -177,6 +178,26 @@ See C++ STL.
- `IsSorted` check whether the slice is sorted
- `IsDescSorted` check whether the slice is sorted in descending order

#### heap

Heap provides basic min heap algorithms:

- `MakeMinHeap` Convert a slice to a min heap
- `IsMinHeap` Check whether a slice is a min heap
- `PushMinHeap` Pushes an element in to the heap
- `PopMinHeap` Popups an element from the top of the heap
- `RemoveMinHeap` Removes an element at index from the heap

and variants with custome comparasion function:

- `MakeHeapFunc`
- `IsHeapFunc`
- `PushHeapFunc`
- `PopHeapFunc`
- `RemoveHeapFunc`

both of them are mush faster and easier to use than [container/heap](https://pkg.go.dev/container/heap).

### Interface Design and Naming

The design leart much from the C++ STL. The `T` here represents `template`. Yes, Go's generic is not template. but who made C++ so influential and STL so famous?
Expand Down
31 changes: 26 additions & 5 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ import "github.com/chen3feng/stl4go"

目前实现的容器有:

- [x] 集合 用 Go 自己的 map 封装了一个 `BuiltinSet`
- [x] 基于 slice 封装的向量 `Vector`。提供了中间插入删除、区间删除等功能,依然与 slice 兼容。
- [x] 双链表 DList
- [x] `Set` 集合。用 Go 自己的 map 封装了一个 `BuiltinSet`
- [x] `Vector` 是基于 slice 封装的向量。提供了中间插入删除、区间删除等功能,依然与 slice 兼容。
- [x] `DList` 是双链表
- [x] [跳表(SkipList)](skiplist.md) 是一种有序的关联容器,可以填补 Go `map` 只支持无序的的空白。这是目前全 GitHub 最快的跳表,参见 [skiplist-survey](https://github.com/chen3feng/skiplist-survey)的性能比较
- [x] 栈 Stack,基于 Slice 实现
- [x] 队列 Queue 双向进出的队列,基于链表实现
- [x] `Stack`,栈基于 Slice 实现
- [x] `Queue` 双向进出的队列,基于链表实现
- [x] `PriorityQuque` 优先队列,基于堆实现,比 [container/heap](https://pkg.go.dev/container/heap) 更易用而且快不少。

不同的容器支持的方法不同,下面是所有容器都支持的方法:

Expand Down Expand Up @@ -161,6 +162,26 @@ func TestSkipList_ForEachMutable(t *testing.T) {
- LowerBound
- UpperBound

#### 堆

提供基本的堆算法:

- `MakeMinHeap` 在一个切片上构造出一个最小堆
- `IsMinHeap` 判断一个切片是不是一个最小堆
- `PushMinHeap` 把一个元素压入最小堆
- `PopMinHeap` 弹出堆顶的元素
- `RemoveMinHeap` 从切片的指定位置删除一个元素

以及相应的自定义比较函数的版本:

- `MakeHeapFunc`
- `IsHeapFunc`
- `PushHeapFunc`
- `PopHeapFunc`
- `RemoveHeapFunc`

都比 go 标准库 [container/heap](https://pkg.go.dev/container/heap) 更容易使用且更快。

### 接口设计和命名

较多地参考了 C++ STL。T 表示模板。是的,Go 的范型不是模板,但是谁让 C++ 那么影响力,而 STL 又那么有名呢。
Expand Down
Loading

0 comments on commit 3b87e2f

Please sign in to comment.