RingView

io.github.scala_tessella.ring_seq.RingView
See theRingView companion object
final class RingView[A]

A lazy '''view''' of a sequence considered circular.

A RingView never copies its elements: it wraps an IndexedSeq together with a rotation offset and a reflected flag, so rotateRight, rotateLeft, startAt, reflectAt and reverse are all O(1) — they return a new view over the same underlying sequence. In particular rotations and rotationsAndReflections produce views in O(1) each, so pipelines like seq.ring.rotations.exists(...) short-circuit without materializing anything.

Because RingView is its own type (not a Seq), the circular operations carry their plain names — no O suffix is needed: seq.ring.slice(-1, 4), seq.ring.sliding(2), and indexing wraps in both directions.

Materialize at the boundary with toSeq, toVector, to or iterator.

Obtain a view with the ring method added to every Seq (and String) by import RingSeq._:

Vector(0, 1, 2, 3).ring.rotateRight(1).toVector // Vector(3, 0, 1, 2)

Attributes

Note

a view over a mutable sequence reflects later mutations of it, exactly like the standard library views. A non-indexed source (e.g. List) is copied once to a Vector on creation.

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def alignTo(that: Seq[A]): Option[Index]

Finds the rotation offset that aligns this ring with a given sequence.

Finds the rotation offset that aligns this ring with a given sequence.

Attributes

Returns

Some(k) such that startAt(k) equals ''that'', or None if no rotation matches (or sizes differ).

def apply(i: IndexO): A

Gets the element at some circular index: any integer is valid, wrapping in both directions.

Gets the element at some circular index: any integer is valid, wrapping in both directions.

Attributes

Throws
java.lang.ArithmeticException

if the ring is empty

Example
Seq(0, 1, 2).ring(3) // 0
def bracelet(implicit ord: Ordering[A]): RingView[A]

The lexicographically smallest representative under both rotation and reflection (bracelet canonical form) as a view.

The lexicographically smallest representative under both rotation and reflection (bracelet canonical form) as a view.

Attributes

def canonical(implicit ord: Ordering[A]): RingView[A]

The lexicographically smallest rotation of this ring (necklace canonical form) as a view.

The lexicographically smallest rotation of this ring (necklace canonical form) as a view.

Attributes

def canonicalIndex(implicit ord: Ordering[A]): Index

The starting index of the lexicographically smallest rotation of this view (two-pointer minimal rotation, O(n) time).

The starting index of the lexicographically smallest rotation of this view (two-pointer minimal rotation, O(n) time).

Attributes

def containsSlice(that: Seq[A]): Boolean

Tests whether this ring contains a given sequence as a (possibly wrapping) slice.

Tests whether this ring contains a given sequence as a (possibly wrapping) slice.

Attributes

def dropWhile(p: A => Boolean, from: IndexO = ...): Iterator[A]

Drops the longest prefix of elements starting at some circular index that satisfy a predicate, as a lazy iterator over the remainder of the lap.

Drops the longest prefix of elements starting at some circular index that satisfy a predicate, as a lazy iterator over the remainder of the lap.

Attributes

override def equals(other: Any): Boolean

Two views are equal iff they present the same elements in the same order, regardless of how they were obtained.

Two views are equal iff they present the same elements in the same order, regardless of how they were obtained.

Attributes

Definition Classes
Any
def grouped(size: Int): Iterator[Seq[A]]

Partitions the ring into non-overlapping fixed-size blocks, the last wrapping across the seam so every block has exactly size elements. Produces ceil(n / size) blocks, none for an empty ring.

Partitions the ring into non-overlapping fixed-size blocks, the last wrapping across the seam so every block has exactly size elements. Produces ceil(n / size) blocks, none for an empty ring.

Attributes

def hammingDistance(that: Seq[A]): Int

The number of positions at which corresponding elements differ (Hamming distance).

The number of positions at which corresponding elements differ (Hamming distance).

Attributes

Throws
java.lang.IllegalArgumentException

if the sizes differ

override def hashCode: Int

Calculates a hash code value for the object.

Calculates a hash code value for the object.

The default hashing algorithm is platform dependent.

Note that it is allowed for two objects to have identical hash codes (o1.hashCode.equals(o2.hashCode)) yet not be equal (o1.equals(o2) returns false). A degenerate implementation could always return 0. However, it is required that if two objects are equal (o1.equals(o2) returns true) that they have identical hash codes (o1.hashCode.equals(o2.hashCode)). Therefore, when overriding this method, be sure to verify that the behavior is consistent with the equals method.

