MXS Dictionary

MXS Dictionary

The implementation below is outdated, if you need a pure-maxscript dictionary I would like to redirect you to my newer approach that is hosted on GitHub:

https://github.com/cb109/mxs_types

 

A very simple pure mxs implementation of a dictionary to store key-value pairs. The code is optimized to be as short as possible (so it can be included in a script file) , which is why it is not very readable :)

It is somewhat “fake” in that it is not using any hashing but an internal array of tuples. Which means the bigger it gets the slower it will be to work with. So its usage is probably quite limited when used with lots of items.

The big advantage to using a dotNet HashTable however is, that you can use anything as a value, not just strings and numbers. So you can create mappings of teapots, animation controllers, materials, whatever you need.

Example usage:

-- Either copy/paste into your script or bundle the file with your main script file and use
fileIn "dict.ms"
-- or
include "dict.ms"

-- Available methods are shown in these examples:

d = dict()
d.add "Peter" 34
d.add "Zacharias" 29
d.add "Christian" 12
d.add "Hank" 56

d.items()
--#(#("Peter", 34), #("Zacharias", 29), #("Christian", 12), #("Hank", 56))
d.keys()
--#("Peter", "Zacharias", "Christian", "Hank")
d.values()
--#(34, 29, 12, 56)

a = d.sortByKey()
--#(#("Christian", 12), #("Hank", 56), #("Peter", 34), #("Zacharias", 29))
b = d.sortByValue()
--#(#("Christian", 12), #("Zacharias", 29), #("Peter", 34), #("Hank", 56))

d.add "George" "McDolan"
b = d.sortByValue()
--"SortException: Dict values must be of same comparable type to sort"

d.hasKey "Hank"
--true
d.get "Hank"
--56

i = d.pop "Hank"
--#("Hank", 56)
d.items()
--#(#("Peter", 34), #("Zacharias", 29), #("Christian", 12))

Download “mxsDict” dict.ms – Downloaded 803 times – 1 kB

If you use this script, be awesome and donate a few bucks!