From 57edf71b256e50b5555772907675e273179189e0 Mon Sep 17 00:00:00 2001 From: Prashan Dharmasena Date: Tue, 4 Jun 2024 14:44:51 -0400 Subject: [PATCH] use separate set of ids --- .../processor/base/MergeWithIds.kt | 42 ++++--------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/processor/base/MergeWithIds.kt b/src/commonMain/kotlin/exchange.dydx.abacus/processor/base/MergeWithIds.kt index 6b4451af9..925ba1014 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/processor/base/MergeWithIds.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/processor/base/MergeWithIds.kt @@ -8,46 +8,22 @@ fun mergeWithIds( existing: List, id: (Any) -> String?, ): List { - val merged = mutableSetOf() + val ids = mutableSetOf() + val merged = mutableListOf() new.forEach { item -> id(item)?.let { itemId -> - merged.add( - ItemWithId( - id = itemId, - item = item, - ), - ) + ids.add(itemId) + merged.add(item) } } existing.forEach { item -> id(item)?.let { itemId -> - merged.add( - ItemWithId( - id = itemId, - item = item, - ), - ) + if (!ids.contains(itemId)) { + ids.add(itemId) + merged.add(item) + } } } - return merged.map { it.item } -} - -// Wrapper for de-duping in set -private data class ItemWithId( - val id: String, - val item: Any, -) { - override fun hashCode(): Int { - return id.hashCode() - } - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other == null || this::class != other::class) return false - - other as ItemWithId - - return id == other.id - } + return merged }