Skip to content

Commit

Permalink
Better type switch syntax (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng committed Aug 11, 2022
1 parent 25b49a9 commit 6039ed6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 46 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ type Signed interface {
}
```

## type [SkipList](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L27-L37>)
## type [SkipList](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L28-L38>)

SkipList is a probabilistic data structure that seem likely to supplant balanced trees as the implementation method of choice for many applications. Skip list algorithms have the same asymptotic expected time bounds as balanced trees and are simpler, faster and use less space.

Expand All @@ -1075,95 +1075,95 @@ type SkipList[K any, V any] struct {
}
```

### func [NewSkipList](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L49>)
### func [NewSkipList](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L50>)

```go
func NewSkipList[K Ordered, V any]() *SkipList[K, V]
```

NewSkipList creates a new Skiplist.

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

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

NewSkipListFromMap create a new Skiplist from a map.

### func [NewSkipListFunc](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L65>)
### func [NewSkipListFunc](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L66>)

```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#L85>)
### func \(\*SkipList\[K, V\]\) [Clear](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L86>)

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

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

```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#L153>)
### func \(\*SkipList\[K, V\]\) [ForEach](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L154>)

```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#L165>)
### func \(\*SkipList\[K, V\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L166>)

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

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

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

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

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

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

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

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

```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#L77>)
### func \(\*SkipList\[K, V\]\) [IsEmpty](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L78>)

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

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

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

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

```go
func (sl *SkipList[K, V]) Remove(key K) bool
Expand Down
30 changes: 15 additions & 15 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ type Signed interface {
}
```

## type [SkipList](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L27-L37>)
## type [SkipList](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L28-L38>)

SkipList is a probabilistic data structure that seem likely to supplant balanced trees as the implementation method of choice for many applications. Skip list algorithms have the same asymptotic expected time bounds as balanced trees and are simpler, faster and use less space.

Expand All @@ -1073,95 +1073,95 @@ type SkipList[K any, V any] struct {
}
```

### func [NewSkipList](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L49>)
### func [NewSkipList](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L50>)

```go
func NewSkipList[K Ordered, V any]() *SkipList[K, V]
```

NewSkipList creates a new Skiplist.

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

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

NewSkipListFromMap create a new Skiplist from a map.

### func [NewSkipListFunc](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L65>)
### func [NewSkipListFunc](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L66>)

```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#L85>)
### func \(\*SkipList\[K, V\]\) [Clear](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L86>)

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

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

```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#L153>)
### func \(\*SkipList\[K, V\]\) [ForEach](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L154>)

```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#L165>)
### func \(\*SkipList\[K, V\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L166>)

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

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

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

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

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

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

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

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

```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#L77>)
### func \(\*SkipList\[K, V\]\) [IsEmpty](<https://github.com/chen3feng/stl4go/blob/master/skiplist.go#L78>)

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

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

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

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

```go
func (sl *SkipList[K, V]) Remove(key K) bool
Expand Down
15 changes: 1 addition & 14 deletions skiplist_findnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,45 @@ func (sl *SkipList[K, V]) findNode(key K) *skipListNode[K, V] {
}
// For knowned Ordered types, use findNodeFast to improve performance.
var iface interface{} = key
switch iface.(type) {
switch v := iface.(type) {
case int:
tsl := pointerCast[*SkipList[int, V]](sl)
v, _ := iface.(int)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case string:
tsl := pointerCast[*SkipList[string, V]](sl)
v, _ := iface.(string)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case int32:
tsl := pointerCast[*SkipList[int32, V]](sl)
v, _ := iface.(int32)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case uint32:
tsl := pointerCast[*SkipList[uint32, V]](sl)
v, _ := iface.(uint32)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case int64:
tsl := pointerCast[*SkipList[int64, V]](sl)
v, _ := iface.(int64)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case uint64:
tsl := pointerCast[*SkipList[uint64, V]](sl)
v, _ := iface.(uint64)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case int8:
tsl := pointerCast[*SkipList[int8, V]](sl)
v, _ := iface.(int8)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case uint8:
tsl := pointerCast[*SkipList[uint8, V]](sl)
v, _ := iface.(uint8)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case int16:
tsl := pointerCast[*SkipList[int16, V]](sl)
v, _ := iface.(int16)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case uint16:
tsl := pointerCast[*SkipList[uint16, V]](sl)
v, _ := iface.(uint16)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case uintptr:
tsl := pointerCast[*SkipList[uintptr, V]](sl)
v, _ := iface.(uintptr)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case float32:
tsl := pointerCast[*SkipList[float32, V]](sl)
v, _ := iface.(float32)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
case float64:
tsl := pointerCast[*SkipList[float64, V]](sl)
v, _ := iface.(float64)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))
}
return sl.findNodeSlow(key)
Expand Down
3 changes: 1 addition & 2 deletions skiplist_findnode_generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ func (sl *SkipList[K, V]) findNode(key K) *skipListNode[K, V] {
}
// For knowned Ordered types, use findNodeFast to improve performance.
var iface interface{} = key
switch iface.(type) {"
switch v := iface.(type) {"

for type in int string int32 uint32 int64 uint64 int8 uint8 int16 uint16 uintptr float32 float64; do
echo "\
case $type:
tsl := pointerCast[*SkipList[$type, V]](sl)
v, _ := iface.($type)
return pointerCast[*skipListNode[K, V]](findNodeFast(tsl, v))"
done
echo "\
Expand Down

0 comments on commit 6039ed6

Please sign in to comment.