Skip to content

Commit

Permalink
feat: 🎸 add Equatable helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanonymous-GitHub committed Jul 17, 2024
1 parent ae03634 commit c2cf3f8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
23 changes: 23 additions & 0 deletions core/src/main/kotlin/tw/xcc/gumtree/api/Equatable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tw.xcc.gumtree.api

/**
* A base class to facilitate [operator ==] and [hashCode] overrides.
* */
abstract class Equatable {
/**
* The list of properties that will be used to determine whether two instances are equal.
* */
protected abstract val equatableProps: List<Any?>

final override operator fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Equatable) return false
if (equatableProps.size != other.equatableProps.size) return false
for (i in equatableProps.indices) {
if (equatableProps[i] != other.equatableProps[i]) return false
}
return true
}

final override fun hashCode(): Int = equatableProps.hashCode() xor javaClass.hashCode()
}
9 changes: 4 additions & 5 deletions core/src/main/kotlin/tw/xcc/gumtree/model/BasicTree.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package tw.xcc.gumtree.model

import tw.xcc.gumtree.api.Equatable
import tw.xcc.gumtree.api.tree.Tree
import java.util.concurrent.atomic.AtomicReference

/**
* The general thread-safe implementation of a tree structure.
* */
abstract class BasicTree<T> : Tree<T> where T : BasicTree<T> {
abstract class BasicTree<T> : Equatable(), Tree<T> where T : BasicTree<T> {
protected abstract val self: T

private val _parent = AtomicReference<T?>()
Expand Down Expand Up @@ -50,9 +51,7 @@ abstract class BasicTree<T> : Tree<T> where T : BasicTree<T> {

final override fun isLeaf(): Boolean = synchronized(this) { childrenMap.get().isEmpty() }

abstract override fun equals(other: Any?): Boolean
abstract fun similarityHashCode(): Int

abstract override fun hashCode(): Int

abstract fun structureHashCode(): Int
abstract fun similarityStructureHashCode(): Int
}
19 changes: 7 additions & 12 deletions core/src/main/kotlin/tw/xcc/gumtree/model/GumTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class GumTree :
Traversable<GumTree>,
Comparable<GumTree> {
private val traversalHelper = TraversalHelper(this)
private val compareHelper = CompareHelper(this)

override val equatableProps: List<Any?> = listOf()

fun insertChildAt(
pos: Int,
Expand All @@ -33,23 +36,15 @@ class GumTree :

override fun postOrdered(): List<GumTree> = traversalHelper.postOrdered()

override infix fun isIsomorphicTo(other: GumTree): Boolean {
TODO("Not yet implemented")
}

override infix fun isIsoStructuralTo(other: GumTree): Boolean {
TODO("Not yet implemented")
}
override infix fun isIsomorphicTo(other: GumTree): Boolean = compareHelper.isIsomorphicTo(other)

override fun equals(other: Any?): Boolean {
TODO("Not yet implemented")
}
override infix fun isIsoStructuralTo(other: GumTree): Boolean = compareHelper.isIsoStructuralTo(other)

override fun hashCode(): Int {
override fun similarityHashCode(): Int {
TODO("Not yet implemented")
}

override fun structureHashCode(): Int {
override fun similarityStructureHashCode(): Int {
TODO("Not yet implemented")
}

Expand Down

0 comments on commit c2cf3f8

Please sign in to comment.