Advertisement

Simple way to create static sorted array?

Started by October 12, 2000 02:32 PM
10 comments, last by Stoffel 24 years, 3 months ago
Mike:
STL is ALWAYS worth it. =)

Your point about checking in debug is really interesting. All it would require is to make sure each entry is less than or equal to the next entry. I dig it.

Function pointers and virtual class overrides (mentioned by the next guy) will pretty much turn this into the "pluggable factory" design pattern, which I thought was over-designing. But it might be the best way.

My questions weren''t geared toward my specific application, but rather I actually wanted to find a general way to do a switch (string) nicely.

quote:
In the end, sometimes the clumsiest methods are the only methods.

Micah, I think you hit the nail directly on the head. May as well just leave it as a mass of if/else''s.
You want to switch on a string and call one of many functions that all have the same parameter list?


Hash table of AVL-trees of struct {CString, Func*()} hash on the first character, sort on the remaining string, return the proc address on Func*() find(CString)

Hash table should be size of 26, or 52 if case matters - pick a prime number if your strings will start with something other than letters, or a smaller prime if you won''t have many strings to search for. If don''t have lots of strings to search for, then drop the avl tree, and just use the hash and make it twice the size you need to store all your strings (and prime). If you do/will have LOTS of strings to search for, this is the fastest way without wasting lots of memory.

Hash Table, doesn''t get any faster than a look-up table baby!

AVL-tree provides perfectly balenced binary search tree for maximum search speed and optimized memory usaged.

struct {CString,Func*()} means no look up table or other &ltcensored> around to get to the function you want to call. Once you found the string, you got the function to call.

You could create the tree on init or on construction of whatever object it is contained in. You''ll need a big ''ol list of all the CString->Func*()

    //psedo-codeHashTable<AVLTree<Map<CString, Func*()>>> BadAssSearch(26);Map<Cstring, Func*()) TempMap;CLimitlessCalculator::CLimitlessCalculator(){TempMap = {"+", Addition()};BadAssSearch.Insert(TempMap);TempMap = {"-", Substraction()};BadAssSearch.Insert(TempMap);TempMap = {"mod", Modulation()};BadAssSearch.Insert(TempMap);///on and on etc...}    


I believe that would be the fastest way possible. It''s important that the hash table handles collisions by searching the tree... If my memory serves me right the average probe count is 2.4 for that structure - though it will vary a lot depending on how many things you stick in it. That is the fastest way to do what you require though, and would handle millions of strings. If you had 1,000,000,000 strings to search it would take 25.2 probes (on average) to find the one you want.

Good luck coding! The good news is that AVL tree doesn''t need to support on-the-fly deletes nor in order iteration (not threaded)!
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement