lexorank

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2026 License: MIT Imports: 3 Imported by: 0

README

LexoRank in Go

A Go implementation of a list ordering system based on JIRA's LexoRank algorithm.

Generates lexicographically sortable string keys for ordering items. String comparison produces correct ordering — no numeric parsing needed.

Install

go get github.com/misa198/lexorank-go

Usage

Package-level functions
import "github.com/misa198/lexorank-go"

// min rank: "0|000000:"
minRank := lexorank.Min()

// max rank: "0|zzzzzz:"
maxRank := lexorank.Max()

// middle rank: "0|hzzzzz:"
midRank := lexorank.Middle()

// parse from string
parsed := lexorank.ParseRank("0|0i0000:")
Generate next/previous
rank := lexorank.Min()

next := rank.GenNext() // "0|100000:"
prev := rank.GenPrev() // generates rank before current
Insert between two ranks
a := lexorank.Min()
b := a.GenNext().GenNext()

between := a.Between(b) // rank between a and b
fmt.Println(between)    // "0|100000:"
Buckets

Three buckets (0, 1, 2) partition the rank space for rebalancing.

rank := lexorank.Min()

// move to next bucket
nextBucket := rank.InNextBucket()

// move to previous bucket
prevBucket := rank.InPrevBucket()
Comparing ranks
a := lexorank.Min()
b := lexorank.Max()

a.CompareTo(b) // -1 (a < b)
a.Equals(b)    // false

// string comparison also works since ranks are lexicographically sorted
a.String() < b.String() // true

Rank format

Ranks are strings like "0|hzzzzz:":

  • 0 — bucket (0, 1, or 2)
  • | — separator
  • hzzzzz — base-36 decimal value (digits 0-z)
  • : — radix point

Normal string comparison produces correct ordering.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket int

Bucket represents one of the 3 rank buckets (0, 1, 2).

const (
	Bucket0 Bucket = 0
	Bucket1 Bucket = 1
	Bucket2 Bucket = 2
)

func BucketFrom

func BucketFrom(s string) (Bucket, error)

BucketFrom parses a bucket from a string ("0", "1", or "2").

func MaxBucket

func MaxBucket() Bucket

MaxBucket returns Bucket2.

func ResolveBucket

func ResolveBucket(bucketID int) (Bucket, error)

ResolveBucket resolves a bucket by numeric ID.

func (Bucket) Format

func (b Bucket) Format() string

func (Bucket) Next

func (b Bucket) Next() Bucket

func (Bucket) Prev

func (b Bucket) Prev() Bucket

func (Bucket) String

func (b Bucket) String() string

type Decimal

type Decimal struct {
	// contains filtered or unexported fields
}

Decimal is an arbitrary-precision decimal built on Integer.

func DecimalFrom

func DecimalFrom(integer Integer) Decimal

DecimalFrom wraps an Integer with scale 0.

func Half

func Half(sys NumeralSystem) Decimal

Half returns 0.5 in the given numeral system.

func MakeDecimal

func MakeDecimal(integer Integer, sig int) Decimal

MakeDecimal creates a Decimal, trimming trailing fractional zeros.

func ParseDecimal

func ParseDecimal(s string, sys NumeralSystem) (Decimal, error)

ParseDecimal parses a string with an optional radix point into a Decimal.

func (Decimal) Add

func (d Decimal) Add(other Decimal) Decimal

func (Decimal) Ceil

func (d Decimal) Ceil() Integer

func (Decimal) CompareTo

func (d Decimal) CompareTo(other Decimal) int

func (Decimal) Equals

func (d Decimal) Equals(other Decimal) bool

func (Decimal) Floor

func (d Decimal) Floor() Integer

func (Decimal) Format

func (d Decimal) Format() string

func (Decimal) IsExact

func (d Decimal) IsExact() bool

func (Decimal) Mul

func (d Decimal) Mul(other Decimal) Decimal

func (Decimal) Scale

func (d Decimal) Scale() int

func (Decimal) SetScale

func (d Decimal) SetScale(nsig int, ceiling bool) Decimal

func (Decimal) String

func (d Decimal) String() string

func (Decimal) Sub

