treelib icon indicating copy to clipboard operation
treelib copied to clipboard

Can it use SortedDict from the SortedCollection for children's order?

Open evandrocoan opened this issue 8 years ago • 3 comments

Can it use SortedDict from the SortedCollection for children's order?

Use SortedDict from the SortedCollection.

A SortedDict provides the same methods as a dict. Additionally, a SortedDict efficiently maintains its keys in sorted order. Consequently, the keys method will return the keys in sorted order, the popitem method will remove the item with the highest key, etc.

Related to:

  1. https://github.com/caesar0301/treelib/issues/65 - How to order by data?

evandrocoan avatar Apr 02 '17 18:04 evandrocoan

Issue #65 addressed a more general ordering of nodes by ANY attribute. SortedDict may replace the inner node dict, but it may introduce more performance concerns (see SO http://stackoverflow.com/questions/8176513/ordereddict-performance-compared-to-deque).

caesar0301 avatar Apr 07 '17 10:04 caesar0301

I see that Ordered dict is implemented in Python (slower), while the current structure is written right on C (faster). So could we create a new type of node called OrderedNode, based on OrderedDict, and use these nodes instead of the current nodes, if it is wanted ordered nodes. If somehow the performance for this new Node type would be bad, an alternative could be looked into, improving the new node type OrderedNode.

evandrocoan avatar Apr 07 '17 19:04 evandrocoan

Note that regular python dictionaries started preserving insertion order as an implementation detail in cpython 3.6, and from 3.7 it's now guaranteed by Guido's decree: 'Make it so. "Dict keeps insertion order" is the ruling.' https://mail.python.org/pipermail/python-dev/2017-December/151283.html

ghost avatar Jul 09 '18 23:07 ghost