Skip to content

Commit

Permalink
Merge pull request #1 from yourbasic/tip
Browse files Browse the repository at this point in the history
Tip
  • Loading branch information
korthaj authored May 16, 2017
2 parents 7b420f4 + a9f8223 commit 4faf53a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ There is an online reference for the package at
* Version numbers adhere to [semantic versioning][sv].

The only accepted reason to modify the API of this package
is to handle bug fixes that can't be resolved in any other
is to handle issues that can't be resolved in any other
reasonable way.

Stefan Nilsson – [korthaj](https://github.com/korthaj)
Expand Down
47 changes: 38 additions & 9 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"testing"
)

const n = 1000

func BenchmarkNew(b *testing.B) {
n := 1000
b.StopTimer()
a := make([]int64, n)
b.StartTimer()
Expand All @@ -14,8 +15,16 @@ func BenchmarkNew(b *testing.B) {
}
}

func BenchmarkAppend(b *testing.B) {
l := new(List)
for i := 0; i < b.N; i++ {
for j := 0; j < n; j++ {
l.Append(int64(j))
}
}
}

func BenchmarkSum(b *testing.B) {
n := 1000
b.StopTimer()
a := make([]int64, n)
l := New(a...)
Expand All @@ -27,18 +36,38 @@ func BenchmarkSum(b *testing.B) {
}
}

func BenchmarkSumSlice(b *testing.B) {
n := 1000
func BenchmarkGet(b *testing.B) {
b.StopTimer()
a := make([]int64, n)
l := New(a...)
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < n; j++ {
_ = l.Get(j)
}
}
}

func BenchmarkSet(b *testing.B) {
b.StopTimer()
a := make([]int64, n)
l := New(a...)
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < n; j++ {
l.Set(j, int64(j))
}
}
}

func BenchmarkAdd(b *testing.B) {
b.StopTimer()
a := make([]int64, n)
l := New(a...)
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < n; j++ {
var sum int64
for k := 0; k < j; k++ {
sum += a[k]
}
_ = sum
l.Add(j, int64(j))
}
}
}
17 changes: 14 additions & 3 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
//
package fenwick

// List represents a Fenwick tree.
// List represents a list of numbers with support for efficient
// prefix sum computation. The zero value is an empty list.
type List struct {
// The tree slice stores range sums of an underlying array t.
// To compute the prefix sum t[0] + t[1] + t[k-1], add elements
Expand Down Expand Up @@ -50,12 +51,22 @@ func (l *List) Len() int {

// Get returns the element at index i.
func (l *List) Get(i int) int64 {
return l.SumRange(i, i+1)
sum := l.tree[i]
j := i + 1
j -= j & -j
for i > j {
sum -= l.tree[i-1]
i -= i & -i
}
return sum
}

// Set sets the element at index i to n.
func (l *List) Set(i int, n int64) {
l.Add(i, n-l.Get(i))
n -= l.Get(i)
for len := len(l.tree); i < len; i |= i + 1 {
l.tree[i] += n
}
}

// Add adds n to the element at index i.
Expand Down
32 changes: 15 additions & 17 deletions list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
)

func TestGet(t *testing.T) {
n := 50
n := 100
a := make([]int64, n)
l := New()
for i := range a {
a[i] = int64(i)
}
l = New(a...)
l := New(a...)
for i := range a {
if a[i] != l.Get(i) {
t.Errorf("Get(%d) = %d; want %d", i, l.Get(i), a[i])
Expand All @@ -20,13 +19,12 @@ func TestGet(t *testing.T) {
}

func TestSet(t *testing.T) {
n := 50
n := 100
a := make([]int64, n)
l := New()
for i := range a {
a[i] = int64(i)
}
l = New(a...)
l := New(a...)
for i := range a {
l.Set(i, 100)
if l.Get(i) != 100 {
Expand All @@ -36,13 +34,12 @@ func TestSet(t *testing.T) {
}

func TestAdd(t *testing.T) {
n := 50
n := 100
a := make([]int64, n)
l := New()
for i := range a {
a[i] = int64(i)
}
l = New(a...)
l := New(a...)
for i := range a {
l.Add(i, 100)
if l.Get(i) != a[i]+100 {
Expand All @@ -52,13 +49,12 @@ func TestAdd(t *testing.T) {
}

func TestSum(t *testing.T) {
n := 50
n := 100
a := make([]int64, n)
l := New()
for i := range a {
a[i] = int64(i)
}
l = New(a...)
l := New(a...)
for i := range a {
var res int64
for j := 0; j < i; j++ {
Expand All @@ -71,13 +67,12 @@ func TestSum(t *testing.T) {
}

func TestSumRange(t *testing.T) {
n := 50
n := 100
a := make([]int64, n)
l := New()
for i := range a {
a[i] = int64(i)
}
l = New(a...)
l := New(a...)
for i := range a {
for j := i; j < n; j++ {
var res int64
Expand All @@ -92,9 +87,12 @@ func TestSumRange(t *testing.T) {
}

func TestAppend(t *testing.T) {
n := 50
n := 100
a := make([]int64, n)
l := New()
l := new(List)
if l.Len() != 0 {
t.Errorf("Len() = %d; want %d", l.Len(), 0)
}
for i := range a {
a[i] = int64(i)
l.Append(int64(i))
Expand Down

0 comments on commit 4faf53a

Please # to comment.