-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add v2, a new version built with generics
- Loading branch information
Showing
10 changed files
with
1,391 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Overview | ||
Deque is a highly optimized double-ended queue. | ||
|
||
# Benchmark | ||
``` | ||
PushBack/Deque 20000000 13.6 ns/op 8 B/op 0 allocs/op | ||
PushBack/list.List 5000000 158.7 ns/op 56 B/op 1 allocs/op | ||
PushFront/Deque 30000000 9.8 ns/op 8 B/op 0 allocs/op | ||
PushFront/list.List 5000000 159.2 ns/op 56 B/op 1 allocs/op | ||
Random/Deque 50000000 13.9 ns/op 0 B/op 0 allocs/op | ||
Random/list.List 30000000 46.9 ns/op 28 B/op 1 allocs/op | ||
``` | ||
|
||
# Usage | ||
``` go | ||
import "github.com/edwingeng/deque/v2" | ||
|
||
dq := NewDeque[int]() | ||
dq.PushBack(100) | ||
dq.PushBack(200) | ||
dq.PushBack(300) | ||
for !dq.IsEmpty() { | ||
fmt.Println(dq.PopFront()) | ||
} | ||
|
||
dq.PushFront(100) | ||
dq.PushFront(200) | ||
dq.PushFront(300) | ||
for i, n := 0, dq.Len(); i < n; i++ { | ||
fmt.Println(dq.PopFront()) | ||
} | ||
|
||
// Output: | ||
// 100 | ||
// 200 | ||
// 300 | ||
// 300 | ||
// 200 | ||
// 100 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package deque | ||
|
||
import ( | ||
"container/list" | ||
"math/rand" | ||
"testing" | ||
) | ||
|
||
func BenchmarkPushBack(b *testing.B) { | ||
b.Run("Deque", func(b *testing.B) { | ||
dq := NewDeque[int]() | ||
for i := 0; i < b.N; i++ { | ||
dq.PushBack(i) | ||
} | ||
}) | ||
b.Run("list.List", func(b *testing.B) { | ||
lst := list.New() | ||
for i := 0; i < b.N; i++ { | ||
lst.PushBack(i) | ||
} | ||
}) | ||
} | ||
|
||
func BenchmarkPushFront(b *testing.B) { | ||
b.Run("Deque", func(b *testing.B) { | ||
dq := NewDeque[int]() | ||
for i := 0; i < b.N; i++ { | ||
dq.PushFront(i) | ||
} | ||
}) | ||
b.Run("list.List", func(b *testing.B) { | ||
lst := list.New() | ||
for i := 0; i < b.N; i++ { | ||
lst.PushFront(i) | ||
} | ||
}) | ||
} | ||
|
||
func BenchmarkRandom(b *testing.B) { | ||
const nn = 10000 | ||
a := make([]int, nn) | ||
for i := 0; i < nn; i++ { | ||
a[i] = rand.Int() | ||
} | ||
|
||
b.Run("Deque", func(b *testing.B) { | ||
dq := NewDeque[int]() | ||
for i := 0; i < b.N; i++ { | ||
switch a[i%nn] % 4 { | ||
case 0: | ||
dq.PushBack(i) | ||
case 1: | ||
dq.PushFront(i) | ||
case 2: | ||
dq.TryPopBack() | ||
case 3: | ||
dq.TryPopFront() | ||
} | ||
} | ||
}) | ||
b.Run("list.List", func(b *testing.B) { | ||
lst := list.New() | ||
for i := 0; i < b.N; i++ { | ||
switch a[i%nn] % 4 { | ||
case 0: | ||
lst.PushBack(i) | ||
case 1: | ||
lst.PushFront(i) | ||
case 2: | ||
if v := lst.Back(); v != nil { | ||
lst.Remove(v) | ||
} | ||
case 3: | ||
if v := lst.Front(); v != nil { | ||
lst.Remove(v) | ||
} | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env bash | ||
|
||
[[ "$TRACE" ]] && set -x | ||
pushd `dirname "$0"` > /dev/null | ||
trap __EXIT EXIT | ||
|
||
colorful=false | ||
tput setaf 7 > /dev/null 2>&1 | ||
if [[ $? -eq 0 ]]; then | ||
colorful=true | ||
fi | ||
|
||
function __EXIT() { | ||
popd > /dev/null | ||
} | ||
|
||
function printError() { | ||
$colorful && tput setaf 1 | ||
>&2 echo "Error: $@" | ||
$colorful && tput setaf 7 | ||
} | ||
|
||
function printImportantMessage() { | ||
$colorful && tput setaf 3 | ||
>&2 echo "$@" | ||
$colorful && tput setaf 7 | ||
} | ||
|
||
function printUsage() { | ||
$colorful && tput setaf 3 | ||
>&2 echo "$@" | ||
$colorful && tput setaf 7 | ||
} | ||
|
||
go test -cover -coverprofile=c.out -v "$@" && go tool cover -html=c.out |
Oops, something went wrong.