-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update build.gradle and import remaining evaluation-core files
- Loading branch information
Tim Yiu
authored and
Tim Yiu
committed
Sep 21, 2023
1 parent
681edc3
commit 8bb7444
Showing
8 changed files
with
562 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
364 changes: 364 additions & 0 deletions
364
sdk/src/main/java/com/amplitude/evaluation-core/src/commonMain/kotlin/EvaluationEngine.kt
Large diffs are not rendered by default.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
sdk/src/main/java/com/amplitude/evaluation-core/src/commonMain/kotlin/EvaluationFlag.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
sdk/src/main/java/com/amplitude/evaluation-core/src/commonMain/kotlin/EvaluationSegment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
.../main/java/com/amplitude/evaluation-core/src/commonMain/kotlin/EvaluationSerialization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.amplitude.experiment.evaluation | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonArray | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonNull | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.json.booleanOrNull | ||
import kotlinx.serialization.json.contentOrNull | ||
import kotlinx.serialization.json.doubleOrNull | ||
import kotlinx.serialization.json.intOrNull | ||
import kotlinx.serialization.json.longOrNull | ||
import kotlin.jvm.JvmField | ||
import kotlin.jvm.JvmSynthetic | ||
|
||
@JvmSynthetic | ||
@JvmField | ||
internal val json = Json { | ||
ignoreUnknownKeys = true | ||
isLenient = true | ||
coerceInputValues = true | ||
explicitNulls = false | ||
} | ||
|
||
@JvmSynthetic | ||
internal fun Any?.toJsonElement(): JsonElement = when (this) { | ||
null -> JsonNull | ||
is Map<*, *> -> toJsonObject() | ||
is Collection<*> -> toJsonArray() | ||
is Boolean -> JsonPrimitive(this) | ||
is Number -> JsonPrimitive(this) | ||
is String -> JsonPrimitive(this) | ||
else -> JsonPrimitive(toString()) | ||
} | ||
|
||
@JvmSynthetic | ||
internal fun Collection<*>.toJsonArray(): JsonArray = JsonArray(map { it.toJsonElement() }) | ||
|
||
@JvmSynthetic | ||
internal fun Map<*, *>.toJsonObject(): JsonObject = JsonObject( | ||
mapNotNull { | ||
(it.key as? String ?: return@mapNotNull null) to it.value.toJsonElement() | ||
}.toMap(), | ||
) | ||
|
||
@JvmSynthetic | ||
internal fun JsonElement.toAny(): Any? { | ||
return when (this) { | ||
is JsonPrimitive -> toAny() | ||
is JsonArray -> toList() | ||
is JsonObject -> toMap() | ||
} | ||
} | ||
|
||
@JvmSynthetic | ||
internal fun JsonPrimitive.toAny(): Any? { | ||
return if (isString) { | ||
contentOrNull | ||
} else { | ||
booleanOrNull ?: intOrNull ?: longOrNull ?: doubleOrNull | ||
} | ||
} | ||
|
||
@JvmSynthetic | ||
internal fun JsonArray.toList(): List<Any?> = map { it.toAny() } | ||
|
||
@JvmSynthetic | ||
internal fun JsonObject.toMap(): Map<String, Any?> = mapValues { it.value.toAny() } | ||
|
||
internal object AnySerializer : KSerializer<Any?> { | ||
private val delegate = JsonElement.serializer() | ||
override val descriptor: SerialDescriptor | ||
get() = SerialDescriptor("Any", delegate.descriptor) | ||
|
||
override fun serialize(encoder: Encoder, value: Any?) { | ||
val jsonElement = value.toJsonElement() | ||
encoder.encodeSerializableValue(delegate, jsonElement) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): Any? { | ||
val jsonElement = decoder.decodeSerializableValue(delegate) | ||
return jsonElement.toAny() | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
sdk/src/main/java/com/amplitude/evaluation-core/src/commonMain/kotlin/EvaluationVariant.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
sdk/src/main/java/com/amplitude/evaluation-core/src/commonMain/kotlin/Murmur3.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.amplitude.experiment.evaluation | ||
|
||
internal object Murmur3 { | ||
|
||
private const val C1_32 = -0x3361d2af | ||
private const val C2_32 = 0x1b873593 | ||
private const val R1_32 = 15 | ||
private const val R2_32 = 13 | ||
private const val M_32 = 5 | ||
private const val N_32 = -0x19ab949c | ||
|
||
internal fun hash32x86(data: ByteArray, length: Int, seed: Int): Int { | ||
var hash = seed | ||
val nblocks = length shr 2 | ||
|
||
// body | ||
for (i in 0 until nblocks) { | ||
val index = (i shl 2) | ||
val k: Int = data.readIntLe(index) | ||
hash = mix32(k, hash) | ||
} | ||
|
||
// tail | ||
val index = (nblocks shl 2) | ||
var k1 = 0 | ||
|
||
when (length - index) { | ||
3 -> { | ||
k1 = k1 xor ((data[index + 2].toInt() and 0xff) shl 16) | ||
k1 = k1 xor ((data[index + 1].toInt() and 0xff) shl 8) | ||
k1 = k1 xor ((data[index].toInt() and 0xff)) | ||
|
||
// mix functions | ||
k1 *= C1_32 | ||
k1 = k1.rotateLeft(R1_32) | ||
k1 *= C2_32 | ||
hash = hash xor k1 | ||
} | ||
2 -> { | ||
k1 = k1 xor ((data[index + 1].toInt() and 0xff) shl 8) | ||
k1 = k1 xor ((data[index].toInt() and 0xff)) | ||
k1 *= C1_32 | ||
k1 = k1.rotateLeft(R1_32) | ||
k1 *= C2_32 | ||
hash = hash xor k1 | ||
} | ||
1 -> { | ||
k1 = k1 xor ((data[index].toInt() and 0xff)) | ||
k1 *= C1_32 | ||
k1 = k1.rotateLeft(R1_32) | ||
k1 *= C2_32 | ||
hash = hash xor k1 | ||
} | ||
} | ||
hash = hash xor length | ||
return fmix32(hash) | ||
} | ||
|
||
private fun mix32(k: Int, hash: Int): Int { | ||
var kResult = k | ||
var hashResult = hash | ||
kResult *= C1_32 | ||
kResult = kResult.rotateLeft(R1_32) | ||
kResult *= C2_32 | ||
hashResult = hashResult xor kResult | ||
return hashResult.rotateLeft( | ||
R2_32 | ||
) * M_32 + N_32 | ||
} | ||
|
||
private fun fmix32(hash: Int): Int { | ||
var hashResult = hash | ||
hashResult = hashResult xor (hashResult ushr 16) | ||
hashResult *= -0x7a143595 | ||
hashResult = hashResult xor (hashResult ushr 13) | ||
hashResult *= -0x3d4d51cb | ||
hashResult = hashResult xor (hashResult ushr 16) | ||
return hashResult | ||
} | ||
|
||
private fun Int.reverseBytes(): Int { | ||
return (this and -0x1000000 ushr 24) or | ||
(this and 0x00ff0000 ushr 8) or | ||
(this and 0x0000ff00 shl 8) or | ||
(this and 0x000000ff shl 24) | ||
} | ||
|
||
private fun ByteArray.readIntLe(index: Int = 0): Int { | ||
return ( | ||
this[index].toInt() and 0xff shl 24 | ||
or (this[index + 1].toInt() and 0xff shl 16) | ||
or (this[index + 2].toInt() and 0xff shl 8) | ||
or (this[index + 3].toInt() and 0xff) | ||
).reverseBytes() | ||
} | ||
} |