Skip to content

Commit

Permalink
add Map interface (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng committed Aug 7, 2022
1 parent 81e8ac2 commit ed23499
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 25 deletions.
50 changes: 38 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Package stl4go is a generic container and algorithm library for go.
- [type HashFn](<#type-hashfn>)
- [type Integer](<#type-integer>)
- [type LessFn](<#type-lessfn>)
- [type Map](<#type-map>)
- [type Numeric](<#type-numeric>)
- [type Ordered](<#type-ordered>)
- [type Queue](<#type-queue>)
Expand Down Expand Up @@ -127,6 +128,7 @@ Package stl4go is a generic container and algorithm library for go.
- [type SkipList](<#type-skiplist>)
- [func NewSkipList[K Ordered, V any]() *SkipList[K, V]](<#func-newskiplist>)
- [func NewSkipListFromMap[K Ordered, V any](m map[K]V) *SkipList[K, V]](<#func-newskiplistfrommap>)
- [func NewSkipListFunc[K any, V any](keyCmp CompareFn[K]) *SkipList[K, V]](<#func-newskiplistfunc>)
- [func (sl *SkipList[K, V]) Clear()](<#func-skiplistk-v-clear>)
- [func (sl *SkipList[K, V]) Find(key K) *V](<#func-skiplistk-v-find>)
- [func (sl *SkipList[K, V]) ForEach(op func(K, *V))](<#func-skiplistk-v-foreach>)
Expand Down Expand Up @@ -839,6 +841,22 @@ LessFn is a function that returns whether 'a' is less than 'b'.
type LessFn[T any] func(a, b T) bool
```

## type [Map](<https://github.com/chen3feng/stl4go/blob/master/container.go#L11-L19>)

Map is a associative container that contains key\-value pairs with unique keys.

```go
type Map[K any, V any] interface {
Container
Has(K) bool // Checks whether the container contains element with specific key.
Find(K) *V // Finds element with specific key.
Insert(K, V) // Inserts a key-value pair in to the container or replace existing value.
Remove(K) bool // Remove element with specific key.
ForEach(func(K, *V)) // Iterate the container.
ForEachIf(func(K, *V) bool) // Iterate the container, stops when the callback returns false.
}
```

## type [Numeric](<https://github.com/chen3feng/stl4go/blob/master/types.go#L40-L42>)

Numeric is a constraint that permits any numeric type.
Expand Down Expand Up @@ -1030,7 +1048,7 @@ SkipList is a probabilistic data structure that seem likely to supplant balanced
See https://en.wikipedia.org/wiki/Skip_list for more details.

```go
type SkipList[K Ordered, V any] struct {
type SkipList[K any, V any] struct {
// contains filtered or unexported fields
}
```
Expand All @@ -1041,69 +1059,77 @@ type SkipList[K Ordered, V any] struct {
func NewSkipList[K Ordered, V any]() *SkipList[K, V]
```

NewSkipList create a new Skiplist.
NewSkipList creates a new Skiplist.

### func [NewSkipListFromMap](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L51>)
### func [NewSkipListFromMap](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L43>)

```go
func NewSkipListFromMap[K Ordered, V any](m map[K]V) *SkipList[K, V]
```

NewSkipListFromMap create a new Skiplist from a map.

### func \(\*SkipList\[K, V\]\) [Clear](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L67>)
### func [NewSkipListFunc](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L52>)

```go
func NewSkipListFunc[K any, V any](keyCmp CompareFn[K]) *SkipList[K, V]
```

NewSkipListFunc creates a new Skiplist with specified compare function keyCmp.

### func \(\*SkipList\[K, V\]\) [Clear](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L72>)

```go
func (sl *SkipList[K, V]) Clear()
```

### func \(\*SkipList\[K, V\]\) [Find](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L106>)
### func \(\*SkipList\[K, V\]\) [Find](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L111>)

```go
func (sl *SkipList[K, V]) Find(key K) *V
```

Find returns the value associated with the passed key if the key is in the skiplist, otherwise returns nil.

### func \(\*SkipList\[K, V\]\) [ForEach](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L135>)
### func \(\*SkipList\[K, V\]\) [ForEach](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L140>)

```go
func (sl *SkipList[K, V]) ForEach(op func(K, *V))
```

### func \(\*SkipList\[K, V\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L141>)
### func \(\*SkipList\[K, V\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L146>)

```go
func (sl *SkipList[K, V]) ForEachIf(op func(K, *V) bool)
```

### func \(\*SkipList\[K, V\]\) [Has](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L114>)
### func \(\*SkipList\[K, V\]\) [Has](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L119>)

```go
func (sl *SkipList[K, V]) Has(key K) bool
```

### func \(\*SkipList\[K, V\]\) [Insert](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L77>)
### func \(\*SkipList\[K, V\]\) [Insert](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L82>)

```go
func (sl *SkipList[K, V]) Insert(key K, value V)
```

Insert inserts a key\-value pair into the skiplist. If the key is already in the skip list, it's value will be updated.

### func \(\*SkipList\[K, V\]\) [IsEmpty](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L59>)
### func \(\*SkipList\[K, V\]\) [IsEmpty](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L64>)

```go
func (sl *SkipList[K, V]) IsEmpty() bool
```

### func \(\*SkipList\[K, V\]\) [Len](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L63>)
### func \(\*SkipList\[K, V\]\) [Len](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L68>)

```go
func (sl *SkipList[K, V]) Len() int
```

### func \(\*SkipList\[K, V\]\) [Remove](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L120>)
### func \(\*SkipList\[K, V\]\) [Remove](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L125>)

```go
func (sl *SkipList[K, V]) Remove(key K) bool
Expand Down
50 changes: 38 additions & 12 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Package stl4go is a generic container and algorithm library for go.
- [type HashFn](<#type-hashfn>)
- [type Integer](<#type-integer>)
- [type LessFn](<#type-lessfn>)
- [type Map](<#type-map>)
- [type Numeric](<#type-numeric>)
- [type Ordered](<#type-ordered>)
- [type Queue](<#type-queue>)
Expand Down Expand Up @@ -123,6 +124,7 @@ Package stl4go is a generic container and algorithm library for go.
- [type SkipList](<#type-skiplist>)
- [func NewSkipList[K Ordered, V any]() *SkipList[K, V]](<#func-newskiplist>)
- [func NewSkipListFromMap[K Ordered, V any](m map[K]V) *SkipList[K, V]](<#func-newskiplistfrommap>)
- [func NewSkipListFunc[K any, V any](keyCmp CompareFn[K]) *SkipList[K, V]](<#func-newskiplistfunc>)
- [func (sl *SkipList[K, V]) Clear()](<#func-skiplistk-v-clear>)
- [func (sl *SkipList[K, V]) Find(key K) *V](<#func-skiplistk-v-find>)
- [func (sl *SkipList[K, V]) ForEach(op func(K, *V))](<#func-skiplistk-v-foreach>)
Expand Down Expand Up @@ -835,6 +837,22 @@ LessFn is a function that returns whether 'a' is less than 'b'.
type LessFn[T any] func(a, b T) bool
```

## type [Map](<https://github.com/chen3feng/stl4go/blob/master/container.go#L11-L19>)

Map is a associative container that contains key\-value pairs with unique keys.

```go
type Map[K any, V any] interface {
Container
Has(K) bool // Checks whether the container contains element with specific key.
Find(K) *V // Finds element with specific key.
Insert(K, V) // Inserts a key-value pair in to the container or replace existing value.
Remove(K) bool // Remove element with specific key.
ForEach(func(K, *V)) // Iterate the container.
ForEachIf(func(K, *V) bool) // Iterate the container, stops when the callback returns false.
}
```

## type [Numeric](<https://github.com/chen3feng/stl4go/blob/master/types.go#L40-L42>)

Numeric is a constraint that permits any numeric type.
Expand Down Expand Up @@ -1026,7 +1044,7 @@ SkipList is a probabilistic data structure that seem likely to supplant balanced
See https://en.wikipedia.org/wiki/Skip_list for more details.

```go
type SkipList[K Ordered, V any] struct {
type SkipList[K any, V any] struct {
// contains filtered or unexported fields
}
```
Expand All @@ -1037,69 +1055,77 @@ type SkipList[K Ordered, V any] struct {
func NewSkipList[K Ordered, V any]() *SkipList[K, V]
```

NewSkipList create a new Skiplist.
NewSkipList creates a new Skiplist.

### func [NewSkipListFromMap](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L51>)
### func [NewSkipListFromMap](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L43>)

```go
func NewSkipListFromMap[K Ordered, V any](m map[K]V) *SkipList[K, V]
```

NewSkipListFromMap create a new Skiplist from a map.

### func \(\*SkipList\[K, V\]\) [Clear](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L67>)
### func [NewSkipListFunc](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L52>)

```go
func NewSkipListFunc[K any, V any](keyCmp CompareFn[K]) *SkipList[K, V]
```

NewSkipListFunc creates a new Skiplist with specified compare function keyCmp.

### func \(\*SkipList\[K, V\]\) [Clear](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L72>)

```go
func (sl *SkipList[K, V]) Clear()
```

### func \(\*SkipList\[K, V\]\) [Find](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L106>)
### func \(\*SkipList\[K, V\]\) [Find](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L111>)

```go
func (sl *SkipList[K, V]) Find(key K) *V
```

Find returns the value associated with the passed key if the key is in the skiplist, otherwise returns nil.

### func \(\*SkipList\[K, V\]\) [ForEach](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L135>)
### func \(\*SkipList\[K, V\]\) [ForEach](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L140>)

```go
func (sl *SkipList[K, V]) ForEach(op func(K, *V))
```

### func \(\*SkipList\[K, V\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L141>)
### func \(\*SkipList\[K, V\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L146>)

```go
func (sl *SkipList[K, V]) ForEachIf(op func(K, *V) bool)
```

### func \(\*SkipList\[K, V\]\) [Has](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L114>)
### func \(\*SkipList\[K, V\]\) [Has](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L119>)

```go
func (sl *SkipList[K, V]) Has(key K) bool
```

### func \(\*SkipList\[K, V\]\) [Insert](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L77>)
### func \(\*SkipList\[K, V\]\) [Insert](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L82>)

```go
func (sl *SkipList[K, V]) Insert(key K, value V)
```

Insert inserts a key\-value pair into the skiplist. If the key is already in the skip list, it's value will be updated.

### func \(\*SkipList\[K, V\]\) [IsEmpty](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L59>)
### func \(\*SkipList\[K, V\]\) [IsEmpty](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L64>)

```go
func (sl *SkipList[K, V]) IsEmpty() bool
```

### func \(\*SkipList\[K, V\]\) [Len](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L63>)
### func \(\*SkipList\[K, V\]\) [Len](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L68>)

```go
func (sl *SkipList[K, V]) Len() int
```

### func \(\*SkipList\[K, V\]\) [Remove](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L120>)
### func \(\*SkipList\[K, V\]\) [Remove](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L125>)

```go
func (sl *SkipList[K, V]) Remove(key K) bool
Expand Down
11 changes: 11 additions & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@ type Container interface {
Len() int // Len returns the number of elements in the container.
Clear() // Clear erases all elements from the container. After this call, Len() returns zero.
}

// Map is a associative container that contains key-value pairs with unique keys.
type Map[K any, V any] interface {
Container
Has(K) bool // Checks whether the container contains element with specific key.
Find(K) *V // Finds element with specific key.
Insert(K, V) // Inserts a key-value pair in to the container or replace existing value.
Remove(K) bool // Remove element with specific key.
ForEach(func(K, *V)) // Iterate the container.
ForEachIf(func(K, *V) bool) // Iterate the container, stops when the callback returns false.
}
2 changes: 1 addition & 1 deletion skiplist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestSkipListInterface(t *testing.T) {
_ = Container(NewSkipList[int, int]())
_ = Map[int, int](NewSkipList[int, int]())
}

func TestNewSkipList(t *testing.T) {
Expand Down

0 comments on commit ed23499

Please sign in to comment.