InsertOrderedSet
An InsertOrderedSet is a data structure with the following properties:
- Contains unique values.
- O(1) manipulation operations (e.g. insert, delete)
- Preserves insertion order when converting to a list.
This data structure loosely conforms to the Set protocol, I'll be adding full support soon.
Installation
If available in Hex, the package can be installed as:
Add
insert_ordered_setto your list of dependencies inmix.exs:def deps do
[{:insert_ordered_set, "~> 0.0.1"}]end
Ensure
insert_ordered_setis started before your application:def application do
[applications: [:insert_ordered_set]]end
Usage
Currently supported Set methods:
delete/2member?/2new/0new/1put/2size/1to_list/1
I've added an additional method wherein you can tell the to_list function to produce the list
in :asc (insertion order) or :desc (reverse insertion order) order.
to_list/2
Unique values
[1, 1, 2]
|> InsertOrderedSet.new
|> InsertOrderedSet.to_list # [ 1, 2 ]Insertion order preservation
Try the following with MapSet:
["b", "c", "a"]
|> InsertOrderedSet.new
|> InsertOrderedSet.to_list # ["b", "c", "a"]Retrieve list in reverse insertion order
["b", "c", "a"]
|> InsertOrderedSet.new
|> InsertOrderedSet.to_list(:desc) # ["a", "c", "b"]TODO
-
Implement the rest of the
Setprotocol. - Adopt the Enumerable protocol.
-
Add
@docannotations. - Add typespecs.