Dispatch creates hash tables only for lists of length larger than 3. Is there any way to force MMA to create hash tables even for lists of length smaller than 4?
Table[Head@Dispatch@Table[Rule[a[i], i], {i, n}], {n, 5}]
(* {List, List, List, Dispatch, Dispatch} *)
Dispatchdoes not seem a good idea to me, if you need to do this often, since it will have to redo the dispatch table at every update, and the complexity of such an update will be generally linear with respect to the length of the list of rules it currently has. With respect to this performance hit, the one of pattern-matching onListorDispatchis a minor one. If you want frequent updates, then you will likely be better off withDownValuesorSystem`Utilities`HashTable. If the updates will not be frequent, this extra overhead should not matter. – Leonid Shifrin Aug 07 '13 at 18:49DispatchtoDownValuesorSystem`Utilities`HashTable? I likeDispatchvery much myself, but normally use it in cases when the updates are either not needed at all or very infrequent. The other two options provide efficient updates, but extracting all keys or all values can be comparatively slow. As long as you don't have to frequently extract full key set or value set, I would go with one of them instead ofDispatch, judging from your description. – Leonid Shifrin Aug 07 '13 at 19:15UtilitiesHashTable. It does not seem to be documented. I do not use DownValues because it conflicts with ReadProtect (writing a package). – Hector Aug 07 '13 at 20:41System`Utilities`HashTable. And I didn't follow your remark onReadProtected. If you mean that you can't change theDownValuesonce you read-protect the symbol, this is not true. In general, if you want to prevent the users from accessing your implementation, this is a different matter, and I am sure that one can do this reasonably well, but you can't get an absolute protection (which is I think true also in general, not just for Mathematica). – Leonid Shifrin Aug 07 '13 at 21:28DownValuesandReadProtectit's possible we can provide you with a work-around. – Mr.Wizard Aug 08 '13 at 03:28