Documentation ¶
Overview ¶
package skiprope provides rope-like data structure for efficient manipulation of large strings.
Index ¶
- Constants
- Variables
- type Rope
- func (r *Rope) Before(at int, fn func(r rune) bool) (retVal int, retRune rune, err error)
- func (r *Rope) EraseAt(point, n int) (err error)
- func (r *Rope) Index(at int) rune
- func (r *Rope) Insert(at int, str string) error
- func (r *Rope) InsertBytes(point int, data []byte) (err error)
- func (r *Rope) InsertRunes(point int, data []rune) (err error)
- func (r *Rope) Runes() int
- func (r *Rope) Size() int
- func (r *Rope) String() string
- func (r *Rope) Substr(pointA, pointB int) string
- func (r *Rope) SubstrBytes(pointA, pointB int) []byte
- func (r *Rope) SubstrRunes(pointA, pointB int) []rune
Examples ¶
Constants ¶
const ( MaxHeight = 60 // maximum size of the skiplist BucketSize = 64 // data bucket size in a knot - about 64 bytes is optimal for insertion on a core i7. )
Variables ¶
var Bias = 20
Bias indicates the probability that a new knot will have height of n+1. This is the parameter to tweak when considering the tradeoff between high amounts of append operations and amount of random writes.
The higher the bias is, the better the data structure is at performing end-of-string appends. The tradeoff is performance of random writes will deterioriate.
Functions ¶
This section is empty.
Types ¶
type Rope ¶
type Rope struct { Head knot // contains filtered or unexported fields }
Rope is a rope data structure built on top of a skip list.
func (*Rope) Before ¶
Before returns the index of the first rune that matches the function before the given point.
Example: "Hello World". Let's say `at` is at 9 (rune = r). And we want to find the whitespace before it. This function will return 5, which is the byte index of the rune immediately after the whitespace.
Example ¶
r := New() _ = r.Insert(0, "Hello World. This is a long sentence. The purpose of this long sentence is to make sure there is more than BucketSize worth of runes") before, char, err := r.Before(70, unicode.IsSpace) if err != nil { fmt.Printf("Error: cannot get before 70: %v", err) } fmt.Printf("First whitespace before position 70: %d - %q\n", before, char)
Output: First whitespace before position 70: 63 - ' '
func (*Rope) InsertBytes ¶
InsertBytes inserts the bytes at the point.
func (*Rope) InsertRunes ¶
InsertRunes inserts the runes at the point
func (*Rope) SubstrBytes ¶
SubstrBytes returns a byte slice given the "substring" of the rope. Both pointA and pointB refers to the rune, not the byte offset.
Example: "你好world" has a length of 11 bytes. If we only want "好", we'd have to call SubstrBytes(1, 2), not SubstrBytes(3, 6) which you would if you were dealing with pure bytes
func (*Rope) SubstrRunes ¶
SubstrRunes is like Substr, but returns []rune