/** * SortedObjectStore * by Mattias Buelens * * Hash table class to store objects, retrieve them by ID * and loop through the IDs alphabetically. */ var SortedObjectStore = function() { var ObjById = {}, ObjAbc = []; return { /** * Get an object by its ID */ Get: function(Id) { return ObjById[Id.toLowerCase()]; }, /** * Get an object by its sorted index */ GetBySortIdx: function(Idx) { return ObjAbc[Idx]; }, /** * Get the amount of stored Objs */ GetCount: function() { return ObjAbc.length; }, /** * Add an object to the store */ Add: function(Obj) { Obj.SortIdx = ObjAbc.length; // Store in the caches ObjById[Obj.Id.toLowerCase()] = ObjAbc[Obj.SortIdx] = Obj; // Update sorted cache UpdateAbc(); }, /** * Changes the ID of an object */ ChangeId: function(Obj, NewId) { // Remove from ID cache delete ObjById[Obj.Id.toLowerCase()]; // Set new ID Obj.Id = NewId; // Store in ID cache ObjById[NewId.toLowerCase()] = Obj; // Update sorted cache UpdateAbc(); }, /** * Removes an object from the store */ Remove: function(Obj) { // Remove from caches delete ObjById[Obj.Id.toLowerCase()]; ObjAbc.splice(Obj.SortIdx, 1); // Set sort index to -1 Obj.SortIdx = -1; // Update sorted cache UpdateAbc(); }, /** * Removes all Objs from the store */ RemoveAll: function() { // Set all sort indexes to -1 for(var i=0, len=ObjAbc.length, item; i b) ? 1 : -1; }); // Update the sort indexes for(var i=0, len=ObjAbc.length; i