PageRenderTime 41ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/scripts/data-structure.js

http://showslow.googlecode.com/
JavaScript | 447 lines | 317 code | 59 blank | 71 comment | 51 complexity | b0640163b40179794d94dadc3240865c MD5 | raw file
  1. /**
  2. * A basic set (in the mathematical sense) data structure
  3. *
  4. * @constructor
  5. * @param {Array or SimileAjax.Set} [a] an initial collection
  6. */
  7. SimileAjax.Set = function(a) {
  8. this._hash = {};
  9. this._count = 0;
  10. if (a instanceof Array) {
  11. for (var i = 0; i < a.length; i++) {
  12. this.add(a[i]);
  13. }
  14. } else if (a instanceof SimileAjax.Set) {
  15. this.addSet(a);
  16. }
  17. }
  18. /**
  19. * Adds the given object to this set, assuming there it does not already exist
  20. *
  21. * @param {Object} o the object to add
  22. * @return {Boolean} true if the object was added, false if not
  23. */
  24. SimileAjax.Set.prototype.add = function(o) {
  25. if (!(o in this._hash)) {
  26. this._hash[o] = true;
  27. this._count++;
  28. return true;
  29. }
  30. return false;
  31. }
  32. /**
  33. * Adds each element in the given set to this set
  34. *
  35. * @param {SimileAjax.Set} set the set of elements to add
  36. */
  37. SimileAjax.Set.prototype.addSet = function(set) {
  38. for (var o in set._hash) {
  39. this.add(o);
  40. }
  41. }
  42. /**
  43. * Removes the given element from this set
  44. *
  45. * @param {Object} o the object to remove
  46. * @return {Boolean} true if the object was successfully removed,
  47. * false otherwise
  48. */
  49. SimileAjax.Set.prototype.remove = function(o) {
  50. if (o in this._hash) {
  51. delete this._hash[o];
  52. this._count--;
  53. return true;
  54. }
  55. return false;
  56. }
  57. /**
  58. * Removes the elements in this set that correspond to the elements in the
  59. * given set
  60. *
  61. * @param {SimileAjax.Set} set the set of elements to remove
  62. */
  63. SimileAjax.Set.prototype.removeSet = function(set) {
  64. for (var o in set._hash) {
  65. this.remove(o);
  66. }
  67. }
  68. /**
  69. * Removes all elements in this set that are not present in the given set, i.e.
  70. * modifies this set to the intersection of the two sets
  71. *
  72. * @param {SimileAjax.Set} set the set to intersect
  73. */
  74. SimileAjax.Set.prototype.retainSet = function(set) {
  75. for (var o in this._hash) {
  76. if (!set.contains(o)) {
  77. delete this._hash[o];
  78. this._count--;
  79. }
  80. }
  81. }
  82. /**
  83. * Returns whether or not the given element exists in this set
  84. *
  85. * @param {SimileAjax.Set} o the object to test for
  86. * @return {Boolean} true if the object is present, false otherwise
  87. */
  88. SimileAjax.Set.prototype.contains = function(o) {
  89. return (o in this._hash);
  90. }
  91. /**
  92. * Returns the number of elements in this set
  93. *
  94. * @return {Number} the number of elements in this set
  95. */
  96. SimileAjax.Set.prototype.size = function() {
  97. return this._count;
  98. }
  99. /**
  100. * Returns the elements of this set as an array
  101. *
  102. * @return {Array} a new array containing the elements of this set
  103. */
  104. SimileAjax.Set.prototype.toArray = function() {
  105. var a = [];
  106. for (var o in this._hash) {
  107. a.push(o);
  108. }
  109. return a;
  110. }
  111. /**
  112. * Iterates through the elements of this set, order unspecified, executing the
  113. * given function on each element until the function returns true
  114. *
  115. * @param {Function} f a function of form f(element)
  116. */
  117. SimileAjax.Set.prototype.visit = function(f) {
  118. for (var o in this._hash) {
  119. if (f(o) == true) {
  120. break;
  121. }
  122. }
  123. }
  124. /**
  125. * A sorted array data structure
  126. *
  127. * @constructor
  128. */
  129. SimileAjax.SortedArray = function(compare, initialArray) {
  130. this._a = (initialArray instanceof Array) ? initialArray : [];
  131. this._compare = compare;
  132. };
  133. SimileAjax.SortedArray.prototype.add = function(elmt) {
  134. var sa = this;
  135. var index = this.find(function(elmt2) {
  136. return sa._compare(elmt2, elmt);
  137. });
  138. if (index < this._a.length) {
  139. this._a.splice(index, 0, elmt);
  140. } else {
  141. this._a.push(elmt);
  142. }
  143. };
  144. SimileAjax.SortedArray.prototype.remove = function(elmt) {
  145. var sa = this;
  146. var index = this.find(function(elmt2) {
  147. return sa._compare(elmt2, elmt);
  148. });
  149. while (index < this._a.length && this._compare(this._a[index], elmt) == 0) {
  150. if (this._a[index] == elmt) {
  151. this._a.splice(index, 1);
  152. return true;
  153. } else {
  154. index++;
  155. }
  156. }
  157. return false;
  158. };
  159. SimileAjax.SortedArray.prototype.removeAll = function() {
  160. this._a = [];
  161. };
  162. SimileAjax.SortedArray.prototype.elementAt = function(index) {
  163. return this._a[index];
  164. };
  165. SimileAjax.SortedArray.prototype.length = function() {
  166. return this._a.length;
  167. };
  168. SimileAjax.SortedArray.prototype.find = function(compare) {
  169. var a = 0;
  170. var b = this._a.length;
  171. while (a < b) {
  172. var mid = Math.floor((a + b) / 2);
  173. var c = compare(this._a[mid]);
  174. if (mid == a) {
  175. return c < 0 ? a+1 : a;
  176. } else if (c < 0) {
  177. a = mid;
  178. } else {
  179. b = mid;
  180. }
  181. }
  182. return a;
  183. };
  184. SimileAjax.SortedArray.prototype.getFirst = function() {
  185. return (this._a.length > 0) ? this._a[0] : null;
  186. };
  187. SimileAjax.SortedArray.prototype.getLast = function() {
  188. return (this._a.length > 0) ? this._a[this._a.length - 1] : null;
  189. };
  190. /*==================================================
  191. * Event Index
  192. *==================================================
  193. */
  194. SimileAjax.EventIndex = function(unit) {
  195. var eventIndex = this;
  196. this._unit = (unit != null) ? unit : SimileAjax.NativeDateUnit;
  197. this._events = new SimileAjax.SortedArray(
  198. function(event1, event2) {
  199. return eventIndex._unit.compare(event1.getStart(), event2.getStart());
  200. }
  201. );
  202. this._idToEvent = {};
  203. this._indexed = true;
  204. };
  205. SimileAjax.EventIndex.prototype.getUnit = function() {
  206. return this._unit;
  207. };
  208. SimileAjax.EventIndex.prototype.getEvent = function(id) {
  209. return this._idToEvent[id];
  210. };
  211. SimileAjax.EventIndex.prototype.add = function(evt) {
  212. this._events.add(evt);
  213. this._idToEvent[evt.getID()] = evt;
  214. this._indexed = false;
  215. };
  216. SimileAjax.EventIndex.prototype.removeAll = function() {
  217. this._events.removeAll();
  218. this._idToEvent = {};
  219. this._indexed = false;
  220. };
  221. SimileAjax.EventIndex.prototype.getCount = function() {
  222. return this._events.length();
  223. };
  224. SimileAjax.EventIndex.prototype.getIterator = function(startDate, endDate) {
  225. if (!this._indexed) {
  226. this._index();
  227. }
  228. return new SimileAjax.EventIndex._Iterator(this._events, startDate, endDate, this._unit);
  229. };
  230. SimileAjax.EventIndex.prototype.getReverseIterator = function(startDate, endDate) {
  231. if (!this._indexed) {
  232. this._index();
  233. }
  234. return new SimileAjax.EventIndex._ReverseIterator(this._events, startDate, endDate, this._unit);
  235. };
  236. SimileAjax.EventIndex.prototype.getAllIterator = function() {
  237. return new SimileAjax.EventIndex._AllIterator(this._events);
  238. };
  239. SimileAjax.EventIndex.prototype.getEarliestDate = function() {
  240. var evt = this._events.getFirst();
  241. return (evt == null) ? null : evt.getStart();
  242. };
  243. SimileAjax.EventIndex.prototype.getLatestDate = function() {
  244. var evt = this._events.getLast();
  245. if (evt == null) {
  246. return null;
  247. }
  248. if (!this._indexed) {
  249. this._index();
  250. }
  251. var index = evt._earliestOverlapIndex;
  252. var date = this._events.elementAt(index).getEnd();
  253. for (var i = index + 1; i < this._events.length(); i++) {
  254. date = this._unit.later(date, this._events.elementAt(i).getEnd());
  255. }
  256. return date;
  257. };
  258. SimileAjax.EventIndex.prototype._index = function() {
  259. /*
  260. * For each event, we want to find the earliest preceding
  261. * event that overlaps with it, if any.
  262. */
  263. var l = this._events.length();
  264. for (var i = 0; i < l; i++) {
  265. var evt = this._events.elementAt(i);
  266. evt._earliestOverlapIndex = i;
  267. }
  268. var toIndex = 1;
  269. for (var i = 0; i < l; i++) {
  270. var evt = this._events.elementAt(i);
  271. var end = evt.getEnd();
  272. toIndex = Math.max(toIndex, i + 1);
  273. while (toIndex < l) {
  274. var evt2 = this._events.elementAt(toIndex);
  275. var start2 = evt2.getStart();
  276. if (this._unit.compare(start2, end) < 0) {
  277. evt2._earliestOverlapIndex = i;
  278. toIndex++;
  279. } else {
  280. break;
  281. }
  282. }
  283. }
  284. this._indexed = true;
  285. };
  286. SimileAjax.EventIndex._Iterator = function(events, startDate, endDate, unit) {
  287. this._events = events;
  288. this._startDate = startDate;
  289. this._endDate = endDate;
  290. this._unit = unit;
  291. this._currentIndex = events.find(function(evt) {
  292. return unit.compare(evt.getStart(), startDate);
  293. });
  294. if (this._currentIndex - 1 >= 0) {
  295. this._currentIndex = this._events.elementAt(this._currentIndex - 1)._earliestOverlapIndex;
  296. }
  297. this._currentIndex--;
  298. this._maxIndex = events.find(function(evt) {
  299. return unit.compare(evt.getStart(), endDate);
  300. });
  301. this._hasNext = false;
  302. this._next = null;
  303. this._findNext();
  304. };
  305. SimileAjax.EventIndex._Iterator.prototype = {
  306. hasNext: function() { return this._hasNext; },
  307. next: function() {
  308. if (this._hasNext) {
  309. var next = this._next;
  310. this._findNext();
  311. return next;
  312. } else {
  313. return null;
  314. }
  315. },
  316. _findNext: function() {
  317. var unit = this._unit;
  318. while ((++this._currentIndex) < this._maxIndex) {
  319. var evt = this._events.elementAt(this._currentIndex);
  320. if (unit.compare(evt.getStart(), this._endDate) < 0 &&
  321. unit.compare(evt.getEnd(), this._startDate) > 0) {
  322. this._next = evt;
  323. this._hasNext = true;
  324. return;
  325. }
  326. }
  327. this._next = null;
  328. this._hasNext = false;
  329. }
  330. };
  331. SimileAjax.EventIndex._ReverseIterator = function(events, startDate, endDate, unit) {
  332. this._events = events;
  333. this._startDate = startDate;
  334. this._endDate = endDate;
  335. this._unit = unit;
  336. this._minIndex = events.find(function(evt) {
  337. return unit.compare(evt.getStart(), startDate);
  338. });
  339. if (this._minIndex - 1 >= 0) {
  340. this._minIndex = this._events.elementAt(this._minIndex - 1)._earliestOverlapIndex;
  341. }
  342. this._maxIndex = events.find(function(evt) {
  343. return unit.compare(evt.getStart(), endDate);
  344. });
  345. this._currentIndex = this._maxIndex;
  346. this._hasNext = false;
  347. this._next = null;
  348. this._findNext();
  349. };
  350. SimileAjax.EventIndex._ReverseIterator.prototype = {
  351. hasNext: function() { return this._hasNext; },
  352. next: function() {
  353. if (this._hasNext) {
  354. var next = this._next;
  355. this._findNext();
  356. return next;
  357. } else {
  358. return null;
  359. }
  360. },
  361. _findNext: function() {
  362. var unit = this._unit;
  363. while ((--this._currentIndex) >= this._minIndex) {
  364. var evt = this._events.elementAt(this._currentIndex);
  365. if (unit.compare(evt.getStart(), this._endDate) < 0 &&
  366. unit.compare(evt.getEnd(), this._startDate) > 0) {
  367. this._next = evt;
  368. this._hasNext = true;
  369. return;
  370. }
  371. }
  372. this._next = null;
  373. this._hasNext = false;
  374. }
  375. };
  376. SimileAjax.EventIndex._AllIterator = function(events) {
  377. this._events = events;
  378. this._index = 0;
  379. };
  380. SimileAjax.EventIndex._AllIterator.prototype = {
  381. hasNext: function() {
  382. return this._index < this._events.length();
  383. },
  384. next: function() {
  385. return this._index < this._events.length() ?
  386. this._events.elementAt(this._index++) : null;
  387. }
  388. };