insert key and value

This commit is contained in:
Chris Lu 2021-10-03 01:07:35 -07:00
parent 69b84bb771
commit 4f50f8c2ca
5 changed files with 84 additions and 123 deletions

View file

@ -3,16 +3,10 @@ package skiplist
import "bytes" import "bytes"
func compareElement(a *SkipListElement, key []byte) int { func compareElement(a *SkipListElement, key []byte) int {
if len(a.Values) == 0 { if len(a.Key) == 0 {
return -1 return -1
} }
if bytes.Compare(a.Values[0], key) < 0 { return bytes.Compare(a.Key, key)
return -1
}
if bytes.Compare(a.Values[len(a.Values)-1], key) > 0 {
return 1
}
return 0
} }
var ( var (
@ -25,7 +19,7 @@ func (node *SkipListElement) Reference() *SkipListElementReference {
} }
return &SkipListElementReference{ return &SkipListElementReference{
ElementPointer: node.Id, ElementPointer: node.Id,
Key: node.Values[0], Key: node.Key,
} }
} }
func (node *SkipListElement) Save() { func (node *SkipListElement) Save() {

View file

@ -15,11 +15,11 @@ const (
) )
type SkipList struct { type SkipList struct {
startLevels [maxLevel]*SkipListElementReference startLevels [maxLevel]*SkipListElementReference
endLevels [maxLevel]*SkipListElementReference endLevels [maxLevel]*SkipListElementReference
maxNewLevel int maxNewLevel int
maxLevel int maxLevel int
elementCount int // elementCount int
} }
// NewSeedEps returns a new empty, initialized Skiplist. // NewSeedEps returns a new empty, initialized Skiplist.
@ -32,9 +32,9 @@ func NewSeed(seed int64) *SkipList {
//fmt.Printf("SkipList seed: %v\n", seed) //fmt.Printf("SkipList seed: %v\n", seed)
list := &SkipList{ list := &SkipList{
maxNewLevel: maxLevel, maxNewLevel: maxLevel,
maxLevel: 0, maxLevel: 0,
elementCount: 0, // elementCount: 0,
} }
return list return list
@ -193,7 +193,7 @@ func (t *SkipList) Delete(key []byte) {
nextNextNode.Prev = currentNode.Reference() nextNextNode.Prev = currentNode.Reference()
nextNextNode.Save() nextNextNode.Save()
} }
t.elementCount-- // t.elementCount--
nextNode.DeleteSelf() nextNode.DeleteSelf()
} }
@ -230,7 +230,7 @@ func (t *SkipList) Delete(key []byte) {
// Insert inserts the given ListElement into the skiplist. // Insert inserts the given ListElement into the skiplist.
// Insert runs in approx. O(log(n)) // Insert runs in approx. O(log(n))
func (t *SkipList) Insert(key []byte) { func (t *SkipList) Insert(key, value []byte) {
if t == nil || key == nil { if t == nil || key == nil {
return return
@ -245,13 +245,14 @@ func (t *SkipList) Insert(key []byte) {
} }
elem := &SkipListElement{ elem := &SkipListElement{
Id: rand.Int63(), Id: rand.Int63(),
Next: make([]*SkipListElementReference, t.maxNewLevel, t.maxNewLevel), Next: make([]*SkipListElementReference, t.maxNewLevel, t.maxNewLevel),
Level: int32(level), Level: int32(level),
Values: [][]byte{key}, Key: key,
Value: value,
} }
t.elementCount++ // t.elementCount++
newFirst := true newFirst := true
newLast := true newLast := true
@ -371,11 +372,6 @@ func (t *SkipList) Insert(key []byte) {
} }
// GetValue extracts the ListElement value from a skiplist node.
func (e *SkipListElement) GetValue() []byte {
return e.Values[0]
}
// GetSmallestNode returns the very first/smallest node in the skiplist. // GetSmallestNode returns the very first/smallest node in the skiplist.
// GetSmallestNode runs in O(1) // GetSmallestNode runs in O(1)
func (t *SkipList) GetSmallestNode() *SkipListElement { func (t *SkipList) GetSmallestNode() *SkipListElement {
@ -406,11 +402,6 @@ func (t *SkipList) Prev(e *SkipListElement) *SkipListElement {
return e.Prev.Load() return e.Prev.Load()
} }
// GetNodeCount returns the number of nodes currently in the skiplist.
func (t *SkipList) GetNodeCount() int {
return t.elementCount
}
// String returns a string format of the skiplist. Useful to get a graphical overview and/or debugging. // String returns a string format of the skiplist. Useful to get a graphical overview and/or debugging.
func (t *SkipList) println() { func (t *SkipList) println() {

View file

@ -30,12 +30,10 @@ type SkipListProto struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
StartLevels []*SkipListElementReference `protobuf:"bytes,1,rep,name=start_levels,json=startLevels,proto3" json:"start_levels,omitempty"` StartLevels []*SkipListElementReference `protobuf:"bytes,1,rep,name=start_levels,json=startLevels,proto3" json:"start_levels,omitempty"`
EndLevels []*SkipListElementReference `protobuf:"bytes,2,rep,name=end_levels,json=endLevels,proto3" json:"end_levels,omitempty"` EndLevels []*SkipListElementReference `protobuf:"bytes,2,rep,name=end_levels,json=endLevels,proto3" json:"end_levels,omitempty"`
MaxNewLevel int32 `protobuf:"varint,3,opt,name=max_new_level,json=maxNewLevel,proto3" json:"max_new_level,omitempty"` MaxNewLevel int32 `protobuf:"varint,3,opt,name=max_new_level,json=maxNewLevel,proto3" json:"max_new_level,omitempty"`
MaxLevel int32 `protobuf:"varint,4,opt,name=max_level,json=maxLevel,proto3" json:"max_level,omitempty"` MaxLevel int32 `protobuf:"varint,4,opt,name=max_level,json=maxLevel,proto3" json:"max_level,omitempty"`
ElementCount int64 `protobuf:"varint,5,opt,name=element_count,json=elementCount,proto3" json:"element_count,omitempty"`
Eps float64 `protobuf:"fixed64,7,opt,name=eps,proto3" json:"eps,omitempty"`
} }
func (x *SkipListProto) Reset() { func (x *SkipListProto) Reset() {
@ -98,20 +96,6 @@ func (x *SkipListProto) GetMaxLevel() int32 {
return 0 return 0
} }
func (x *SkipListProto) GetElementCount() int64 {
if x != nil {
return x.ElementCount
}
return 0
}
func (x *SkipListProto) GetEps() float64 {
if x != nil {
return x.Eps
}
return 0
}
type SkipListElementReference struct { type SkipListElementReference struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
@ -172,11 +156,12 @@ type SkipListElement struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
Next []*SkipListElementReference `protobuf:"bytes,2,rep,name=next,proto3" json:"next,omitempty"` Next []*SkipListElementReference `protobuf:"bytes,2,rep,name=next,proto3" json:"next,omitempty"`
Level int32 `protobuf:"varint,3,opt,name=level,proto3" json:"level,omitempty"` Level int32 `protobuf:"varint,3,opt,name=level,proto3" json:"level,omitempty"`
Values [][]byte `protobuf:"bytes,4,rep,name=values,proto3" json:"values,omitempty"` Key []byte `protobuf:"bytes,4,opt,name=key,proto3" json:"key,omitempty"`
Prev *SkipListElementReference `protobuf:"bytes,5,opt,name=prev,proto3" json:"prev,omitempty"` Value []byte `protobuf:"bytes,5,opt,name=value,proto3" json:"value,omitempty"`
Prev *SkipListElementReference `protobuf:"bytes,6,opt,name=prev,proto3" json:"prev,omitempty"`
} }
func (x *SkipListElement) Reset() { func (x *SkipListElement) Reset() {
@ -232,9 +217,16 @@ func (x *SkipListElement) GetLevel() int32 {
return 0 return 0
} }
func (x *SkipListElement) GetValues() [][]byte { func (x *SkipListElement) GetKey() []byte {
if x != nil { if x != nil {
return x.Values return x.Key
}
return nil
}
func (x *SkipListElement) GetValue() []byte {
if x != nil {
return x.Value
} }
return nil return nil
} }
@ -250,7 +242,7 @@ var File_skiplist_proto protoreflect.FileDescriptor
var file_skiplist_proto_rawDesc = []byte{ var file_skiplist_proto_rawDesc = []byte{
0x0a, 0x0e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x0a, 0x0e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x12, 0x08, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x22, 0x91, 0x02, 0x0a, 0x0d, 0x53, 0x12, 0x08, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x22, 0xda, 0x01, 0x0a, 0x0d, 0x53,
0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x74, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x45, 0x0a, 0x0c, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x74, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x45, 0x0a, 0x0c,
0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x73, 0x18, 0x01, 0x20, 0x03, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x73, 0x18, 0x01, 0x20, 0x03,
0x28, 0x0b, 0x32, 0x22, 0x2e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x2e, 0x53, 0x6b, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x2e, 0x53, 0x6b,
@ -264,32 +256,29 @@ var file_skiplist_proto_rawDesc = []byte{
0x77, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0b, 0x6d, 0x77, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0b, 0x6d,
0x61, 0x78, 0x4e, 0x65, 0x77, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x1b, 0x0a, 0x09, 0x6d, 0x61, 0x61, 0x78, 0x4e, 0x65, 0x77, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x1b, 0x0a, 0x09, 0x6d, 0x61,
0x78, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x6d, 0x78, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x6d,
0x61, 0x78, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x23, 0x0a, 0x0d, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x61, 0x78, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0x55, 0x0a, 0x18, 0x53, 0x6b, 0x69, 0x70, 0x4c,
0x6e, 0x74, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0c, 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65,
0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6e, 0x63, 0x65, 0x12, 0x27, 0x0a, 0x0f, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x5f, 0x70,
0x65, 0x70, 0x73, 0x18, 0x07, 0x20, 0x01, 0x28, 0x01, 0x52, 0x03, 0x65, 0x70, 0x73, 0x22, 0x55, 0x6f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0e, 0x65, 0x6c,
0x0a, 0x18, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x50, 0x6f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x12, 0x10, 0x0a, 0x03,
0x74, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65, 0x12, 0x27, 0x0a, 0x0f, 0x65, 0x6c, 0x6b, 0x65, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0xcf,
0x65, 0x6d, 0x65, 0x6e, 0x74, 0x5f, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x0a, 0x0f, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65,
0x01, 0x28, 0x03, 0x52, 0x0e, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x50, 0x6f, 0x69, 0x6e, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02,
0x74, 0x65, 0x72, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x69, 0x64, 0x12, 0x36, 0x0a, 0x04, 0x6e, 0x65, 0x78, 0x74, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b,
0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0xbf, 0x01, 0x0a, 0x0f, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x32, 0x22, 0x2e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x2e, 0x53, 0x6b, 0x69, 0x70,
0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x4c, 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x66, 0x65, 0x72,
0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x36, 0x0a, 0x04, 0x6e, 0x65, 0x78, 0x65, 0x6e, 0x63, 0x65, 0x52, 0x04, 0x6e, 0x65, 0x78, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x65,
0x74, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c,
0x73, 0x74, 0x2e, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x03, 0x6b,
0x6e, 0x74, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65, 0x52, 0x04, 0x6e, 0x65, 0x78, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28,
0x74, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x36, 0x0a, 0x04, 0x70, 0x72, 0x65, 0x76,
0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73,
0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x12, 0x74, 0x2e, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e,
0x36, 0x0a, 0x04, 0x70, 0x72, 0x65, 0x76, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x74, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65, 0x52, 0x04, 0x70, 0x72, 0x65, 0x76,
0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x2e, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x42, 0x33, 0x5a, 0x31, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x63,
0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x68, 0x72, 0x69, 0x73, 0x6c, 0x75, 0x73, 0x66, 0x2f, 0x73, 0x65, 0x61, 0x77, 0x65, 0x65, 0x64,
0x65, 0x52, 0x04, 0x70, 0x72, 0x65, 0x76, 0x42, 0x33, 0x5a, 0x31, 0x67, 0x69, 0x74, 0x68, 0x75, 0x66, 0x73, 0x2f, 0x77, 0x65, 0x65, 0x64, 0x2f, 0x75, 0x74, 0x69, 0x6c, 0x2f, 0x73, 0x6b, 0x69,
0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x63, 0x68, 0x72, 0x69, 0x73, 0x6c, 0x75, 0x73, 0x66, 0x2f, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x73, 0x65, 0x61, 0x77, 0x65, 0x65, 0x64, 0x66, 0x73, 0x2f, 0x77, 0x65, 0x65, 0x64, 0x2f, 0x75,
0x74, 0x69, 0x6c, 0x2f, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

View file

@ -9,8 +9,6 @@ message SkipListProto {
repeated SkipListElementReference end_levels = 2; repeated SkipListElementReference end_levels = 2;
int32 max_new_level = 3; int32 max_new_level = 3;
int32 max_level = 4; int32 max_level = 4;
int64 element_count = 5;
double eps = 7;
} }
message SkipListElementReference { message SkipListElementReference {
@ -22,6 +20,7 @@ message SkipListElement {
int64 id = 1; int64 id = 1;
repeated SkipListElementReference next = 2; repeated SkipListElementReference next = 2;
int32 level = 3; int32 level = 3;
repeated bytes values = 4; bytes key = 4;
SkipListElementReference prev = 5; bytes value = 5;
SkipListElementReference prev = 6;
} }

View file

@ -18,7 +18,7 @@ func TestInsertAndFind(t *testing.T) {
var list *SkipList var list *SkipList
var listPointer *SkipList var listPointer *SkipList
listPointer.Insert(k0) listPointer.Insert(k0, k0)
if _, ok := listPointer.Find(k0); ok { if _, ok := listPointer.Find(k0); ok {
t.Fail() t.Fail()
} }
@ -34,7 +34,7 @@ func TestInsertAndFind(t *testing.T) {
// Test at the beginning of the list. // Test at the beginning of the list.
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(maxN-i)) key := []byte(strconv.Itoa(maxN-i))
list.Insert(key) list.Insert(key, key)
} }
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(maxN-i)) key := []byte(strconv.Itoa(maxN-i))
@ -48,7 +48,7 @@ func TestInsertAndFind(t *testing.T) {
// Test at the end of the list. // Test at the end of the list.
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(i)) key := []byte(strconv.Itoa(i))
list.Insert(key) list.Insert(key, key)
} }
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(i)) key := []byte(strconv.Itoa(i))
@ -62,17 +62,17 @@ func TestInsertAndFind(t *testing.T) {
rList := rand.Perm(maxN) rList := rand.Perm(maxN)
for _, e := range rList { for _, e := range rList {
key := []byte(strconv.Itoa(e)) key := []byte(strconv.Itoa(e))
println("insert", e) // println("insert", e)
list.Insert(key) list.Insert(key, key)
} }
for _, e := range rList { for _, e := range rList {
key := []byte(strconv.Itoa(e)) key := []byte(strconv.Itoa(e))
println("find", e) // println("find", e)
if _, ok := list.Find(key); !ok { if _, ok := list.Find(key); !ok {
t.Fail() t.Fail()
} }
} }
println("print list") // println("print list")
list.println() list.println()
} }
@ -97,7 +97,7 @@ func TestDelete(t *testing.T) {
t.Fail() t.Fail()
} }
list.Insert(k0) list.Insert(k0, k0)
list.Delete(k0) list.Delete(k0)
if !list.IsEmpty() { if !list.IsEmpty() {
t.Fail() t.Fail()
@ -105,7 +105,7 @@ func TestDelete(t *testing.T) {
// Delete elements at the beginning of the list. // Delete elements at the beginning of the list.
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
list.Insert(Element(i)) list.Insert(Element(i), Element(i))
} }
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
list.Delete(Element(i)) list.Delete(Element(i))
@ -117,7 +117,7 @@ func TestDelete(t *testing.T) {
list = New() list = New()
// Delete elements at the end of the list. // Delete elements at the end of the list.
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
list.Insert(Element(i)) list.Insert(Element(i), Element(i))
} }
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
list.Delete(Element(maxN - i - 1)) list.Delete(Element(maxN - i - 1))
@ -130,7 +130,7 @@ func TestDelete(t *testing.T) {
// Delete elements at random positions in the list. // Delete elements at random positions in the list.
rList := rand.Perm(maxN) rList := rand.Perm(maxN)
for _, e := range rList { for _, e := range rList {
list.Insert(Element(e)) list.Insert(Element(e), Element(e))
} }
for _, e := range rList { for _, e := range rList {
list.Delete(Element(e)) list.Delete(Element(e))
@ -144,7 +144,7 @@ func TestNext(t *testing.T) {
list := New() list := New()
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
list.Insert(Element(i)) list.Insert(Element(i), Element(i))
} }
smallest := list.GetSmallestNode() smallest := list.GetSmallestNode()
@ -155,7 +155,7 @@ func TestNext(t *testing.T) {
for node != largest { for node != largest {
node = list.Next(node) node = list.Next(node)
// Must always be incrementing here! // Must always be incrementing here!
if bytes.Compare(node.Values[0], lastNode.Values[0]) <= 0 { if bytes.Compare(node.Key, lastNode.Key) <= 0 {
t.Fail() t.Fail()
} }
// Next.Prev must always point to itself! // Next.Prev must always point to itself!
@ -174,7 +174,7 @@ func TestPrev(t *testing.T) {
list := New() list := New()
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
list.Insert(Element(i)) list.Insert(Element(i), Element(i))
} }
smallest := list.GetSmallestNode() smallest := list.GetSmallestNode()
@ -185,7 +185,7 @@ func TestPrev(t *testing.T) {
for node != smallest { for node != smallest {
node = list.Prev(node) node = list.Prev(node)
// Must always be incrementing here! // Must always be incrementing here!
if bytes.Compare(node.Values[0], lastNode.Values[0]) >= 0 { if bytes.Compare(node.Key, lastNode.Key) >= 0 {
t.Fail() t.Fail()
} }
// Next.Prev must always point to itself! // Next.Prev must always point to itself!
@ -200,18 +200,6 @@ func TestPrev(t *testing.T) {
} }
} }
func TestGetNodeCount(t *testing.T) {
list := New()
for i := 0; i < maxN; i++ {
list.Insert(Element(i))
}
if list.GetNodeCount() != maxN {
t.Fail()
}
}
func TestFindGreaterOrEqual(t *testing.T) { func TestFindGreaterOrEqual(t *testing.T) {
maxNumber := maxN * 100 maxNumber := maxN * 100
@ -227,21 +215,21 @@ func TestFindGreaterOrEqual(t *testing.T) {
list = New() list = New()
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
list.Insert(Element(rand.Intn(maxNumber))) list.Insert(Element(rand.Intn(maxNumber)), Element(i))
} }
for i := 0; i < maxN; i++ { for i := 0; i < maxN; i++ {
key := Element(rand.Intn(maxNumber)) key := Element(rand.Intn(maxNumber))
if v, ok := list.FindGreaterOrEqual(key); ok { if v, ok := list.FindGreaterOrEqual(key); ok {
// if f is v should be bigger than the element before // if f is v should be bigger than the element before
if bytes.Compare(v.Prev.Key, key) >= 0 { if v.Prev != nil && bytes.Compare(v.Prev.Key, key) >= 0 {
fmt.Printf("PrevV: %s\n key: %s\n\n", string(v.Prev.Key), string(key)) fmt.Printf("PrevV: %s\n key: %s\n\n", string(v.Prev.Key), string(key))
t.Fail() t.Fail()
} }
// v should be bigger or equal to f // v should be bigger or equal to f
// If we compare directly, we get an equal key with a difference on the 10th decimal point, which fails. // If we compare directly, we get an equal key with a difference on the 10th decimal point, which fails.
if bytes.Compare(v.Values[0], key) < 0 { if bytes.Compare(v.Key, key) < 0 {
fmt.Printf("v: %s\n key: %s\n\n", string(v.Values[0]), string(key)) fmt.Printf("v: %s\n key: %s\n\n", string(v.Key), string(key))
t.Fail() t.Fail()
} }
} else { } else {