Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.collections.SortedList");
14
dojo.require("dojo.collections.Collections");
15
dojo.collections.SortedList = function (dictionary) {
16
	var _this = this;
17
	var items = {};
18
	var q = [];
19
	var sorter = function (a, b) {
20
		if (a.key > b.key) {
21
			return 1;
22
		}
23
		if (a.key < b.key) {
24
			return -1;
25
		}
26
		return 0;
27
	};
28
	var build = function () {
29
		q = [];
30
		var e = _this.getIterator();
31
		while (!e.atEnd()) {
32
			q.push(e.get());
33
		}
34
		q.sort(sorter);
35
	};
36
	var testObject = {};
37
	this.count = q.length;
38
	this.add = function (k, v) {
39
		if (!items[k]) {
40
			items[k] = new dojo.collections.DictionaryEntry(k, v);
41
			this.count = q.push(items[k]);
42
			q.sort(sorter);
43
		}
44
	};
45
	this.clear = function () {
46
		items = {};
47
		q = [];
48
		this.count = q.length;
49
	};
50
	this.clone = function () {
51
		return new dojo.collections.SortedList(this);
52
	};
53
	this.contains = this.containsKey = function (k) {
54
		if (testObject[k]) {
55
			return false;
56
		}
57
		return (items[k] != null);
58
	};
59
	this.containsValue = function (o) {
60
		var e = this.getIterator();
61
		while (!e.atEnd()) {
62
			var item = e.get();
63
			if (item.value == o) {
64
				return true;
65
			}
66
		}
67
		return false;
68
	};
69
	this.copyTo = function (arr, i) {
70
		var e = this.getIterator();
71
		var idx = i;
72
		while (!e.atEnd()) {
73
			arr.splice(idx, 0, e.get());
74
			idx++;
75
		}
76
	};
77
	this.entry = function (k) {
78
		return items[k];
79
	};
80
	this.forEach = function (fn, scope) {
81
		var s = scope || dj_global;
82
		if (Array.forEach) {
83
			Array.forEach(q, fn, s);
84
		} else {
85
			for (var i = 0; i < q.length; i++) {
86
				fn.call(s, q[i], i, q);
87
			}
88
		}
89
	};
90
	this.getByIndex = function (i) {
91
		return q[i].valueOf();
92
	};
93
	this.getIterator = function () {
94
		return new dojo.collections.DictionaryIterator(items);
95
	};
96
	this.getKey = function (i) {
97
		return q[i].key;
98
	};
99
	this.getKeyList = function () {
100
		var arr = [];
101
		var e = this.getIterator();
102
		while (!e.atEnd()) {
103
			arr.push(e.get().key);
104
		}
105
		return arr;
106
	};
107
	this.getValueList = function () {
108
		var arr = [];
109
		var e = this.getIterator();
110
		while (!e.atEnd()) {
111
			arr.push(e.get().value);
112
		}
113
		return arr;
114
	};
115
	this.indexOfKey = function (k) {
116
		for (var i = 0; i < q.length; i++) {
117
			if (q[i].key == k) {
118
				return i;
119
			}
120
		}
121
		return -1;
122
	};
123
	this.indexOfValue = function (o) {
124
		for (var i = 0; i < q.length; i++) {
125
			if (q[i].value == o) {
126
				return i;
127
			}
128
		}
129
		return -1;
130
	};
131
	this.item = function (k) {
132
		if (k in items && !testObject[k]) {
133
			return items[k].valueOf();
134
		}
135
		return undefined;
136
	};
137
	this.remove = function (k) {
138
		delete items[k];
139
		build();
140
		this.count = q.length;
141
	};
142
	this.removeAt = function (i) {
143
		delete items[q[i].key];
144
		build();
145
		this.count = q.length;
146
	};
147
	this.replace = function (k, v) {
148
		if (!items[k]) {
149
			this.add(k, v);
150
			return false;
151
		} else {
152
			items[k] = new dojo.collections.DictionaryEntry(k, v);
153
			q.sort(sorter);
154
			return true;
155
		}
156
	};
157
	this.setByIndex = function (i, o) {
158
		items[q[i].key].value = o;
159
		build();
160
		this.count = q.length;
161
	};
162
	if (dictionary) {
163
		var e = dictionary.getIterator();
164
		while (!e.atEnd()) {
165
			var item = e.get();
166
			q[q.length] = items[item.key] = new dojo.collections.DictionaryEntry(item.key, item.value);
167
		}
168
		q.sort(sorter);
169
	}
170
};
171