From 6651b8c806f2cc9a0bd85132b6532f57865caab4 Mon Sep 17 00:00:00 2001 From: Eric Eilebrecht Date: Tue, 17 Oct 2023 13:16:57 -0700 Subject: [PATCH] WIP --- .../kotlin/com/certora/collect/TreapSet.kt | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/collect/src/main/kotlin/com/certora/collect/TreapSet.kt b/collect/src/main/kotlin/com/certora/collect/TreapSet.kt index 10a36ea..2b29340 100644 --- a/collect/src/main/kotlin/com/certora/collect/TreapSet.kt +++ b/collect/src/main/kotlin/com/certora/collect/TreapSet.kt @@ -3,7 +3,7 @@ package com.certora.collect import kotlinx.collections.immutable.PersistentSet /** - A [PersistentSet] implemented as a [Treap](https://en.wikipedia.org/wiki/Treap) - a kind of balanced binary tree. + A [PersistentSet] implemented as a [Treap](https://en.wikipedia.org/wiki/Treap) - a kind of balanced binary tree. */ @Treapable public interface TreapSet : PersistentSet { @@ -16,7 +16,7 @@ public interface TreapSet : PersistentSet { override fun clear(): TreapSet /** - A [PersistentSet.Builder] that produces a [TreapSet]. + A [PersistentSet.Builder] that produces a [TreapSet]. */ public interface Builder : PersistentSet.Builder { override fun build(): TreapSet @@ -25,16 +25,48 @@ public interface TreapSet : PersistentSet { @Suppress("Treapability") override fun builder(): Builder<@UnsafeVariance T> = TreapSetBuilder(this) + /** + Checks if this set contains any of the given [elements]. This is equivalent to, but more efficient than, + `this.intersect(elements).isNotEmpty()`. + */ public fun containsAny(elements: Iterable<@UnsafeVariance T>): Boolean + + /** + If this set contains exactly one element, returns that element. Otherwise, throws [NoSuchElementException]. + */ public fun single(): T + + /** + If this set contains exactly one element, returns that element. Otherwise, returns null. + */ public fun singleOrNull(): T? + + /** + If this set contains an element that compares equal to the specified [element], returns that element instance. + This is useful for implementing intern tables, for example. + */ public fun findEqual(element: @UnsafeVariance T): T? + + /** + Calls [action] for each element in the set. This traverses the treap without allocating temporary storage, + which may be more efficient than [forEach]. + */ public fun forEachElement(action: (element: T) -> Unit): Unit } - +/** + Returns an empty [TreapSet] + */ public fun <@Treapable T> treapSetOf(): TreapSet = EmptyTreapSet() + +/** + Returns a new [TreapSet] containing the given element. + */ public fun <@Treapable T> treapSetOf(element: T): TreapSet = treapSetOf().add(element) + +/** + Returns a new [TreapSet] containing the given elements. + */ public fun <@Treapable T> treapSetOf(vararg elements: T): TreapSet = treapSetOf().mutate { it.addAll(elements) } public fun <@Treapable T> treapSetOfNotNull(element: T?): TreapSet = if (element != null) { treapSetOf(element) } else { treapSetOf() }