Implementation of "Purely Functional Random Access Lists" by Chris Okasaki. This gives O(1) cons and uncons, and 2 log_2 N lookup.
A consequence of the log N complexity is that naive recursion on the inner methods will (almost) never blow the stack since the depth of the structure is log N, this greatly simplifies many methods. A key example is that unlike List, using a TreeList you can sequence and traverse very large lists without blowing the stack since the stack depth is only log N.
This data-structure is useful when you want fast cons and uncons, but also want to index. It does not have an optimized concatenation.
- Companion:
- object
- Source:
- TreeList.scala
Value members
Abstract methods
We can efficiently drop things off the front without rebuilding
We can efficiently drop things off the front without rebuilding
O(n) operation (complexity is the number of things being dropped)
- Source:
- TreeList.scala
map to a type with a Monoid and combine in the order of the list, O(N)
map to a type with a Monoid and combine in the order of the list, O(N)
- Source:
- TreeList.scala
lookup the given index in the list. O(log N). if the item is < 0 or >= size, return None
lookup the given index in the list. O(log N). if the item is < 0 or >= size, return None
- Source:
- TreeList.scala
lookup the given index in the list. O(log N). if the item is < 0 or >= size, else throw
lookup the given index in the list. O(log N). if the item is < 0 or >= size, else throw
- Source:
- TreeList.scala
get the last element, if it is not empty. O(log N) a bit more efficient than get(size - 1)
get the last element, if it is not empty. O(log N) a bit more efficient than get(size - 1)
- Source:
- TreeList.scala
standard map. O(N) operation. Since this preserves structure, it is more efficient than converting toIterator, mapping the iterator, and converting back
standard map. O(N) operation. Since this preserves structure, it is more efficient than converting toIterator, mapping the iterator, and converting back
- Source:
- TreeList.scala
How many items are in this TreeList. O(log N)
How many items are in this TreeList. O(log N)
- Source:
- TreeList.scala
return the right most full binary tree on the right, the rest on left val (l, r) = items.split assert((l ++ r) == items)
return the right most full binary tree on the right, the rest on left val (l, r) = items.split assert((l ++ r) == items)
O(log N)
- Source:
- TreeList.scala
a strict, right-to-left fold. Note, cats.Foldable defines foldRight to work on Eval, we use a different name here not to collide with the cats syntax
a strict, right-to-left fold. Note, cats.Foldable defines foldRight to work on Eval, we use a different name here not to collide with the cats syntax
O(N)
- Source:
- TreeList.scala
Get an iterator through the TreeList
Get an iterator through the TreeList
We can iterate through in O(N) time
- Source:
- TreeList.scala
Convert to a scala standard list, but reversed O(N)
Convert to a scala standard list, but reversed O(N)
- Source:
- TreeList.scala
Get a reverse iterator through the TreeList not as efficient as going in the left to right order.
Get a reverse iterator through the TreeList not as efficient as going in the left to right order.
It appears this is N log N in cost (although possible only N, as we have not proven the bound on cost)
This is useful if you only want a few things from the right, if you need to iterate the entire list, it is better to to use toListReverse which is O(N)
This is only a constant more efficient that iterating via random access using get
- Source:
- TreeList.scala
This is like headOption and tailOption in one call. O(1)
This is like headOption and tailOption in one call. O(1)
- Source:
- TreeList.scala
If the given index is in the list, update it, else return the current list with no change.
If the given index is in the list, update it, else return the current list with no change.
O(log N)
- Source:
- TreeList.scala
Concrete methods
Concatenate two TreeLists. This requires doing as much work as this.size
Concatenate two TreeLists. This requires doing as much work as this.size
O(this.size)
- Source:
- TreeList.scala
keep the elements that match a predicate O(N)
keep the elements that match a predicate O(N)
- Source:
- TreeList.scala
Standard flatMap on a List type. O(result.size + this.size)
Standard flatMap on a List type. O(result.size + this.size)
- Source:
- TreeList.scala
Take the first n things off the list. O(n)
Take the first n things off the list. O(n)
- Source:
- TreeList.scala
If the given index is in the list, update and return Some(updated). else return None
If the given index is in the list, update and return Some(updated). else return None
O(log N)
- Source:
- TreeList.scala