Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Options

Is map order arbitrary?

Evan_ReeseEvan_Reese Member Posts: 2,066 PRO
When I have an empty map and I add new entries, does it add them in any particular order? If not, can I force it to? For example if I do this:

var myMap = {};

myMap.one = 1;
myMap.two = 22;
myMap.three = 333;

print(myMap);
The printed result might look like this, which is out of order. I hoped it would always add the new one at the end.
{"two" : 22, "one" : 1, "three" : 333}

Evan Reese / Principal and Industrial Designer with Ovyl
Website: ovyl.io

Answers

  • Options
    john_mcclaryjohn_mcclary Member, Developers Posts: 3,904 PRO
    I figured the map was based off of a hash table, or dictionary in other C-like languages.
    Where each item in the map is assigned a random hash code that can be looked up quickly given the key.
    So, it's position of an object in the map is irrelevant to function and becomes unsorted.

    (103) 10. Dictionaries - YouTube
    This video will teach you everything you want to know about dictionaries, but you can skip to about 50min, that's when it gets out of the theory and into a more practical view of it.

    I'm not sure how Onshape handles the specifics, but I would assume the hash code is generated in the same way as the internal IDs in featurescript.
    Or I could be completely wrong.
  • Options
    _anton_anton Member, Onshape Employees Posts: 289
    https://cad.onshape.com/FsDoc/relational.html

    Iteration over FS maps should happen in key order. I expect to (and do) see
    { one : 1 , three : 333 , two : 22 }
  • Options
    Evan_ReeseEvan_Reese Member Posts: 2,066 PRO
    Thanks, John! I'll check it out.
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • Options
    ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,178
    @john_mcclary if you want details you can have them: FeatureScript maps are internally implemented as functional AVL trees (sorted, balanced binary trees), rather than hash tables.  This is to make the following kinds of code fast and memory-efficient:

    println("Old value: " ~ x[3]);
    println("New value: " ~ y[3]);var x = { large map };
    var y = x;
    y[3] = 5;
    
    With a hash-table, we'd basically have to make a copy on either the second or third line, but with a tree, we can share almost the entire tree between x and y.  Plus I think that having the data in key order is nice.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Options
    john_mcclaryjohn_mcclary Member, Developers Posts: 3,904 PRO
    Cool, i'm going to have to learn more about avl trees to digest what you're throwing down. But that's good to know how the nuts and bolts work. 
Sign In or Register to comment.