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
package com.sibirjak.asperform.collectiontests.examples.set {
import flash.utils.Dictionary;

public class OrderedSetRaw {

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

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

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

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

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

size++;
}

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

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

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

size++;
}

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

public function remove(item : *) : void {
var node : Node;

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

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;

remove(first.data);
}

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

remove(last.data);
}

}
}

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

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

Change log

r12 by jens.struwe on Apr 29, 2010   Diff
[DEV] Ordered set example
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 2323 bytes, 120 lines
Powered by Google Project Hosting