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 <censored> 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