holy hell python doesnt even have a built in red-black tree implementation, just a hashmap style Dict... There is a third party implementation but the best one is something like 8 months since the last release and the repo only has 6 commits...
How do people even survive in #python?
@freemo I don’t think I’ve ever needed such a thing, I suspect most people haven’t, which is why there’s nothing built-in.
You may find what you want by looking instead at how people solve similar problems in Python. Might be something out there that has a red-black tree or similar at its core but isn’t advertised as such.
@pganssle Depends what you mean by “need”. Obviously you could always roll your own Treemap so in the strictest sense you never truly “need” it.
But presuming you are relatively active coder I can guarantee you that you have been in positions where having it would have either (or both) made your code simpler and/or more efficient.
Chances are you accomplished the same end result by either using a hashmap (ordinary dictionary) or a hashset, and just repeatedly sorting it by calling a sort function every time you needed a sorted version of the data (which you may have even done after every new insert in some extreme cases). Which means inefficient code (often significantly so) and more lines of code and more logic than you might need. Not to mention the potential for all sorts of issues on top of that if it is a multithreaded environment.
I have used and needed Tree maps more times than I can count though could have hacked around it easy enough if i had really wanted to.
@pganssle And yes, Treemaps and Navigable maps in most languages are backed by balanced red-black trees underneath or something very similar. So if you’ve ever seen a sorted map/dict in a language it was likely a red-black tree.
@pganssle Well having your code not be slow enough to force you to use it is, of course, not that uncommon. though in those cases your code was probably still significantly slower than it needed to be… But as i said it isnt just about code performance it is also about code complexity. Even if you never needed the performance improvement it would have forced your code in many situations to be more complex and bug-prone then it needed to be.
In terms of code elegance pretty much everyone has hit it if you ever needed a map where the keys had a sorting order (sorting it yourself every time you modify the map is added code and prone to issues).
In terms of performance you’d only need it if you needed a sorted-key map and that map was particularly large and needed to be re-sorted often.
@pganssle in my case its a performance concern. I am basically implementing a pass-through array cache that remembers balues read from the underlying array-like object and stores it in a local array it needs to know what values it has seen though which makes it quite complicated as it would need to save ranges of seen values. Whole point of such a cahce is performance.
@pganssle so the keys i a sorted containers implementation are required to be comparable and dont need to be hashable?
@freemo I mean, there’s sortedcontainers, which is very popular and seems relatively well-maintained. It’s implemented in pure python and I don’t think it’s a red-black tree, so if it’s a performance bottleneck then you may need something else, but if you just want to simplify your code, then that should work.