My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.sibirjak.asperform.collectiontests.examples.map {
import flash.utils.Dictionary;

public class OrderedMapRaw {

public var map : Dictionary;
public var stringMap : Object;
public var first : Node;
public var last : Node;
public var size : uint;

public function OrderedMapRaw() {
map = new Dictionary();
stringMap = new Object();
}

public function addFirst(key : *, item : *) : void {
var node : Node = new Node(key, item);

if (key is String) {
if (stringMap[key] !== undefined) removeKey(key);
stringMap[key] = node;
} else {
if (map[key] !== undefined) removeKey(key);
map[key] = node;
}

if (first) {
first.left = node;
first.left.right = first;
first = first.left;
} else {
first = last = node;
}

size++;
}

public function addLast(key : *, item : *) : void {
var node : Node = new Node(key, item);

if (key is String) {
if (stringMap[key] !== undefined) removeKey(key);
stringMap[key] = node;
} else {
if (map[key] !== undefined) removeKey(key);
map[key] = node;
}

if (last) {
last.right = node;
last.right.left = last;
last = last.right;
} else {
first = last = node;
}

size++;
}

public function hasKey(key : *) : Boolean {
if (key is String) {
return stringMap[key] !== undefined;
} else {
return map[key] !== undefined;
}
}

public function removeKey(key : *) : void {
var node : Node;

if (key is String) {
if (stringMap[key] === undefined) return;
node = stringMap[key];
delete stringMap[key];
} else {
if (map[key] === undefined) return;
node = map[key];
delete map[key];
}

if (node.left) {
node.left.right = node.right;
} else { // has been the first
first = node.right;
}

if (node.right) {
node.right.left = node.left;
} else { // has been the last
last = node.left;
}

size--;
}

public function removeFirst() : void {
if (!first) return;

removeKey(first.key);
}

public function removeLast() : void {
if (!last) return;

removeKey(last.key);
}

}
}

internal class Node {
public var left : Node;
public var right : Node;
public var key : *;
public var data : *;

public function Node(theKey : *, theData : *) {
key = theKey;
data = theData;
}
}

Change log

r9 by jens.struwe on Apr 28, 2010   Diff
[DEV] Ordered map and treap example
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 2372 bytes, 122 lines
Powered by Google Project Hosting