Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SkipList<T> #if (flash||cpp) implements haxe.rtti.Generic #end {
- public var bookmarks:Vector<SkipCell<T>>;
- public var first:SkipCell<T>;
- public function new(_length:Int) {
- bookmarks = new Vector<SkipCell<T>>(_length,true);
- var skipcell:SkipCell<T> = null;
- var prev:SkipCell<T> = null;
- while(--_length > -1) {
- skipcell = new SkipCell<T>();
- skipcell.isbookmark = true;
- skipcell.bookmark = _length;
- skipcell.next = prev;
- bookmarks[_length] = skipcell;
- prev = skipcell;
- }
- first = skipcell;
- }
- public inline function add(_value:T,_index:Int):SkipCell<T> {
- var mybookmark = getBookmark(_index);
- var myskipcell = new SkipCell<T>();
- myskipcell.value = _value;
- myskipcell.next = mybookmark.next;
- mybookmark.next = myskipcell;
- return myskipcell;
- }
- public inline function getBookmark(_index:Int):SkipCell<T> {
- return bookmarks[_index];
- }
- public inline function clear():Void {
- var i:Int = 0;
- while(++i < Std.int(bookmarks.length)) {
- bookmarks[i].next = bookmarks[i+1];
- }
- bookmarks[bookmarks.length-1].next = null;
- }
- public inline function toString():String {
- var result:String = "[bookmark_0],";
- var loop = first.next;
- if(loop != null) {
- do {
- if(loop.isbookmark) {
- result += ",";
- result += "[bookmark_" + loop.bookmark + "]";
- } else {
- result += ",";
- result += loop.value;
- }
- } while((loop = loop.next) != null);
- }
- return result;
- }
- }
- class SkipCell<T> #if (flash||cpp) implements haxe.rtti.Generic #end {
- public var bookmark:Int;
- public var isbookmark:Bool;
- public var next:SkipCell<T>;
- public var value:T;
- public function new() {}
- public inline function getNext(_skipmode:SkipMode):SkipCell<T> {
- return switch(_skipmode) {
- case NORMAL: next;
- case STOP: (next!=null)?(next.bookmark==-1)?next:null:null;
- case SKIP:
- var loop:SkipCell<T> = this;
- var result:SkipCell<T> = null;
- while(loop != null) {if(loop.bookmark == -1) {result=loop.next; break;}loop = loop.next;}
- result;
- }
- }
- }
- enum SkipMode {
- NORMAL;
- STOP;
- SKIP;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement