OrderedCodec

org.sigilaris.core.codec.OrderedCodec
See theOrderedCodec companion object
trait OrderedCodec[A] extends ByteCodec[A], Ordering[A]

OrderedCodec extends ByteCodec with an ordering preservation law.

Law: compare(x, y) ≡ encode(x).compare(encode(y))

This ensures that the encoded byte representation preserves the ordering relationship of the original values. This property is critical for blockchain applications where prefix-free encoding and key ordering matter.

The law states: For all x, y of type A, sign(compare(x, y)) == sign(encode(x).compare(encode(y)))

Where sign(n) is:

  • negative if n < 0
  • zero if n == 0
  • positive if n > 0

Attributes

Example
 val codec = OrderedCodec[Long]
 val x     = -100L
 val y     = 200L
 // The law guarantees:
 // x.compare(y) < 0  ⟹  encode(x).compare(encode(y)) < 0
Companion
object
Source
OrderedCodec.scala
Graph
Supertypes
trait Ordering[A]
trait PartialOrdering[A]
trait Equiv[A]
trait Serializable
trait Comparator[A]
trait ByteCodec[A]
trait ByteEncoder[A]
trait ByteDecoder[A]
class Object
trait Matchable
class Any
Show all

Members list

Type members

Inherited classlikes

class OrderingOps(lhs: Ordering.this.T)

Attributes

Inherited from:
Ordering
Supertypes
class Object
trait Matchable
class Any

Value members

Concrete methods

def satisfiesLaw(x: A, y: A): Boolean

Verifies the ordering preservation law for two values.

Verifies the ordering preservation law for two values.

Value parameters

x

first value

y

second value

Attributes

Returns

true if the law holds for this pair

Source
OrderedCodec.scala

Inherited methods

def contramap[B](f: B => A): ByteEncoder[B]

Creates a new encoder by applying a function before encoding.

Creates a new encoder by applying a function before encoding.

This is the contravariant map operation for encoders.

Value parameters

f

function to transform input before encoding

Attributes

Returns

new encoder for type B

Example
 case class UserId(value: Long)
 given ByteEncoder[UserId] = ByteEncoder[Long].contramap(_.value)
Inherited from:
ByteEncoder
Source
ByteEncoder.scala
def emap[B](f: A => Either[DecodeFailure, B]): ByteDecoder[B]

Creates a new decoder with validation.

Creates a new decoder with validation.

Value parameters

f

function that may fail with DecodeFailure

Attributes

Returns

new decoder for type B

Example
 val positiveDecoder = ByteDecoder[Long].emap { n =>
   Either.cond(n > 0, n, DecodeFailure("Must be positive"))
 }
Inherited from:
ByteDecoder
Source
ByteDecoder.scala
override def equiv(x: A, y: A): Boolean

Attributes

Definition Classes
Ordering -> PartialOrdering -> Equiv
Inherited from:
Ordering
def flatMap[B](f: A => ByteDecoder[B]): ByteDecoder[B]

Creates a new decoder by chaining decoders.

Creates a new decoder by chaining decoders.

Value parameters

f

function that creates a decoder based on decoded value

Attributes

Returns

new decoder for type B

Inherited from:
ByteDecoder
Source
ByteDecoder.scala
override def gt(x: A, y: A): Boolean

Attributes

Definition Classes
Ordering -> PartialOrdering
Inherited from:
Ordering
override def gteq(x: A, y: A): Boolean

Attributes

Definition Classes
Ordering -> PartialOrdering
Inherited from:
Ordering
def imap[B](to: A => B, from: B => A): ByteCodec[B]

Maps a codec through an isomorphism.

Maps a codec through an isomorphism.

Useful for opaque wrappers whose representation already has a codec.

Type parameters

B

the target type

Value parameters

from

conversion from B to A

to

conversion from A to B

Attributes

Returns

a new ByteCodec for type B

Inherited from:
ByteCodec
Source
ByteCodec.scala
def isReverseOf(other: Ordering[_]): Boolean

Attributes

Inherited from:
Ordering
override def lt(x: A, y: A): Boolean

Attributes

Definition Classes
Ordering -> PartialOrdering
Inherited from:
Ordering
override def lteq(x: A, y: A): Boolean

Attributes

Definition Classes
Ordering -> PartialOrdering
Inherited from:
Ordering
def map[B](f: A => B): ByteDecoder[B]

Creates a new decoder by transforming the decoded value.

Creates a new decoder by transforming the decoded value.

Value parameters

f

function to transform decoded value

Attributes

Returns

new decoder for type B

Inherited from:
ByteDecoder
Source
ByteDecoder.scala
def max[U <: A](x: U, y: U): U

Attributes

Inherited from:
Ordering
def min[U <: A](x: U, y: U): U

Attributes

Inherited from:
Ordering
def on[U](f: U => A): Ordering[U]

Attributes

Inherited from:
Ordering
def orElse(other: Ordering[A]): Ordering[A]

Attributes

Inherited from:
Ordering
def orElseBy[S](f: A => S)(implicit ord: Ordering[S]): Ordering[A]

Attributes

Inherited from:
Ordering
override def reverse: Ordering[A]

Attributes

Definition Classes
Ordering -> PartialOrdering
Inherited from:
Ordering
def reversed(): Comparator[A]

Attributes

Inherited from:
Comparator
def thenComparing[U <: Comparable[_ >: U <: <FromJavaObject>]](x$0: Function[_ >: A <: <FromJavaObject>, _ <: U]): Comparator[A]

Attributes

Inherited from:
Comparator
def thenComparing[U <: <FromJavaObject>](x$0: Function[_ >: A <: <FromJavaObject>, _ <: U], x$1: Comparator[_ >: U <: <FromJavaObject>]): Comparator[A]

Attributes

Inherited from:
Comparator
def thenComparing(x$0: Comparator[_ >: A <: <FromJavaObject>]): Comparator[A]

Attributes

Inherited from:
Comparator
def thenComparingDouble(x$0: ToDoubleFunction[_ >: A <: <FromJavaObject>]): Comparator[A]

Attributes

Inherited from:
Comparator
def thenComparingInt(x$0: ToIntFunction[_ >: A <: <FromJavaObject>]): Comparator[A]

Attributes

Inherited from:
Comparator
def thenComparingLong(x$0: ToLongFunction[_ >: A <: <FromJavaObject>]): Comparator[A]

Attributes

Inherited from:
Comparator
def tryCompare(x: A, y: A): Some[Int]

Attributes

Inherited from:
Ordering
def widen[AA >: A]: ByteDecoder[AA]

Widens the decoder to a supertype.

Widens the decoder to a supertype.

Type parameters

AA

the supertype

Attributes

Returns

a decoder for the wider type

Inherited from:
ByteDecoder
Source
ByteDecoder.scala

Inherited and Abstract methods

def compare(x: A, y: A): Int

Attributes

Inherited from:
Ordering
def decode(bytes: ByteVector): Either[DecodeFailure, DecodeResult[A]]

Decodes bytes to a value.

Decodes bytes to a value.

Value parameters

bytes

the byte sequence to decode

Attributes

Returns

Either failure or decoded value with remainder

Inherited from:
ByteDecoder
Source
ByteDecoder.scala
def encode(value: A): ByteVector

Encodes a value to a deterministic byte sequence.

Encodes a value to a deterministic byte sequence.

Value parameters

value

the value to encode

Attributes

Returns

the encoded byte sequence

Inherited from:
ByteEncoder
Source
ByteEncoder.scala

Implicits

Inherited implicits

implicit def mkOrderingOps(lhs: A): OrderingOps

Attributes

Inherited from:
Ordering