TreeList

sealed abstract class TreeList[+A]

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
class Object
trait Matchable
class Any

Value members

Abstract methods

def drop(n: Long): TreeList[A]

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
def foldLeft[B](init: B)(fn: (B, A) => B): B

A strict, left-to-right fold: O(N)

A strict, left-to-right fold: O(N)

Source:
TreeList.scala
def foldMap[B : Monoid](fn: A => B): B

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
def get(idx: Long): Option[A]

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
def getUnsafe(idx: Long): A

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

The first item if nonempty

The first item if nonempty

Source:
TreeList.scala

returns true if size == 0. O(1)

returns true if size == 0. O(1)

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
def map[B](fn: A => B): TreeList[B]

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

returns true if size != 0. O(1)

returns true if size != 0. O(1)

Source:
TreeList.scala
def prepend[A1 >: A](a1: A1): TreeList[A1]

put an item on the front. O(1)

put an item on the front. O(1)

Source:
TreeList.scala
def size: Long

How many items are in this TreeList. O(log N)

How many items are in this TreeList. O(log N)

Source:
TreeList.scala
def split: (TreeList[A], TreeList[A])

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
def strictFoldRight[B](fin: B)(fn: (A, B) => B): B

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

All but the first item if nonempty

All but the first item if nonempty

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
def toList: List[A]

Convert to a scala standard List. O(N)

Convert to a scala standard List. O(N)

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
def uncons: Option[(A, TreeList[A])]

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
def updatedOrThis[A1 >: A](idx: Long, value: A1): TreeList[A1]

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

final def ++[A1 >: A](that: TreeList[A1]): TreeList[A1]

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
final def ::[A1 >: A](a1: A1): TreeList[A1]
final def filter(fn: A => Boolean): TreeList[A]

keep the elements that match a predicate O(N)

keep the elements that match a predicate O(N)

Source:
TreeList.scala
final def filterNot(fn: A => Boolean): TreeList[A]

same as filter(!fn(_)) O(N)

same as filter(!fn(_)) O(N)

Source:
TreeList.scala
final def flatMap[B](fn: A => TreeList[B]): TreeList[B]

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
final def reverse: TreeList[A]

O(N) reversal of the treeList

O(N) reversal of the treeList

Source:
TreeList.scala
final def take(n: Long): TreeList[A]

Take the first n things off the list. O(n)

Take the first n things off the list. O(n)

Source:
TreeList.scala
override def toString: String
Definition Classes
Any
Source:
TreeList.scala
final def updated[A1 >: A](idx: Long, value: A1): Option[TreeList[A1]]

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