| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
76 |
| 15 |
package skiplist |
| 16 |
|
| 17 |
import ( |
| 18 |
"math/rand" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
const p = 0.25 |
| 29 |
|
| 30 |
const DefaultMaxLevel = 32 |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
type node struct { |
| 35 |
forward []*node |
| 36 |
backward *node |
| 37 |
key, value interface{} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
func (n *node) next() *node { |
| 42 |
if len(n.forward) == 0 { |
| 43 |
return nil |
| 44 |
} |
| 45 |
return n.forward[0] |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func (n *node) previous() *node { |
| 50 |
return n.backward |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
func (n *node) hasNext() bool { |
| 55 |
return n.next() != nil |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
func (n *node) hasPrevious() bool { |
| 60 |
return n.previous() != nil |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
type SkipList struct { |
| 75 |
lessThan func(l, r interface{}) bool |
| 76 |
header *node |
| 77 |
footer *node |
| 78 |
length int |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
MaxLevel int |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
func (s *SkipList) Len() int { |
| 96 |
return s.length |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
type Iterator interface { |
| 105 |
|
| 106 |
|
| 107 |
Next() (ok bool) |
| 108 |
|
| 109 |
|
| 110 |
Previous() (ok bool) |
| 111 |
|
| 112 |
Key() interface{} |
| 113 |
|
| 114 |
Value() interface{} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
Seek(key interface{}) (ok bool) |
| 121 |
|
| 122 |
|
| 123 |
Close() |
| 124 |
} |
| 125 |
|
| 126 |
type iter struct { |
| 127 |
current *node |
| 128 |
key interface{} |
| 129 |
list *SkipList |
| 130 |
value interface{} |
| 131 |
} |
| 132 |
|
| 133 |
func (i iter) Key() interface{} { |
| 134 |
return i.key |
| 135 |
} |
| 136 |
|
| 137 |
func (i iter) Value() interface{} { |
| 138 |
return i.value |
| 139 |
} |
| 140 |
|
| 141 |
func (i *iter) Next() bool { |
| 142 |
if !i.current.hasNext() { |
| 143 |
return false |
| 144 |
} |
| 145 |
|
| 146 |
i.current = i.current.next() |
| 147 |
i.key = i.current.key |
| 148 |
i.value = i.current.value |
| 149 |
|
| 150 |
return true |
| 151 |
} |
| 152 |
|
| 153 |
func (i *iter) Previous() bool { |
| 154 |
if !i.current.hasPrevious() { |
| 155 |
return false |
| 156 |
} |
| 157 |
|
| 158 |
i.current = i.current.previous() |
| 159 |
i.key = i.current.key |
| 160 |
i.value = i.current.value |
| 161 |
|
| 162 |
return true |
| 163 |
} |
| 164 |
|
| 165 |
func (i *iter) Seek(key interface{}) (ok bool) { |
| 166 |
current := i.current |
| 167 |
list := i.list |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
if current == nil { |
| 172 |
current = list.header |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
if current.key != nil && list.lessThan(key, current.key) { |
| 179 |
current = list.header |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
if current.backward == nil { |
| 185 |
current = list.header |
| 186 |
} else { |
| 187 |
current = current.backward |
| 188 |
} |
| 189 |
|
| 190 |
current = list.getPath(current, nil, key) |
| 191 |
|
| 192 |
if current == nil { |
| 193 |
return |
| 194 |
} |
| 195 |
|
| 196 |
i.current = current |
| 197 |
i.key = current.key |
| 198 |
i.value = current.value |
| 199 |
|
| 200 |
return true |
| 201 |
} |
| 202 |
|
| 203 |
func (i *iter) Close() { |
| 204 |
i.key = nil |
| 205 |
i.value = nil |
| 206 |
i.current = nil |
| 207 |
i.list = nil |
| 208 |
} |
| 209 |
|
| 210 |
type rangeIterator struct { |
| 211 |
iter |
| 212 |
upperLimit interface{} |
| 213 |
lowerLimit interface{} |
| 214 |
} |
| 215 |
|
| 216 |
func (i *rangeIterator) Next() bool { |
| 217 |
if !i.current.hasNext() { |
| 218 |
return false |
| 219 |
} |
| 220 |
|
| 221 |
next := i.current.next() |
| 222 |
|
| 223 |
if !i.list.lessThan(next.key, i.upperLimit) { |
| 224 |
return false |
| 225 |
} |
| 226 |
|
| 227 |
i.current = i.current.next() |
| 228 |
i.key = i.current.key |
| 229 |
i.value = i.current.value |
| 230 |
return true |
| 231 |
} |
| 232 |
|
| 233 |
func (i *rangeIterator) Previous() bool { |
| 234 |
if !i.current.hasPrevious() { |
| 235 |
return false |
| 236 |
} |
| 237 |
|
| 238 |
previous := i.current.previous() |
| 239 |
|
| 240 |
if i.list.lessThan(previous.key, i.lowerLimit) { |
| 241 |
return false |
| 242 |
} |
| 243 |
|
| 244 |
i.current = i.current.previous() |
| 245 |
i.key = i.current.key |
| 246 |
i.value = i.current.value |
| 247 |
return true |
| 248 |
} |
| 249 |
|
| 250 |
func (i *rangeIterator) Seek(key interface{}) (ok bool) { |
| 251 |
if i.list.lessThan(key, i.lowerLimit) { |
| 252 |
return |
| 253 |
} else if !i.list.lessThan(key, i.upperLimit) { |
| 254 |
return |
| 255 |
} |
| 256 |
|
| 257 |
return i.iter.Seek(key) |
| 258 |
} |
| 259 |
|
| 260 |
func (i *rangeIterator) Close() { |
| 261 |
i.iter.Close() |
| 262 |
i.upperLimit = nil |
| 263 |
i.lowerLimit = nil |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
func (s *SkipList) Iterator() Iterator { |
| 268 |
return &iter{ |
| 269 |
current: s.header, |
| 270 |
list: s, |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
func (s *SkipList) Seek(key interface{}) Iterator { |
| 277 |
current := s.getPath(s.header, nil, key) |
| 278 |
if current == nil { |
| 279 |
return nil |
| 280 |
} |
| 281 |
|
| 282 |
return &iter{ |
| 283 |
current: current, |
| 284 |
key: current.key, |
| 285 |
list: s, |
| 286 |
value: current.value, |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
func (s *SkipList) SeekToFirst() Iterator { |
| 293 |
if s.length == 0 { |
| 294 |
return nil |
| 295 |
} |
| 296 |
|
| 297 |
current := s.header.next() |
| 298 |
|
| 299 |
return &iter{ |
| 300 |
current: current, |
| 301 |
key: current.key, |
| 302 |
list: s, |
| 303 |
value: current.value, |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
func (s *SkipList) SeekToLast() Iterator { |
| 310 |
current := s.footer |
| 311 |
if current == nil { |
| 312 |
return nil |
| 313 |
} |
| 314 |
|
| 315 |
return &iter{ |
| 316 |
current: current, |
| 317 |
key: current.key, |
| 318 |
list: s, |
| 319 |
value: current.value, |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
func (s *SkipList) Range(from, to interface{}) Iterator { |
| 327 |
start := s.getPath(s.header, nil, from) |
| 328 |
return &rangeIterator{ |
| 329 |
iter: iter{ |
| 330 |
current: &node{ |
| 331 |
forward: []*node{start}, |
| 332 |
backward: start, |
| 333 |
}, |
| 334 |
list: s, |
| 335 |
}, |
| 336 |
upperLimit: to, |
| 337 |
lowerLimit: from, |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
func (s *SkipList) level() int { |
| 342 |
return len(s.header.forward) - 1 |
| 343 |
} |
| 344 |
|
| 345 |
func maxInt(x, y int) int { |
| 346 |
if x > y { |
| 347 |
return x |
| 348 |
} |
| 349 |
return y |
| 350 |
} |
| 351 |
|
| 352 |
func (s *SkipList) effectiveMaxLevel() int { |
| 353 |
return maxInt(s.level(), s.MaxLevel) |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
func (s SkipList) randomLevel() (n int) { |
| 358 |
for n = 0; n < s.effectiveMaxLevel() && rand.Float64() < p; n++ { |
| 359 |
} |
| 360 |
return |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
func (s *SkipList) Get(key interface{}) (value interface{}, ok bool) { |
| 367 |
candidate := s.getPath(s.header, nil, key) |
| 368 |
|
| 369 |
if candidate == nil || candidate.key != key { |
| 370 |
return nil, false |
| 371 |
} |
| 372 |
|
| 373 |
return candidate.value, true |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
func (s *SkipList) GetGreaterOrEqual(min interface{}) (actualKey, value interface{}, ok bool) { |
| 380 |
candidate := s.getPath(s.header, nil, min) |
| 381 |
|
| 382 |
if candidate != nil { |
| 383 |
return candidate.key, candidate.value, true |
| 384 |
} |
| 385 |
return nil, nil, false |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
func (s *SkipList) getPath(current *node, update []*node, key interface{}) *node { |
| 394 |
depth := len(current.forward) - 1 |
| 395 |
|
| 396 |
for i := depth; i >= 0; i-- { |
| 397 |
for current.forward[i] != nil && s.lessThan(current.forward[i].key, key) { |
| 398 |
current = current.forward[i] |
| 399 |
} |
| 400 |
if update != nil { |
| 401 |
update[i] = current |
| 402 |
} |
| 403 |
} |
| 404 |
return current.next() |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
func (s *SkipList) Set(key, value interface{}) { |
| 409 |
if key == nil { |
| 410 |
panic("goskiplist: nil keys are not supported") |
| 411 |
} |
| 412 |
|
| 413 |
update := make([]*node, s.level()+1, s.effectiveMaxLevel()+1) |
| 414 |
candidate := s.getPath(s.header, update, key) |
| 415 |
|
| 416 |
if candidate != nil && candidate.key == key { |
| 417 |
candidate.value = value |
| 418 |
return |
| 419 |
} |
| 420 |
|
| 421 |
newLevel := s.randomLevel() |
| 422 |
|
| 423 |
if currentLevel := s.level(); newLevel > currentLevel { |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
for i := currentLevel + 1; i <= newLevel; i++ { |
| 428 |
update = append(update, s.header) |
| 429 |
s.header.forward = append(s.header.forward, nil) |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
newNode := &node{ |
| 434 |
forward: make([]*node, newLevel+1, s.effectiveMaxLevel()+1), |
| 435 |
key: key, |
| 436 |
value: value, |
| 437 |
} |
| 438 |
|
| 439 |
if previous := update[0]; previous.key != nil { |
| 440 |
newNode.backward = previous |
| 441 |
} |
| 442 |
|
| 443 |
for i := 0; i <= newLevel; i++ { |
| 444 |
newNode.forward[i] = update[i].forward[i] |
| 445 |
update[i].forward[i] = newNode |
| 446 |
} |
| 447 |
|
| 448 |
s.length++ |
| 449 |
|
| 450 |
if newNode.forward[0] != nil { |
| 451 |
if newNode.forward[0].backward != newNode { |
| 452 |
newNode.forward[0].backward = newNode |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
if s.footer == nil || s.lessThan(s.footer.key, key) { |
| 457 |
s.footer = newNode |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
func (s *SkipList) Delete(key interface{}) (value interface{}, ok bool) { |
| 465 |
if key == nil { |
| 466 |
panic("goskiplist: nil keys are not supported") |
| 467 |
} |
| 468 |
update := make([]*node, s.level()+1, s.effectiveMaxLevel()) |
| 469 |
candidate := s.getPath(s.header, update, key) |
| 470 |
|
| 471 |
if candidate == nil || candidate.key != key { |
| 472 |
return nil, false |
| 473 |
} |
| 474 |
|
| 475 |
previous := candidate.backward |
| 476 |
if s.footer == candidate { |
| 477 |
s.footer = previous |
| 478 |
} |
| 479 |
|
| 480 |
next := candidate.next() |
| 481 |
if next != nil { |
| 482 |
next.backward = previous |
| 483 |
} |
| 484 |
|
| 485 |
for i := 0; i <= s.level() && update[i].forward[i] == candidate; i++ { |
| 486 |
update[i].forward[i] = candidate.forward[i] |
| 487 |
} |
| 488 |
|
| 489 |
for s.level() > 0 && s.header.forward[s.level()] == nil { |
| 490 |
s.header.forward = s.header.forward[:s.level()] |
| 491 |
} |
| 492 |
s.length-- |
| 493 |
|
| 494 |
return candidate.value, true |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
func NewCustomMap(lessThan func(l, r interface{}) bool) *SkipList { |
| 501 |
return &SkipList{ |
| 502 |
lessThan: lessThan, |
| 503 |
header: &node{ |
| 504 |
forward: []*node{nil}, |
| 505 |
}, |
| 506 |
MaxLevel: DefaultMaxLevel, |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
type Ordered interface { |
| 515 |
LessThan(other Ordered) bool |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
func New() *SkipList { |
| 522 |
comparator := func(left, right interface{}) bool { |
| 523 |
return left.(Ordered).LessThan(right.(Ordered)) |
| 524 |
} |
| 525 |
return NewCustomMap(comparator) |
| 526 |
|
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
func NewIntMap() *SkipList { |
| 531 |
return NewCustomMap(func(l, r interface{}) bool { |
| 532 |
return l.(int) < r.(int) |
| 533 |
}) |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
func NewStringMap() *SkipList { |
| 538 |
return NewCustomMap(func(l, r interface{}) bool { |
| 539 |
return l.(string) < r.(string) |
| 540 |
}) |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
type Set struct { |
| 556 |
skiplist SkipList |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
func NewSet() *Set { |
| 561 |
comparator := func(left, right interface{}) bool { |
| 562 |
return left.(Ordered).LessThan(right.(Ordered)) |
| 563 |
} |
| 564 |
return NewCustomSet(comparator) |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
func NewCustomSet(lessThan func(l, r interface{}) bool) *Set { |
| 571 |
return &Set{skiplist: SkipList{ |
| 572 |
lessThan: lessThan, |
| 573 |
header: &node{ |
| 574 |
forward: []*node{nil}, |
| 575 |
}, |
| 576 |
MaxLevel: DefaultMaxLevel, |
| 577 |
}} |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
func NewIntSet() *Set { |
| 582 |
return NewCustomSet(func(l, r interface{}) bool { |
| 583 |
return l.(int) < r.(int) |
| 584 |
}) |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
func NewStringSet() *Set { |
| 589 |
return NewCustomSet(func(l, r interface{}) bool { |
| 590 |
return l.(string) < r.(string) |
| 591 |
}) |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
func (s *Set) Add(key interface{}) { |
| 596 |
s.skiplist.Set(key, nil) |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
func (s *Set) Remove(key interface{}) (ok bool) { |
| 602 |
_, ok = s.skiplist.Delete(key) |
| 603 |
return ok |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
func (s *Set) Len() int { |
| 608 |
return s.skiplist.Len() |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
func (s *Set) Contains(key interface{}) bool { |
| 613 |
_, ok := s.skiplist.Get(key) |
| 614 |
return ok |
| 615 |
} |
| 616 |
|
| 617 |
func (s *Set) Iterator() Iterator { |
| 618 |
return s.skiplist.Iterator() |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
func (s *Set) Range(from, to interface{}) Iterator { |
| 624 |
return s.skiplist.Range(from, to) |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
func (s *Set) SetMaxLevel(newMaxLevel int) { |
| 629 |
s.skiplist.MaxLevel = newMaxLevel |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
func (s *Set) GetMaxLevel() int { |
| 634 |
return s.skiplist.MaxLevel |
| 635 |
} |