Attributes

Returns

the hash code value for this object.

Definition Classes
Any

Normalize a given circular index to [0, size).

Normalize a given circular index to [0, size).

Attributes

Throws
java.lang.ArithmeticException

if the ring is empty

def indexOf(elem: A, from: IndexO = ...): Index

Finds the circular index of the first element equal to a given value, searching circularly from a given circular index and wrapping past the end.

Finds the circular index of the first element equal to a given value, searching circularly from a given circular index and wrapping past the end.

Attributes

Returns

the index in [0, size), or -1 if the element is not in the ring.

def indexOfSlice(that: Seq[A], from: IndexO = ...): Index

Finds the first index at or after a circular start index where this ring contains a given sequence as a (possibly wrapping) slice, or -1 if none exists.

Finds the first index at or after a circular start index where this ring contains a given sequence as a (possibly wrapping) slice, or -1 if none exists.

Attributes

def isEmpty: Boolean

Tests whether the ring is empty.

Tests whether the ring is empty.

Attributes

def isReflectionOf(that: Seq[A]): Boolean

Tests whether this ring is a reflection of a given sequence.

Tests whether this ring is a reflection of a given sequence.

Attributes

def isReversionOf(that: Seq[A]): Boolean

Tests whether this ring is a reversion of a given sequence.

Tests whether this ring is a reversion of a given sequence.

Attributes

def isRotationOf(that: Seq[A]): Boolean

Tests whether this ring is a rotation of a given sequence.

Tests whether this ring is a rotation of a given sequence.

Attributes

def isRotationOrReflectionOf(that: Seq[A]): Boolean

Tests whether this ring is a rotation or a reflection of a given sequence.

Tests whether this ring is a rotation or a reflection of a given sequence.

Attributes

def iterator: Iterator[A]

Iterates once over the ring in view order.

Iterates once over the ring in view order.

Attributes

def lastIndexOfSlice(that: Seq[A], end: IndexO = ...): Index

Finds the last index at or before a circular end index where this ring contains a given sequence as a (possibly wrapping) slice, or -1 if none exists.

Finds the last index at or before a circular end index where this ring contains a given sequence as a (possibly wrapping) slice, or -1 if none exists.

Attributes

def length: Int

The number of elements in the ring.

The number of elements in the ring.

Attributes

def lift(i: IndexO): Option[A]

Optionally gets the element at some circular index: None only when the ring is empty.

Optionally gets the element at some circular index: None only when the ring is empty.

Attributes

Example
Seq(0, 1, 2).ring.lift(3) // Some(0)
def minRotationalHammingDistance(that: Seq[A]): Int

The minimum Hamming distance over all rotations of this ring.

The minimum Hamming distance over all rotations of this ring.

Attributes

Throws
java.lang.IllegalArgumentException

if the sizes differ

def nonEmpty: Boolean

Tests whether the ring is not empty.

Tests whether the ring is not empty.

Attributes

def reflectAt(i: IndexO = ...): RingView[A]

A view of this ring reflected (direction flipped) to start at some circular index — O(1), no copy. Applying reflectAt at the same index twice returns the original view.

A view of this ring reflected (direction flipped) to start at some circular index — O(1), no copy. Applying reflectAt at the same index twice returns the original view.

Attributes

Example
Seq(0, 1, 2).ring.reflectAt().toVector // Vector(0, 2, 1)

Calculates the axes of reflectional symmetry of this ring.

Calculates the axes of reflectional symmetry of this ring.

Attributes

def reflections: Iterator[RingView[A]]

The 2 reflections of this ring as views (itself and reflectAt()), or just itself if empty.

The 2 reflections of this ring as views (itself and reflectAt()), or just itself if empty.

Attributes

def reverse: RingView[A]

A view of this ring with the elements in reverse order — O(1), no copy.

A view of this ring with the elements in reverse order — O(1), no copy.

Attributes

def reversions: Iterator[RingView[A]]

The 2 reversions of this ring as views (itself and reverse), or just itself if empty.

The 2 reversions of this ring as views (itself and reverse), or just itself if empty.

Attributes

def rotateLeft(step: Int): RingView[A]

A view of this ring rotated to the left by some steps (negative = right) — O(1), no copy.

A view of this ring rotated to the left by some steps (negative = right) — O(1), no copy.

Attributes