func (d Decimal) Sub(other Decimal) Decimal

func (Decimal) System

func (d Decimal) System() NumeralSystem

type Integer

type Integer struct {
	// contains filtered or unexported fields
}

Integer is an arbitrary-precision integer in any base. Magnitude is stored little-endian (index 0 = least significant digit).

func NewInteger

func NewInteger(sys NumeralSystem, sign int, mag []int) Integer

NewInteger creates an Integer, trimming trailing zeros.

func One

func One(sys NumeralSystem) Integer

One returns the one Integer for the given system.

func ParseInteger

func ParseInteger(s string, sys NumeralSystem) (Integer, error)

ParseInteger parses a string into an Integer.

func Zero

func Zero(sys NumeralSystem) Integer

Zero returns the zero Integer for the given system.

func (Integer) Add

func (i Integer) Add(other Integer) Integer

func (Integer) CompareTo

func (i Integer) CompareTo(other Integer) int

func (Integer) Complement

func (i Integer) Complement() Integer

func (Integer) ComplementDigits

func (i Integer) ComplementDigits(digits int) Integer

func (Integer) Equals

func (i Integer) Equals(other Integer) bool

func (Integer) Format

func (i Integer) Format() string

func (Integer) IsOne

func (i Integer) IsOne() bool

func (Integer) IsZero

func (i Integer) IsZero() bool

func (Integer) MagAt

func (i Integer) MagAt(index int) int

func (Integer) Mul

func (i Integer) Mul(other Integer) Integer

func (Integer) Negate

func (i Integer) Negate() Integer

func (Integer) ShiftLeft

func (i Integer) ShiftLeft(times int) Integer

func (Integer) ShiftRight

func (i Integer) ShiftRight(times int) Integer

func (Integer) String

func (i Integer) String() string

func (Integer) Sub

func (i Integer) Sub(other Integer) Integer

func (Integer) System

func (i Integer) System() NumeralSystem

type NumeralSystem

type NumeralSystem interface {
	Base() int
	PositiveChar() byte
	NegativeChar() byte
	RadixPointChar() byte
	ToDigit(ch byte) (int, error)
	ToChar(digit int) byte
}

NumeralSystem defines the interface for a base-N numeral system.

var (
	System10 NumeralSystem = numeralSystem10{}
	System36 NumeralSystem = numeralSystem36{}
	System64 NumeralSystem = numeralSystem64{}
)

type Rank

type Rank struct {
	// contains filtered or unexported fields
}

Rank is a lexicographically sortable rank string.

func Initial

func Initial(bucket Bucket) Rank

Initial returns the initial rank for a bucket.

func Max

func Max(bucket ...Bucket) Rank

Max returns the maximum rank for the given bucket (default Bucket0).

func Middle

func Middle() Rank

Middle returns the midpoint between min and max.

func Min

func Min() Rank

Min returns the minimum rank.

func ParseRank

func ParseRank(s string) (Rank, error)

ParseRank parses a rank string like "0|000000:".

func RankFrom

func RankFrom(bucket Bucket, decimal Decimal) Rank

RankFrom creates a Rank from a bucket and decimal.

func (Rank) Between

func (r Rank) Between(other Rank) (Rank, error)

Between returns the midpoint rank between r and other. Returns an error if the ranks are in different buckets or have the same decimal.

func (Rank) CompareTo

func (r Rank) CompareTo(other Rank) int

func (Rank) Equals

func (r Rank) Equals(other Rank) bool

func (Rank) Format

func (r Rank) Format() string

func (Rank) GenNext

func (r Rank) GenNext() Rank

func (Rank) GenPrev

func (r Rank) GenPrev() Rank

func (Rank) GetBucket

func (r Rank) GetBucket() Bucket

func (Rank) GetDecimal

func (r Rank) GetDecimal() Decimal

func (Rank) InNextBucket

func (r Rank) InNextBucket() Rank

func (Rank) InPrevBucket

func (r Rank) InPrevBucket() Rank

func (Rank) IsMax

func (r Rank) IsMax() bool

func (Rank) IsMin

func (r Rank) IsMin() bool

func (Rank) String

func (r Rank) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL