From 5c8e50c3dfb80b843d9db078beca354d59c4c8d4 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 7 Jan 2025 22:30:25 +0300 Subject: [PATCH] Move attributes to separate project --- attributes-kt/api/attributes-kt.api | 104 ------------ attributes-kt/build.gradle.kts | 20 --- .../space/kscience/attributes/Attribute.kt | 29 ---- .../kscience/attributes/AttributeContainer.kt | 20 --- .../space/kscience/attributes/Attributes.kt | 157 ------------------ .../kscience/attributes/AttributesBuilder.kt | 68 -------- .../space/kscience/attributes/annotations.kt | 17 -- kmath-core/build.gradle.kts | 2 +- 8 files changed, 1 insertion(+), 416 deletions(-) delete mode 100644 attributes-kt/api/attributes-kt.api delete mode 100644 attributes-kt/build.gradle.kts delete mode 100644 attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attribute.kt delete mode 100644 attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributeContainer.kt delete mode 100644 attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt delete mode 100644 attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt delete mode 100644 attributes-kt/src/commonMain/kotlin/space/kscience/attributes/annotations.kt diff --git a/attributes-kt/api/attributes-kt.api b/attributes-kt/api/attributes-kt.api deleted file mode 100644 index c735ad1dc..000000000 --- a/attributes-kt/api/attributes-kt.api +++ /dev/null @@ -1,104 +0,0 @@ -public abstract interface class space/kscience/attributes/Attribute { -} - -public abstract interface class space/kscience/attributes/AttributeContainer { - public abstract fun getAttributes ()Lspace/kscience/attributes/Attributes; -} - -public abstract interface class space/kscience/attributes/AttributeScope { -} - -public abstract interface class space/kscience/attributes/AttributeWithDefault : space/kscience/attributes/Attribute { - public abstract fun getDefault ()Ljava/lang/Object; -} - -public abstract interface class space/kscience/attributes/Attributes { - public static final field Companion Lspace/kscience/attributes/Attributes$Companion; - public abstract fun equals (Ljava/lang/Object;)Z - public fun get (Lspace/kscience/attributes/Attribute;)Ljava/lang/Object; - public abstract fun getContent ()Ljava/util/Map; - public fun getKeys ()Ljava/util/Set; - public abstract fun hashCode ()I - public abstract fun toString ()Ljava/lang/String; -} - -public final class space/kscience/attributes/Attributes$Companion { - public final fun equals (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/Attributes;)Z - public final fun getEMPTY ()Lspace/kscience/attributes/Attributes; -} - -public final class space/kscience/attributes/AttributesBuilder : space/kscience/attributes/Attributes { - public final fun add (Lspace/kscience/attributes/SetAttribute;Ljava/lang/Object;)V - public final fun build ()Lspace/kscience/attributes/Attributes; - public fun equals (Ljava/lang/Object;)Z - public fun getContent ()Ljava/util/Map; - public fun hashCode ()I - public final fun invoke (Lspace/kscience/attributes/Attribute;Ljava/lang/Object;)V - public final fun put (Lspace/kscience/attributes/Attribute;Ljava/lang/Object;)V - public final fun putAll (Lspace/kscience/attributes/Attributes;)V - public final fun remove (Lspace/kscience/attributes/SetAttribute;Ljava/lang/Object;)V - public final fun set (Lspace/kscience/attributes/Attribute;Ljava/lang/Object;)V - public fun toString ()Ljava/lang/String; -} - -public final class space/kscience/attributes/AttributesBuilderKt { - public static final fun Attributes (Lkotlin/jvm/functions/Function1;)Lspace/kscience/attributes/Attributes; -} - -public final class space/kscience/attributes/AttributesKt { - public static final fun Attributes (Lspace/kscience/attributes/Attribute;)Lspace/kscience/attributes/Attributes; - public static final fun Attributes (Lspace/kscience/attributes/Attribute;Ljava/lang/Object;)Lspace/kscience/attributes/Attributes; - public static final fun getOrDefault (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/AttributeWithDefault;)Ljava/lang/Object; - public static final fun isEmpty (Lspace/kscience/attributes/Attributes;)Z - public static final fun modified (Lspace/kscience/attributes/Attributes;Lkotlin/jvm/functions/Function1;)Lspace/kscience/attributes/Attributes; - public static final fun plus (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/Attributes;)Lspace/kscience/attributes/Attributes; - public static final fun withAttribute (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/Attribute;)Lspace/kscience/attributes/Attributes; - public static final fun withAttribute (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/Attribute;Ljava/lang/Object;)Lspace/kscience/attributes/Attributes; - public static final fun withAttributeElement (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/SetAttribute;Ljava/lang/Object;)Lspace/kscience/attributes/Attributes; - public static final fun withoutAttribute (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/Attribute;)Lspace/kscience/attributes/Attributes; - public static final fun withoutAttributeElement (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/SetAttribute;Ljava/lang/Object;)Lspace/kscience/attributes/Attributes; -} - -public abstract interface class space/kscience/attributes/FlagAttribute : space/kscience/attributes/Attribute { -} - -public abstract class space/kscience/attributes/PolymorphicAttribute : space/kscience/attributes/Attribute { - public synthetic fun (Lkotlin/reflect/KType;Lkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun equals (Ljava/lang/Object;)Z - public final fun getType-V0oMfBY ()Lkotlin/reflect/KType; - public fun hashCode ()I -} - -public final class space/kscience/attributes/PolymorphicAttributeKt { - public static final fun get (Lspace/kscience/attributes/Attributes;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object; - public static final fun set (Lspace/kscience/attributes/AttributesBuilder;Lkotlin/jvm/functions/Function0;Ljava/lang/Object;)V -} - -public final class space/kscience/attributes/SafeType { - public static final synthetic fun box-impl (Lkotlin/reflect/KType;)Lspace/kscience/attributes/SafeType; - public static fun constructor-impl (Lkotlin/reflect/KType;)Lkotlin/reflect/KType; - public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl (Lkotlin/reflect/KType;Ljava/lang/Object;)Z - public static final fun equals-impl0 (Lkotlin/reflect/KType;Lkotlin/reflect/KType;)Z - public final fun getKType ()Lkotlin/reflect/KType; - public fun hashCode ()I - public static fun hashCode-impl (Lkotlin/reflect/KType;)I - public fun toString ()Ljava/lang/String; - public static fun toString-impl (Lkotlin/reflect/KType;)Ljava/lang/String; - public final synthetic fun unbox-impl ()Lkotlin/reflect/KType; -} - -public final class space/kscience/attributes/SafeTypeKt { - public static final fun getKClass-X0YbwmU (Lkotlin/reflect/KType;)Lkotlin/reflect/KClass; -} - -public abstract interface class space/kscience/attributes/SetAttribute : space/kscience/attributes/Attribute { -} - -public abstract interface annotation class space/kscience/attributes/UnstableAttributesAPI : java/lang/annotation/Annotation { -} - -public abstract interface class space/kscience/attributes/WithType { - public abstract fun getType-V0oMfBY ()Lkotlin/reflect/KType; -} - diff --git a/attributes-kt/build.gradle.kts b/attributes-kt/build.gradle.kts deleted file mode 100644 index 555567aac..000000000 --- a/attributes-kt/build.gradle.kts +++ /dev/null @@ -1,20 +0,0 @@ -plugins { - id("space.kscience.gradle.mpp") - `maven-publish` -} - -version = rootProject.extra.get("attributesVersion").toString() - -kscience { - jvm() - js() - native() - wasm() -} - -readme { - maturity = space.kscience.gradle.Maturity.DEVELOPMENT - description = """ - An API and basic implementation for arranging objects in a continuous memory block. - """.trimIndent() -} diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attribute.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attribute.kt deleted file mode 100644 index 9142cafdb..000000000 --- a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attribute.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2018-2023 KMath contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package space.kscience.attributes - -/** - * A marker interface for an attribute. Attributes are used as keys to access contents of type [T] in the container. - */ -public interface Attribute - -/** - * An attribute that could be either present or absent - */ -public interface FlagAttribute : Attribute - -/** - * An attribute with a default value - */ -public interface AttributeWithDefault : Attribute { - public val default: T -} - -/** - * Attribute containing a set of values - */ -public interface SetAttribute : Attribute> - diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributeContainer.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributeContainer.kt deleted file mode 100644 index 734887bdb..000000000 --- a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributeContainer.kt +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2018-2023 KMath contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package space.kscience.attributes - -/** - * A container for [Attributes] - */ -public interface AttributeContainer { - public val attributes: Attributes -} - -/** - * A scope, where attribute keys could be resolved. - * [O] is used only to resolve types in compile-time. - */ -public interface AttributeScope - diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt deleted file mode 100644 index 936cf2ee2..000000000 --- a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright 2018-2023 KMath contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package space.kscience.attributes - -/** - * A set of attributes. The implementation must guarantee that [content] keys correspond to their value types. - */ -public interface Attributes { - /** - * Raw content for this [Attributes] - */ - public val content: Map, Any?> - - /** - * Attribute keys contained in this [Attributes] - */ - public val keys: Set> get() = content.keys - - /** - * Provide an attribute value. Return null if attribute is not present or if its value is null. - */ - @Suppress("UNCHECKED_CAST") - public operator fun get(attribute: Attribute): T? = content[attribute] as? T - - override fun toString(): String - override fun equals(other: Any?): Boolean - override fun hashCode(): Int - - public companion object { - public val EMPTY: Attributes = object : Attributes { - override val content: Map, Any?> get() = emptyMap() - - override fun toString(): String = "Attributes.EMPTY" - - override fun equals(other: Any?): Boolean = (other as? Attributes)?.isEmpty() ?: false - - override fun hashCode(): Int = Unit.hashCode() - } - - public fun equals(a1: Attributes, a2: Attributes): Boolean = - a1.keys == a2.keys && a1.keys.all { a1[it] == a2[it] } - } -} - -internal class MapAttributes(override val content: Map, Any?>) : Attributes { - override fun toString(): String = "Attributes(value=${content.entries})" - override fun equals(other: Any?): Boolean = other is Attributes && Attributes.equals(this, other) - override fun hashCode(): Int = content.hashCode() -} - -public fun Attributes.isEmpty(): Boolean = keys.isEmpty() - -/** - * Get attribute value or default - */ -public fun Attributes.getOrDefault(attribute: AttributeWithDefault): T = get(attribute) ?: attribute.default - -/** - * Check if there is an attribute that matches given key by type and adheres to [predicate]. - */ -@Suppress("UNCHECKED_CAST") -public inline fun > Attributes.hasAny(predicate: (value: T) -> Boolean): Boolean = - content.any { (mapKey, mapValue) -> mapKey is A && predicate(mapValue as T) } - -/** - * Check if there is an attribute of given type (subtypes included) - */ -public inline fun > Attributes.hasAny(): Boolean = - content.any { (mapKey, _) -> mapKey is A } - -/** - * Check if [Attributes] contains a flag. Multiple keys that are instances of a flag could be present - */ -public inline fun Attributes.hasFlag(): Boolean = - content.keys.any { it is A } - -/** - * Create [Attributes] with an added or replaced attribute key. - */ -public fun > Attributes.withAttribute( - attribute: A, - attrValue: T, -): Attributes = MapAttributes(content + (attribute to attrValue)) - -public fun > Attributes.withAttribute(attribute: A): Attributes = - withAttribute(attribute, Unit) - -/** - * Create a new [Attributes] by modifying the current one - */ -public fun Attributes.modified(block: AttributesBuilder.() -> Unit): Attributes = Attributes { - putAll(this@modified) - block() -} - -/** - * Create new [Attributes] by removing [attribute] key - */ -public fun Attributes.withoutAttribute(attribute: Attribute<*>): Attributes = MapAttributes(content.minus(attribute)) - -/** - * Add an element to a [SetAttribute] - */ -public fun > Attributes.withAttributeElement( - attribute: A, - attrValue: T, -): Attributes { - val currentSet: Set = get(attribute) ?: emptySet() - return MapAttributes( - content + (attribute to (currentSet + attrValue)) - ) -} - -/** - * Remove an element from [SetAttribute] - */ -public fun > Attributes.withoutAttributeElement( - attribute: A, - attrValue: T, -): Attributes { - val currentSet: Set = get(attribute) ?: emptySet() - return MapAttributes(content + (attribute to (currentSet - attrValue))) -} - -/** - * Create [Attributes] with a single key - */ -public fun > Attributes( - attribute: A, - attrValue: T, -): Attributes = MapAttributes(mapOf(attribute to attrValue)) - -/** - * Create Attributes with a single [Unit] valued attribute - */ -public fun > Attributes( - attribute: A, -): Attributes = MapAttributes(mapOf(attribute to Unit)) - -/** - * Create a new [Attributes] that overlays [other] on top of this set of attributes. New attributes are added. - * Existing attribute keys are replaced. - */ -public operator fun Attributes.plus(other: Attributes): Attributes = when { - isEmpty() -> other - other.isEmpty() -> this - else -> MapAttributes(content + other.content) -} - -/** - * Create a new [Attributes] with removed [key] (if it is present). - */ -public operator fun Attributes.minus(key: Attribute<*>): Attributes = - if (content.contains(key)) MapAttributes(content.minus(key)) else this \ No newline at end of file diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt deleted file mode 100644 index ecf3da6f2..000000000 --- a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2018-2023 KMath contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package space.kscience.attributes - -/** - * A builder for [Attributes]. - * The builder is not thread safe - * - * @param O type marker of an owner object, for which these attributes are made - */ -public class AttributesBuilder internal constructor() : Attributes { - - private val map = mutableMapOf, Any?>() - - override fun toString(): String = "Attributes(value=${map.entries})" - override fun equals(other: Any?): Boolean = other is Attributes && Attributes.equals(this, other) - override fun hashCode(): Int = map.hashCode() - - override val content: Map, Any?> get() = map - - public operator fun set(attribute: Attribute, value: T?) { - if (value == null) { - map.remove(attribute) - } else { - map[attribute] = value - } - } - - public operator fun Attribute.invoke(value: V?) { - set(this, value) - } - - public infix fun Attribute.put(value: V?) { - set(this, value) - } - - /** - * Put all attributes for given [attributes] - */ - public fun putAll(attributes: Attributes) { - map.putAll(attributes.content) - } - - public infix fun SetAttribute.add(attrValue: V) { - val currentSet: Set = get(this) ?: emptySet() - map[this] = currentSet + attrValue - } - - /** - * Remove an element from [SetAttribute] - */ - public infix fun SetAttribute.remove(attrValue: V) { - val currentSet: Set = get(this) ?: emptySet() - map[this] = currentSet - attrValue - } - - public fun build(): Attributes = MapAttributes(map) -} - -/** - * Create [Attributes] with a given [builder] - * @param O the type for which attributes are built. The type is used only during compilation phase for static extension dispatch - */ -public fun Attributes(builder: AttributesBuilder.() -> Unit): Attributes = - AttributesBuilder().apply(builder).build() \ No newline at end of file diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/annotations.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/annotations.kt deleted file mode 100644 index 80d93daaf..000000000 --- a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/annotations.kt +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright 2018-2023 KMath contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package space.kscience.attributes - -/** - * Marks declarations that are still experimental in the Attributes-kt APIs, which means that the design of the corresponding - * declarations has open issues that may (or may not) lead to their changes in the future. Roughly speaking, there is - * a chance of those declarations will be deprecated in the future or the semantics of their behavior may change - * in some way that may break some code. - */ -@MustBeDocumented -@Retention(value = AnnotationRetention.BINARY) -@RequiresOptIn("This API is unstable and could change in future", RequiresOptIn.Level.WARNING) -public annotation class UnstableAttributesAPI \ No newline at end of file diff --git a/kmath-core/build.gradle.kts b/kmath-core/build.gradle.kts index 699dd82e2..019e16aa4 100644 --- a/kmath-core/build.gradle.kts +++ b/kmath-core/build.gradle.kts @@ -9,7 +9,7 @@ kscience { wasm() dependencies { - api(projects.attributesKt) + api(libs.attributes) } testDependencies {