Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell data structure that is efficient for swapping elements?

I am looking for a Haskell data structure that stores an ordered list of elements and that is time-efficient at swapping pairs of elements at arbitrary locations within the list. It's not [a], obviously. It's not Vector because swapping creates new vectors. Which data structure is efficient at this?

like image 834
Ana Avatar asked Oct 04 '15 05:10

Ana


1 Answers

The most efficient implementations of persistent data structures, which exhibit O(1) updates (as well as appending, prepending, counting and slicing), are based on the Array Mapped Trie algorithm. The Vector data-structures of Clojure and Scala are based on it, for instance. The only Haskell implementation of that data-structure that I know of is presented by the "persistent-vector" package.

This algorithm is very young, it was only first presented in the year 2000, which might be the reason why not so many people have ever heard about it. But the thing turned out to be such a universal solution that it got adapted for Hash-tables soon after. The adapted version of this algorithm is called Hash Array Mapped Trie. It is as well used in Clojure and Scala to implement the Set and Map data-structures. It is also more ubiquitous in Haskell with packages like "unordered-containers" and "stm-containers" revolving around it.

To learn more about the algorithm I recommend the following links:

  • http://blog.higher-order.net/2009/02/01/understanding-clojures-persistentvector-implementation.html
  • http://lampwww.epfl.ch/papers/idealhashtrees.pdf
like image 120
Nikita Volkov Avatar answered Oct 21 '22 07:10

Nikita Volkov