-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 758aa92
Showing
7 changed files
with
173 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Public Domain (-) 2011 The Ampify Authors. | ||
# See the Ampify UNLICENSE file for details. | ||
|
||
include $(GOROOT)/src/Make.inc | ||
|
||
TARG=ampstore | ||
GOFILES=\ | ||
skiplist.go\ | ||
|
||
include $(GOROOT)/src/Make.pkg |
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 @@ | ||
// Ampstore.go : Draft version of the Ampify data store |
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 @@ | ||
// Ampstore.go : Draft version of the Ampify data store |
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 @@ | ||
// Ampstore.go : Draft version of the Ampify data store |
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,46 @@ | ||
// Ampstore.go : Draft version of the Ampify data store | ||
import ( | ||
"./skiplist" | ||
"./serializer" | ||
) | ||
|
||
type Key []byte | ||
type Value []byte | ||
|
||
type Store struct { | ||
keyList *SkipList | ||
table *[Key] Value | ||
} | ||
|
||
type Datum struct { | ||
k Key | ||
v Value | ||
} | ||
|
||
func (s *Store) Get(k Key) Datum { | ||
datum := s.table.Get(k) | ||
return Datum | ||
} | ||
|
||
func (s *Store) Set(k Key, v Value) { | ||
s.keyList.Insert(k) | ||
s.table.Set(k, v) | ||
} | ||
|
||
func (s *Store) Scan(start Key, end Key) { | ||
vect = make(Vector) | ||
for k,v := range(s.keyList[start, end]) { | ||
vect.Append(new Datum{ k, v }) | ||
} | ||
return Serializer.Write(vect) | ||
} | ||
|
||
func (s *Store) Delete(k Key) { | ||
s.keyList.Delete(k) | ||
s.table.Delete(k) | ||
} | ||
|
||
func Init(opts Opt) *Store { | ||
store = new Store | ||
return &store | ||
} |
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,99 @@ | ||
// Ampstore.go : Draft version of the Ampify data store | ||
package ampstore | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
type Element struct { | ||
Next []*Element | ||
Value []byte | ||
} | ||
|
||
type SkipList struct { | ||
Head *Element | ||
Level int | ||
Length int | ||
} | ||
|
||
func New(maxLevel int) *SkipList { | ||
head := &Element{make([]*Element, maxLevel), []byte{}} | ||
return &SkipList{Level: 0, Head: head, Length: 0} | ||
} | ||
|
||
func (s *SkipList) Insert(value []byte) { | ||
level := 0 | ||
t := time.Now() | ||
rand.Seed((int64)(t.Nanosecond())) | ||
for rand.Float64() < 0.5 { | ||
level++ | ||
} | ||
if level+1 > s.Level { | ||
s.Level = level + 1 | ||
} | ||
|
||
newElement := &Element{make([]*Element, level+1), value} | ||
|
||
for i := s.Level - 1; i >= 0; i-- { | ||
var ptr *Element | ||
for ptr = s.Head; ptr.Next[i] != nil; ptr = ptr.Next[i] { | ||
if bytes.Compare(ptr.Next[i].Value, value) > 0 { | ||
break | ||
} | ||
} | ||
|
||
if i <= level { | ||
newElement.Next[i] = ptr.Next[i] | ||
ptr.Next[i] = newElement | ||
} | ||
} | ||
s.Length++ | ||
} | ||
|
||
func (s *SkipList) Delete(value []byte) { | ||
if !s.Contains(value) { | ||
var prePtr *Element | ||
for i := s.Level - 1; i >= 0; i++ { | ||
for ptr := s.Head; ptr.Next[i] != nil; ptr = ptr.Next[i] { | ||
if bytes.Compare(ptr.Value, value) == 0 { | ||
prePtr.Next[i] = ptr.Next[i].Next[i] | ||
break | ||
} | ||
if bytes.Compare(ptr.Next[i].Value, value) > 0 { | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (s *SkipList) Contains(value []byte) bool { | ||
for i := s.Level - 1; i >= 0; i-- { | ||
for ptr := s.Head; ptr.Next[i] != nil; ptr = ptr.Next[i] { | ||
if bytes.Compare(ptr.Next[i].Value, value) == 0 { | ||
return true | ||
} | ||
if bytes.Compare(ptr.Next[i].Value, value) > 0 { | ||
break | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (s *SkipList) Len() int { | ||
return s.Length | ||
} | ||
|
||
func (s *SkipList) Show() { | ||
for i := s.Level - 1; i >= 0; i-- { | ||
fmt.Printf("Level %d: ", i) | ||
for ptr := s.Head; ptr.Next[i] != nil; ptr = ptr.Next[i] { | ||
fmt.Printf("%v ", ptr.Next[i].Value) | ||
} | ||
fmt.Printf("\n\n") | ||
} | ||
} |
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,15 @@ | ||
// Ampstore.go : Draft version of the Ampify data store | ||
package ampstore | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_SkipList(t *testing.T) { | ||
sl := New(32) | ||
sl.Insert([]byte{1, 2, 3}) | ||
sl.Insert([]byte{3, 2, 3}) | ||
sl.Insert([]byte{5, 2, 3}) | ||
sl.Insert([]byte{7, 2, 3}) | ||
sl.Show() | ||
} |