Add hashCode() for primitives. (#104)
This commit is contained in:
@@ -443,6 +443,11 @@ task interface0(type: RunKonanTest) {
|
||||
source = "runtime/basic/interface0.kt"
|
||||
}
|
||||
|
||||
task hash0(type: RunKonanTest) {
|
||||
goldValue = "239\n0\n97\n1065353216\n1072693248\n1\n0\ntrue\ntrue\n"
|
||||
source = "runtime/basic/hash0.kt"
|
||||
}
|
||||
|
||||
task array_list1(type: RunKonanTest) {
|
||||
goldValue = "OK\n"
|
||||
source = "runtime/collections/array_list1.kt"
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
fun main(args : Array<String>) {
|
||||
println(239.hashCode())
|
||||
println((-1L).hashCode())
|
||||
println('a'.hashCode())
|
||||
println(1.0f.hashCode())
|
||||
println(1.0.hashCode())
|
||||
println(true.hashCode())
|
||||
println(false.hashCode())
|
||||
println(Any().hashCode() != Any().hashCode())
|
||||
val a = CharArray(5)
|
||||
a[0] = 'H'
|
||||
a[1] = 'e'
|
||||
a[2] = 'l'
|
||||
a[3] = 'l'
|
||||
a[4] = 'o'
|
||||
// Note that it uses private Konan API.
|
||||
println("Hello".hashCode() == fromCharArray(a, 0, 5).hashCode())
|
||||
}
|
||||
@@ -248,8 +248,13 @@ KLong Kotlin_Long_dec (KLong a ) { return --a; }
|
||||
KLong Kotlin_Long_unaryPlus (KLong a ) { return +a; }
|
||||
KLong Kotlin_Long_unaryMinus (KLong a ) { return -a; }
|
||||
|
||||
KLong Kotlin_Long_xor_Long (KLong a, KLong b) { return a^b; }
|
||||
KLong Kotlin_Long_ushr_Long (KLong a, KLong b) { return a >> b; }
|
||||
KLong Kotlin_Long_xor_Long (KLong a, KLong b) { return a ^ b; }
|
||||
KLong Kotlin_Long_shr_Int (KLong a, KInt b) {
|
||||
return a >> b;
|
||||
}
|
||||
KLong Kotlin_Long_ushr_Int (KLong a, KInt b) {
|
||||
return static_cast<uint64_t>(a) >> b;
|
||||
}
|
||||
|
||||
KByte Kotlin_Long_toByte (KLong a ) { return a; }
|
||||
KShort Kotlin_Long_toShort (KLong a ) { return a; }
|
||||
@@ -314,6 +319,15 @@ KLong Kotlin_Float_toLong (KFloat a ) { return a; }
|
||||
KFloat Kotlin_Float_toFloat (KFloat a ) { return a; }
|
||||
KDouble Kotlin_Float_toDouble (KFloat a ) { return a; }
|
||||
|
||||
KInt Kotlin_Float_bits (KFloat a) {
|
||||
union {
|
||||
KFloat f;
|
||||
KInt i;
|
||||
} alias;
|
||||
alias.f = a;
|
||||
return alias.i;
|
||||
}
|
||||
|
||||
//--- Double ------------------------------------------------------------------//
|
||||
|
||||
KInt Kotlin_Double_compareTo_Byte (KDouble a, KByte b) { if (a == b) return 0; return (a < b) ? -1 : 1; }
|
||||
@@ -369,4 +383,14 @@ KInt Kotlin_Double_toInt (KDouble a ) { return a; }
|
||||
KLong Kotlin_Double_toLong (KDouble a ) { return a; }
|
||||
KFloat Kotlin_Double_toFloat (KDouble a ) { return a; }
|
||||
KDouble Kotlin_Double_toDouble (KDouble a ) { return a; }
|
||||
} // extern "C"
|
||||
|
||||
KLong Kotlin_Double_bits (KDouble a) {
|
||||
union {
|
||||
KDouble d;
|
||||
KLong l;
|
||||
} alias;
|
||||
alias.d = a;
|
||||
return alias.l;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
@@ -35,4 +35,8 @@ public final class Boolean : Comparable<Boolean> {
|
||||
// Konan-specific.
|
||||
@SymbolName("Kotlin_Boolean_toString")
|
||||
external public override fun toString(): String
|
||||
|
||||
public override fun hashCode(): Int {
|
||||
return if (this) 1 else 0;
|
||||
}
|
||||
}
|
||||
@@ -94,5 +94,9 @@ public final class Char : Comparable<Char> {
|
||||
// Konan-specific.
|
||||
@SymbolName("Kotlin_Char_toString")
|
||||
external public override fun toString(): String
|
||||
|
||||
public override fun hashCode(): Int {
|
||||
return this.toInt();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -203,6 +203,10 @@ public final class Byte : Number(), Comparable<Byte> {
|
||||
// Konan-specific.
|
||||
@SymbolName("Kotlin_Byte_toString")
|
||||
external public override fun toString(): String
|
||||
|
||||
public override fun hashCode(): Int {
|
||||
return this.toInt()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -408,6 +412,10 @@ public final class Short : Number(), Comparable<Short> {
|
||||
// Konan-specific.
|
||||
@SymbolName("Kotlin_Short_toString")
|
||||
external public override fun toString(): String
|
||||
|
||||
public override fun hashCode(): Int {
|
||||
return this.toInt()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -635,6 +643,10 @@ public final class Int : Number(), Comparable<Int> {
|
||||
// Konan-specific.
|
||||
@SymbolName("Kotlin_Int_toString")
|
||||
external public override fun toString(): String
|
||||
|
||||
public override fun hashCode(): Int {
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -826,10 +838,10 @@ public final class Long : Number(), Comparable<Long> {
|
||||
@SymbolName("Kotlin_Long_shl_Long")
|
||||
external public infix fun shl(bitCount: Int): Long
|
||||
/** Shifts this value right by [bits], filling the leftmost bits with copies of the sign bit. */
|
||||
@SymbolName("Kotlin_Long_shr_Long")
|
||||
@SymbolName("Kotlin_Long_shr_Int")
|
||||
external public infix fun shr(bitCount: Int): Long
|
||||
/** Shifts this value right by [bits], filling the leftmost bits with zeros. */
|
||||
@SymbolName("Kotlin_Long_ushr_Long")
|
||||
@SymbolName("Kotlin_Long_ushr_Int")
|
||||
external public infix fun ushr(bitCount: Int): Long
|
||||
/** Performs a bitwise AND operation between the two values. */
|
||||
@SymbolName("Kotlin_Long_and_Long")
|
||||
@@ -862,6 +874,10 @@ public final class Long : Number(), Comparable<Long> {
|
||||
// Konan-specific.
|
||||
@SymbolName("Kotlin_Long_toString")
|
||||
external public override fun toString(): String
|
||||
|
||||
public override fun hashCode(): Int {
|
||||
return ((this ushr 32) xor this).toInt()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1047,7 +1063,6 @@ public final class Float : Number(), Comparable<Float> {
|
||||
@SymbolName("Kotlin_Float_unaryMinus")
|
||||
external public operator fun unaryMinus(): Float
|
||||
|
||||
|
||||
@SymbolName("Kotlin_Float_toByte")
|
||||
external public override fun toByte(): Byte
|
||||
@SymbolName("Kotlin_Float_toChar")
|
||||
@@ -1066,6 +1081,13 @@ public final class Float : Number(), Comparable<Float> {
|
||||
// Konan-specific.
|
||||
@SymbolName("Kotlin_Float_toString")
|
||||
external public override fun toString(): String
|
||||
|
||||
public override fun hashCode(): Int {
|
||||
return bits()
|
||||
}
|
||||
|
||||
@SymbolName("Kotlin_Float_bits")
|
||||
external public fun bits(): Int
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1270,4 +1292,11 @@ public final class Double : Number(), Comparable<Double> {
|
||||
// Konan-specific.
|
||||
@SymbolName("Kotlin_Double_toString")
|
||||
external public override fun toString(): String
|
||||
|
||||
public override fun hashCode(): Int {
|
||||
return bits().hashCode()
|
||||
}
|
||||
|
||||
@SymbolName("Kotlin_Double_bits")
|
||||
external public fun bits(): Long
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user