Which ordering criteria does this particular dictionary use?
It is the insertion order. I am just converting this code for pure fun and make some tests. I will definitively have a look at Leo's code as well!
On Jul 13, 2018, at 11:16 AM, Christian Grün christian.gruen@gmail.com wrote:
Hi Giuseppe,
I was wondering why to change the original order anyway.
As maps are defined to be unordered in the spec, it is perfectly legal for XQuery processors to change the order of map entries when optimizing the query and constructing the map, and there is no guarantee that the “original order”, which may be derived from the string representation of the query, will be preserved in the compiled query. For example, in the following query, …
map:merge((1 to 100000) ! map:entry(., .))
…an implementation may decide to add the 10 input maps in parallel, or to reorder them before adding them to the final map.
What is the BaseX rationale for order?
BaseX uses a hash-based map implementation, which has no notion of ordering [1]. While the entries of these data structure would yield have a deterministic order when being serialized, this order will have no similarities with the order in which the map entries were written down in the textual query.
I was also trying to hack it adding initial numbers in the key names, but it does not work (but it does if key names are only numbers).
It might look like that ;) The chosen order (for parts of the data structure) depends on the computed hash code [2].
Bottom line: I am trying to "closely" reproduce some Python code involving dictionaries, where original order in dictionary.keys() is now interestingly kept (from Python 3.6, as far as I know). Unfortunately, the computation on the dictionary values assumes a specific order of the keys (which is not even an alphabetical one )...
Which ordering criteria does this particular dictionary use?
If performance is not critical (i.e., if you do not plan to store millions of items in the map), you could write a simple XQuery wrapper module that does what you need. If performance is an important factor, and if you have some (more) spare time, you could dive into the fascinating world of Leo’s XQuery data structures [3] and adapt the provided map implementations to your needs…
Christian
[1] https://en.wikipedia.org/wiki/Hash_array_mapped_trie [2] https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/ba... [3] https://github.com/LeoWoerteler/xq-modules