In that case, you might be better off with what I suggested in my previous post:
quote:
Originally posted by Matti
a custom sorted hash table class with getter/setter and enumerator methods
This solution would consist of an object (indexed on window ID) and an array (alphabetically sorted by ID) holding the window objects. These indexes can then be stored in the window objects themselves so you can quickly access them in both caches. I liked this idea, so I decided to try this myself.
I created a
SortedObjectStore class which lets you retrieve and add objects while taking care of the sorting automatically. It exposes getter/enumeration methods (
Get,
GetBySortIdx and
GetCount) and setter methods (
Add,
ChangeId,
Remove and
RemoveAll).
Here's an example of how to use it:
js code:
Interfaces.Window = new SortedObjectStore;
var Window = {};
Window.Id = "MyWindow";
Interface.Window.Add(Window);
// MyWindow is now in the cache
var Window = Interface.Window.Get("MyWindow");
// Retrieved MyWindow from the cache
Interface.Window.ChangeId(Window, "YourWindow");
// Window is now called YourWindow instead of MyWindow
Interface.Window.Remove(Window);
// YourWindow is now removed from the cache
Looping through the sorted cache is done with
GetBySortIdx and
GetCount:
js code:
for(var i=0, len=Interface.Window.GetCount(); i<len; ++i) {
var Window = Interface.Window.GetBySortIdx(i);
// Loop logic goes here
}
The class can also be used for controls and elements, just create a new instance of it:
js code:
var MyWindow = {};
MyWindow.Id = "MyWindow";
MyWindow.Control = new SortedObjectStore;
MyWindow.Element = new SortedObjectStore;
or a bit more
classy:
js code:
var Window = function(Id) {
this.Id = Id;
this.Control = new SortedObjectStore;
this.Element = new SortedObjectStore;
Interface.Window.Add(this);
}
var MyWindow = new Window("MyWindow");
Some notes:
- You may not directly change the Id and SortIdx properties of the stored objects, since these are used by the class. You may only directly change the Id property when the object is not in the store, for example when creating an object to add to the store.
- Both sorting and retrieval are case-insensitive, they use the lowercase version of the Id property.
- When an object is removed from the store, its SortIdx is set to -1.
- The class is not foolproof. There are no validation checks to make sure that the right variable types are passed in as parameters, it just assumes that you're doing it right. It's up to you to implement some checks when working with user inputted data.
The class is attached and should be ready to use. Even if you don't plan to use the code, you may learn something from it.