Advertisement
Drakim

SkipList

Apr 12th, 2012
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. class SkipList<T> #if (flash||cpp) implements haxe.rtti.Generic #end {
  4.   public var bookmarks:Vector<SkipCell<T>>;
  5.   public var first:SkipCell<T>;
  6.   public function new(_length:Int) {
  7.     bookmarks = new Vector<SkipCell<T>>(_length,true);
  8.     var skipcell:SkipCell<T> = null;
  9.     var prev:SkipCell<T> = null;
  10.     while(--_length > -1) {
  11.       skipcell = new SkipCell<T>();
  12.       skipcell.isbookmark = true;
  13.       skipcell.bookmark = _length;
  14.       skipcell.next = prev;
  15.       bookmarks[_length] = skipcell;
  16.       prev = skipcell;
  17.     }
  18.     first = skipcell;
  19.   }
  20.   public inline function add(_value:T,_index:Int):SkipCell<T> {
  21.     var mybookmark = getBookmark(_index);
  22.     var myskipcell = new SkipCell<T>();
  23.     myskipcell.value = _value;
  24.     myskipcell.next = mybookmark.next;
  25.     mybookmark.next = myskipcell;
  26.     return myskipcell;
  27.   }
  28.   public inline function getBookmark(_index:Int):SkipCell<T> {
  29.     return bookmarks[_index];
  30.   }
  31.   public inline function clear():Void {
  32.     var i:Int = 0;
  33.     while(++i < Std.int(bookmarks.length)) {
  34.       bookmarks[i].next = bookmarks[i+1];
  35.     }
  36.     bookmarks[bookmarks.length-1].next = null;
  37.   }
  38.   public inline function toString():String {
  39.     var result:String = "[bookmark_0],";
  40.     var loop = first.next;
  41.     if(loop != null) {
  42.       do {
  43.         if(loop.isbookmark) {
  44.           result += ",";
  45.           result += "[bookmark_" + loop.bookmark + "]";
  46.         } else {
  47.           result += ",";
  48.           result += loop.value;
  49.         }
  50.       } while((loop = loop.next) != null);
  51.     }
  52.     return result;
  53.   }
  54. }
  55.  
  56.  
  57. class SkipCell<T> #if (flash||cpp) implements haxe.rtti.Generic #end {
  58.   public var bookmark:Int;
  59.   public var isbookmark:Bool;
  60.   public var next:SkipCell<T>;
  61.   public var value:T;
  62.   public function new() {}
  63.   public inline function getNext(_skipmode:SkipMode):SkipCell<T> {
  64.     return switch(_skipmode) {
  65.     case NORMAL: next;
  66.     case STOP: (next!=null)?(next.bookmark==-1)?next:null:null;
  67.     case SKIP:
  68.       var loop:SkipCell<T> = this;
  69.       var result:SkipCell<T> = null;
  70.       while(loop != null) {if(loop.bookmark == -1) {result=loop.next; break;}loop = loop.next;}
  71.       result;
  72.     }
  73.   }
  74. }
  75.  
  76. enum SkipMode {
  77.   NORMAL;
  78.   STOP;
  79.   SKIP;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement