Scoop API

scoop:scoop.core:0.1.0

src/types.scoop

Selected production source · src/types.scoop

1/**
2 * The operator protocol for same-domain value equality. Implementations used with Hash must assign
3 * equal values equal hashes.
4 */
5public interface Equals<T> {
6 /**
7 * Returns whether this value and the operand are equal under the implementing type's contract.
8 */
9 public operator fun equals(other: T): Boolean
12/**
13 * Provides a human-readable textual representation without prescribing a serialization format.
14 */
15public interface ToString {
16 /**
17 * Returns the value's textual representation.
18 */
19 public fun toString(): String
22/**
23 * Provides a Long hash consistent with the type's value equality; it is not a cryptographic
24 * digest.
25 */
26public interface Hash {
27 /**
28 * Returns the value's hash. Equal values must produce equal hashes.
29 */
30 public fun hash(): Long
33/**
34 * A transparent alias for [Int8] with the same representation and operations.
35 */
36public typealias Byte = Int8
37/**
38 * A transparent alias for [Int16] with the same representation and operations.
39 */
40public typealias Short = Int16
41/**
42 * A transparent alias for [Int] with the same representation and operations.
43 */
44public typealias Int32 = Int
45/**
46 * A transparent alias for [Long] with the same representation and operations.
47 */
48public typealias Int64 = Long
49/**
50 * A transparent alias for [UInt8] with the same representation and operations.
51 */
52public typealias UByte = UInt8
53/**
54 * A transparent alias for [UInt16] with the same representation and operations.
55 */
56public typealias UShort = UInt16
57/**
58 * A transparent alias for [UInt] with the same representation and operations.
59 */
60public typealias UInt32 = UInt
61/**
62 * A transparent alias for [ULong] with the same representation and operations.
63 */
64public typealias UInt64 = ULong
66/**
67 * The two logical values true and false, with Boolean negation and scalar codec support.
68 */
69@Intrinsic("core_boolean")
70public struct Boolean : Not<Boolean>, Equals<Boolean>, ToString, Hash {
71 /**
72 * Compares the two logical values.
73 */
74 public override operator fun equals(other: Boolean): Boolean =
75 if (this) { other } else { !other }
78 /**
79 * Returns false for true and true for false.
80 */
81 @Intrinsic("boolean_not")
82 public override operator fun not(): Boolean
84 /**
85 * Returns `true` or `false`.
86 */
87 public override fun toString(): String = if (this) { "true" } else { "false" }
89 /**
90 * Returns a hash consistent with Boolean equality.
91 */
92 public override fun hash(): Long = if (this) { 1L } else { 0L }
94 /**
95 * Inspection and serialization operations for this value type.
96 */
97 public companion object : ValueEncoder<Boolean>, ValueDecoder<Boolean>, ValueInspector<Boolean> {
98 /**
99 * Exposes the value as a typed scalar observation.
100 */
101 public override fun inspect(value: Boolean): ValueView =
102 ValueObservation(typeInfo<Boolean>().type, None, { ValueShape.Scalar(ScalarValue.Boolean(value)) })
104 /**
105 * Writes the scalar value through the corresponding single-value encoding operation.
106 */
107 public override fun encode(value: Boolean, encoder: Encoder): Unit =
108 encoder.singleValue().writeBoolean(value)
110 /**
111 * Reads the scalar through the corresponding decoding operation.
112 */
113 public override fun decode(decoder: Decoder): Boolean =
114 decoder.singleValue().readBoolean()
115 }
118// Preserve the existing word hash using fixed-width wrapping arithmetic.
119@NoGC
120internal fun coreLongHash(input: Long): Long = coreULongHash(input.toULong())
122@NoGC
123internal fun coreULongHash(input: ULong): Long {
124 var value = input
125 value = value.xor(value.shr(30L)) * 0xbf58476d1ce4e5b9uL
126 value = value.xor(value.shr(27L)) * 0x94d049bb133111ebuL
127 return value.xor(value.shr(31L)).toLong()