TreeList

cats.collections.TreeList
See theTreeList companion object
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.

Attributes

Companion
object
Source
TreeList.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

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)

Attributes

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)

Attributes

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)

Attributes

Source
TreeList.scala
def get(idx: Long): Option[A]

lookup the given index in the list.

lookup the given index in the list. O(log N). if the item is < 0 or >= size, return None

Attributes

Source
TreeList.scala
def getUnsafe(idx: Long): A

lookup the given index in the list.

lookup the given index in the list. O(log N). if the item is < 0 or >= size, else throw

Attributes

Source
TreeList.scala
def headOption: Option[A]

The first item if nonempty

The first item if nonempty

Attributes

Source
TreeList.scala
def isEmpty: Boolean

returns true if size == 0.

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

Attributes

Source
TreeList.scala
def lastOption: Option[A]

get the last element, if it is not empty.

get the last element, if it is not empty. O(log N) a bit more efficient than get(size - 1)

Attributes

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

standard map.

standard map. O(N) operation. Since this preserves structure, it is more efficient than converting toIterator, mapping the iterator, and converting back

Attributes

Source
TreeList.scala
def nonEmpty: Boolean

returns true if size != 0.

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

Attributes

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

put an item on the front.

put an item on the front. O(1)

Attributes

Source
TreeList.scala
def size: Long

How many items are in this TreeList.

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

Attributes

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)

Attributes

Source
TreeList.scala
def strictFoldRight[B](fin: B)(fn: (A, B) => B): B

a strict, right-to-left fold.

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)

Attributes

Source
TreeList.scala
def tailOption: Option[TreeList[A]]

All but the first item if nonempty

All but the first item if nonempty

Attributes

Source
TreeList.scala
def toIterator: Iterator[A]

Get an iterator through the TreeList

Get an iterator through the TreeList

We can iterate through in O(N) time

Attributes

Source
TreeList.scala
def toList: List[A]

Convert to a scala standard List.

Convert to a scala standard List. O(N)

Attributes

Source
TreeList.scala
def toListReverse: List[A]

Convert to a scala standard list, but reversed O(N)

Convert to a scala standard list, but reversed O(N)

Attributes

Source
TreeList.scala
def toReverseIterator: Iterator[A]

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

Attributes

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

This is like headOption and tailOption in one call.

This is like headOption and tailOption in one call. O(1)

Attributes

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)

Attributes

Source
TreeList.scala

Concrete methods

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

Concatenate two TreeLists.

Concatenate two TreeLists. This requires doing as much work as this.size

O(this.size)

Attributes

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

Attributes

Source
TreeList.scala
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)

Attributes

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

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

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

Attributes

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

Standard flatMap on a List type.

Standard flatMap on a List type. O(result.size + this.size)

Attributes

Source
TreeList.scala
final def reverse: TreeList[A]

O(N) reversal of the treeList

O(N) reversal of the treeList

Attributes

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

Take the first n things off the list.

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

Attributes

Source
TreeList.scala
override def toString: String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns

a string representation of the object.

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).

If the given index is in the list, update and return Some(updated). else return None

O(log N)

Attributes

Source
TreeList.scala