I've been playing around with adding ordered sets to Common Lisp lately. I started out by pulling my usual tricks, skip lists and binary searched/dynamically grown vectors; but none of them turned out the way I wanted; I always had the feeling I was fighting the language rather than enjoying the ride, which is unusual.
So I turned a new page; took a deep breath; and brain dumped the most naive, workable solution I could think of. Like Lisp's built in sets;slists
store their items as lists, with the difference that they're sorted and keep track of length and tail. This allows them to implement ordering and more in 200 lines of reasonable code; while being significantly (currently hovering above 10x) faster than built in (as inSBCL
) functionality for set operations. Once again, keeping it simple proves to be a successful strategy.
And here is the benchmark on which I base these outrageous claims, and a print out from my machine:
A full implementation of these ideas and more may be foundhere