Example
Seq(0, 1, 2).ring.rotateLeft(1).toVector // Vector(1, 2, 0)
def rotateRight(step: Int): RingView[A]

A view of this ring rotated to the right by some steps (negative = left) — O(1), no copy.

A view of this ring rotated to the right by some steps (negative = left) — O(1), no copy.

Attributes

Example
Seq(0, 1, 2).ring.rotateRight(1).toVector // Vector(2, 0, 1)

Computes the order of rotational symmetry possessed by this ring.

Computes the order of rotational symmetry possessed by this ring.

Attributes

def rotations: Iterator[RingView[A]]

All the rotations of this ring as views — O(1) each, starting from this view itself, or just itself if empty.

All the rotations of this ring as views — O(1) each, starting from this view itself, or just itself if empty.

Attributes

Example
Seq(0, 1, 2).ring.rotations.map(_.toVector) // Iterator(Vector(0, 1, 2), Vector(1, 2, 0), Vector(2, 0, 1))
def rotationsAndReflections: Iterator[RingView[A]]

All the rotations and reflections of this ring as views — O(1) each, or just itself if empty.

All the rotations and reflections of this ring as views — O(1) each, or just itself if empty.

Attributes

def segmentLength(p: A => Boolean, from: IndexO = ...): Int

Computes the length of the longest segment starting at some circular index whose elements all satisfy a predicate (at most one full lap).

Computes the length of the longest segment starting at some circular index whose elements all satisfy a predicate (at most one full lap).

Attributes

def size: Int

The number of elements in the ring.

The number of elements in the ring.

Attributes

def slice(from: IndexO, until: IndexO): Iterator[A]

Selects an interval of elements as a lazy iterator: circular indices, and the interval may be longer than the ring itself.

Selects an interval of elements as a lazy iterator: circular indices, and the interval may be longer than the ring itself.

Attributes

Example
Seq(0, 1, 2).ring.slice(-1, 4).toList // List(2, 0, 1, 2, 0)
def sliding(size: Int, step: Int = ...): Iterator[Seq[A]]

Groups elements in fixed size blocks by passing a "sliding window" over the ring, wrapping past the end.

Groups elements in fixed size blocks by passing a "sliding window" over the ring, wrapping past the end.

Attributes

Example
Seq(0, 1, 2).ring.sliding(2).toList // List(Seq(0, 1), Seq(1, 2), Seq(2, 0))
def span(p: A => Boolean, from: IndexO = ...): (Iterator[A], Iterator[A])

Splits one lap of the ring at the first element, starting from some circular index, that does not satisfy the predicate.

Splits one lap of the ring at the first element, starting from some circular index, that does not satisfy the predicate.

Attributes

Returns

a pair (takeWhile(p, from), dropWhile(p, from)) of lazy iterators.

def startAt(i: IndexO): RingView[A]

A view of this ring rotated to start at some circular index — O(1), no copy.

A view of this ring rotated to start at some circular index — O(1), no copy.

Attributes

Example
Seq(0, 1, 2, 3).ring.startAt(2).toVector // Vector(2, 3, 0, 1)
def symmetry: Int

Computes the order of reflectional (mirror) symmetry possessed by this ring.

Computes the order of reflectional (mirror) symmetry possessed by this ring.

Attributes

def symmetryIndices: List[Index]

Finds the indices of each element of this ring close to an axis of reflectional symmetry.

Finds the indices of each element of this ring close to an axis of reflectional symmetry.

Attributes

def takeWhile(p: A => Boolean, from: IndexO = ...): Iterator[A]

Selects the longest prefix of elements starting at some circular index that satisfy a predicate, as a lazy iterator (at most one full lap).

Selects the longest prefix of elements starting at some circular index that satisfy a predicate, as a lazy iterator (at most one full lap).

Attributes

def to[C1](factory: Factory[A, C1]): C1

Materializes this view to any collection type.

Materializes this view to any collection type.

Attributes

Example
Seq(0, 1, 2).ring.rotateLeft(1).to(List) // List(1, 2, 0)
def toSeq: Seq[A]

Materializes this view to an immutable Seq.

Materializes this view to an immutable Seq.

Attributes

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
def toVector: Vector[A]

Materializes this view to a Vector.

Materializes this view to a Vector.

Attributes

def zipWithIndex(from: IndexO = ...): Iterator[(A, Index)]

Iterates over the elements paired with their view index, starting at some circular index. Indices are in [0, size).

Iterates over the elements paired with their view index, starting at some circular index. Indices are in [0, size).

Attributes