diff options
Diffstat (limited to 'node_modules/montage-user/core/shim/structures.js')
-rwxr-xr-x | node_modules/montage-user/core/shim/structures.js | 552 |
1 files changed, 552 insertions, 0 deletions
diff --git a/node_modules/montage-user/core/shim/structures.js b/node_modules/montage-user/core/shim/structures.js new file mode 100755 index 00000000..fb8e2191 --- /dev/null +++ b/node_modules/montage-user/core/shim/structures.js | |||
@@ -0,0 +1,552 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | // Specification: | ||
8 | // http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets | ||
9 | |||
10 | /** | ||
11 | This module provides common data structure utililties, such as maps and sets. | ||
12 | @module montage/core/shim/structures | ||
13 | @see [Map class]{@link module:montage/core/shim/structures.Map} | ||
14 | */ | ||
15 | /** | ||
16 | @class module:montage/core/shim/structures.Map | ||
17 | @classdesc Provides a Map data structure for managing key/value pairs, including methods for querying and manipulating map elements. A map cannot contain duplicate keys; each key can map to at most one value. | ||
18 | */ | ||
19 | exports.Map = Map; | ||
20 | function Map(reserved, options) { | ||
21 | if (!(this instanceof Map)) { | ||
22 | return new Map(reserved, options); | ||
23 | } | ||
24 | options = options || {}; | ||
25 | var eq = options.eq || Set.eq; | ||
26 | var hash = options.hash || Set.hash; | ||
27 | this._set = Set( | ||
28 | undefined, | ||
29 | { | ||
30 | eq: function (a, b) { | ||
31 | return eq(a.key, b.key); | ||
32 | }, | ||
33 | hash: function (pair) { | ||
34 | return hash(pair.key); | ||
35 | } | ||
36 | } | ||
37 | ); | ||
38 | } | ||
39 | |||
40 | Object.defineProperties(Map.prototype, /** @lends module:montage/core/shim/structures.Map# */ { | ||
41 | /** | ||
42 | @function | ||
43 | @returns this._set.empty() | ||
44 | */ | ||
45 | empty: { | ||
46 | value: function () { | ||
47 | return this._set.empty(); | ||
48 | } | ||
49 | }, | ||
50 | |||
51 | /** | ||
52 | Returns the value associated with the <code>key</code> parameter, if it exists. | ||
53 | @function | ||
54 | @param {String} key The name of the key. | ||
55 | @returns {Object} The value of the specified key, if it exists; otherwise returns undefined. | ||
56 | */ | ||
57 | get: { | ||
58 | value: function (key) { | ||
59 | var pair = this._set.get({ | ||
60 | key: key | ||
61 | }); | ||
62 | return pair ? pair.value : undefined; | ||
63 | } | ||
64 | }, | ||
65 | |||
66 | /** | ||
67 | Adds a new key/value pair to the map. | ||
68 | @function | ||
69 | @param {Object} key The key to use for the new pair. | ||
70 | @param {Object} value The value to associate with the key. | ||
71 | @returns key, value | ||
72 | */ | ||
73 | set: { | ||
74 | value: function (key, value) { | ||
75 | return this._set.add({ | ||
76 | key: key, | ||
77 | value: value | ||
78 | }); | ||
79 | } | ||
80 | }, | ||
81 | |||
82 | /** | ||
83 | Deletes the element from the map specified by the <code>key</code> parameter. | ||
84 | @function | ||
85 | @param {Object} key The key of the map element to remove. | ||
86 | @returns key | ||
87 | */ | ||
88 | |||
89 | "delete": { // née del | ||
90 | value: function (key) { | ||
91 | return this._set["delete"]({ | ||
92 | key: key | ||
93 | }); | ||
94 | } | ||
95 | }, | ||
96 | |||
97 | /** | ||
98 | Returns true if the map contains an element with the specified key, otherwise returns false. | ||
99 | @function | ||
100 | @param {Object} key The key of the element you want to query for. | ||
101 | @returns {Boolean} Returns <code>true</code> if the map contains the specified key, otherwise returns <code>false</code>. | ||
102 | */ | ||
103 | has: { | ||
104 | value: function (key) { | ||
105 | return this._set.has({ | ||
106 | key: key | ||
107 | }); | ||
108 | } | ||
109 | }, | ||
110 | |||
111 | /** | ||
112 | Executes a function once per map element, passing the current element to each function as a parameter. | ||
113 | @function | ||
114 | @param {Function} callback The function to execute for each element. | ||
115 | */ | ||
116 | |||
117 | forEach: { | ||
118 | value: function (callback /*, thisp*/) { | ||
119 | var self = Object(this), | ||
120 | thisp = arguments[1]; | ||
121 | return this._set.forEach(function (pair) { | ||
122 | callback.call(thisp, pair.value, pair.key, self); | ||
123 | }); | ||
124 | } | ||
125 | } | ||
126 | }); | ||
127 | |||
128 | /** | ||
129 | Provides a set data structure and methods to query and modify the set.<br> | ||
130 | A set is a collection that contains no duplicate elements. It stores<br> | ||
131 | @class module:montage/core/shim/structures.Set | ||
132 | */ | ||
133 | exports.Set = Set; | ||
134 | function Set(reserved, options) { | ||
135 | if (!(this instanceof Set)) { | ||
136 | return new Set(reserved, options); | ||
137 | } | ||
138 | options = options || {}; | ||
139 | var eq = options.eq || Set.eq; | ||
140 | var hash = options.hash || Set.hash; | ||
141 | this._buckets = {}; | ||
142 | this._Bucket = function () { | ||
143 | return OrderedSet(undefined, { | ||
144 | eq: eq | ||
145 | }); | ||
146 | }; | ||
147 | this._eq = eq; | ||
148 | this._hash = hash; | ||
149 | } | ||
150 | |||
151 | Object.defineProperties(Set.prototype, /** @lends module:montage/core/shim/structures.Set# */ { | ||
152 | /** | ||
153 | Determines if the set is empty or not. | ||
154 | @function | ||
155 | @returns {Boolean} Returns <code>true</code> if the set is empty, otherwise returns <code>false</code>. | ||
156 | */ | ||
157 | empty: { | ||
158 | value: function () { | ||
159 | return !Object.keys(this._buckets).length; | ||
160 | } | ||
161 | }, | ||
162 | |||
163 | /** | ||
164 | Retrieves the value of the specified set element. | ||
165 | @function | ||
166 | @param {Object} value The key to an element in the set. | ||
167 | */ | ||
168 | get: { | ||
169 | value: function (value) { | ||
170 | var hash = this._hash(value); | ||
171 | var buckets = this._buckets; | ||
172 | return buckets[hash] ? buckets[hash].get(value) : undefined; | ||
173 | } | ||
174 | }, | ||
175 | |||
176 | /** | ||
177 | Determines if the set contains a specified item. | ||
178 | @function | ||
179 | @param {Number} value | ||
180 | @returns {Object} The value of the set element. | ||
181 | */ | ||
182 | has: { | ||
183 | value: function (value) { | ||
184 | var hash = this._hash(value); | ||
185 | var buckets = this._buckets; | ||
186 | return buckets[hash] ? buckets[hash].has(value) : false; | ||
187 | } | ||
188 | }, | ||
189 | |||
190 | /** | ||
191 | Inserts a new element into the set with <code>value</code> as the hash key. | ||
192 | @function | ||
193 | @param {String} value The new element's hash key. | ||
194 | */ | ||
195 | add: { // née insert | ||
196 | value: function (value) { | ||
197 | var hash = this._hash(value); | ||
198 | var buckets = this._buckets; | ||
199 | var bucket = buckets[hash] = buckets[hash] || this._Bucket(); | ||
200 | bucket.add(value); | ||
201 | } | ||
202 | }, | ||
203 | |||
204 | /** | ||
205 | Removes an element from the set identified by the specified hash key. | ||
206 | @function | ||
207 | @param {String} value The hash key of the element to remove. | ||
208 | */ | ||
209 | "delete": { // née remove | ||
210 | value: function (value) { | ||
211 | var hash = this._hash(value); | ||
212 | var buckets = this._buckets; | ||
213 | var bucket = buckets[hash] = buckets[hash] || this._Bucket(); | ||
214 | bucket["delete"](value); | ||
215 | if (bucket.empty()) { | ||
216 | delete buckets[hash]; | ||
217 | } | ||
218 | } | ||
219 | }, | ||
220 | |||
221 | /** | ||
222 | Executes a function once per set element. | ||
223 | @function | ||
224 | @param {Function} callback The function to execute on each element. | ||
225 | @returns {Object} object | ||