Class Rope

java.lang.Object
io.lacuna.bifurcan.Rope
All Implemented Interfaces:
Comparable<Rope>

public class Rope extends Object implements Comparable<Rope>
A tree-based immutable string representation, indexed on both full Unicode code points and Java's UTF-16 code units. Storage at the leaves uses UTF-8 encoding. It allows for efficient insert, remove, slice, and concat calls, and can be converted in constant time to a Java CharSequence via toCharSequence().
  • Field Details

    • EMPTY

      public static final Rope EMPTY
  • Method Details

    • from

      public static Rope from(CharSequence cs)
      Returns:
      a rope corresponding to cs
    • concat

      public Rope concat(Rope rope)
      Returns:
      a new Rope with rope concatenated to the end
    • nth

      public int nth(int idx)
      Returns:
      the nth code point within the rope
      Throws:
      IndexOutOfBoundsException - if idx is not within [0, size)
    • size

      public int size()
      Returns:
      the number of code points in the rope
    • remove

      public Rope remove(int start, int end)
      Returns:
      a rope without the code points within [start, end)
      Throws:
      IllegalArgumentException - if start or end are not within [0, size)
    • insert

      public Rope insert(int idx, Rope rope)
      Returns:
      a new rope with rope inserted after the first idx code points
    • insert

      public Rope insert(int index, CharSequence cs)
      Returns:
      a new rope with cs inserted after the first index code points
    • slice

      public Rope slice(int start, int end)
      Returns:
      a new rope representing the code points within [start, end)
      Throws:
      IllegalArgumentException - if end < start, or start and end are not within [0, size)
    • isLinear

      public boolean isLinear()
    • forked

      public Rope forked()
    • linear

      public Rope linear()
    • bytes

      public Iterator<ByteBuffer> bytes()
      Returns:
      a sequence of bytes representing the UTF-8 encoding of the rope
    • reverseChars

      public PrimitiveIterator.OfInt reverseChars()
      Returns:
      a sequence of integers representing the UTF-16 code units from back to front
    • chars

      public PrimitiveIterator.OfInt chars()
      Returns:
      a sequence of integers representing the UTF-16 code units from front to back
    • reverseCodePoints

      public PrimitiveIterator.OfInt reverseCodePoints()
      Returns:
      a sequence of integers representing the code points from back to front
    • codePoints

      public PrimitiveIterator.OfInt codePoints()
      Returns:
      a sequence of integers representing the code points from front to back
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      a corresponding Java-style String in O(N) time
    • toCharSequence

      public CharSequence toCharSequence()
      Returns:
      a corresponding Java-style CharSequence in O(1) time
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • compareTo

      public int compareTo(Rope o)
      Specified by:
      compareTo in interface Comparable<Rope>
      Returns:
      a value representing the lexicographic comparison of the code points