Skip to content

Commit

Permalink
implement equals, hashCode, and toString for clause collections
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed May 28, 2020
1 parent da09156 commit 360139b
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 6 deletions.
40 changes: 40 additions & 0 deletions core/src/commonMain/kotlin/it/unibo/tuprolog/utils/IterUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,46 @@ fun <T> Sequence<T>.subsequences(): Sequence<Sequence<T>> {
}
}

fun <T> itemWiseEquals(iterable1: Iterable<T>, iterable2: Iterable<T>, comparator: (T, T) -> Boolean): Boolean {
val i = iterable1.iterator()
val j = iterable2.iterator()
while (i.hasNext() && j.hasNext()) {
if (!comparator(i.next(), j.next())) {
return false
}
}
return i.hasNext() == j.hasNext()
}

fun <T> itemWiseEquals(iterable1: Iterable<T>, iterable2: Iterable<T>): Boolean =
itemWiseEquals(iterable1, iterable2) { a, b -> a == b }

fun <T> itemWiseEquals(sequence1: Sequence<T>, sequence2: Sequence<T>, comparator: (T, T) -> Boolean): Boolean {
return itemWiseEquals(sequence1.asIterable(), sequence2.asIterable(), comparator)
}

fun <T> itemWiseEquals(sequence1: Sequence<T>, sequence2: Sequence<T>): Boolean {
return itemWiseEquals(sequence1.asIterable(), sequence2.asIterable())
}

fun <T> itemWiseHashCode(vararg items: T): Int {
return itemWiseHashCode(items.asIterable())
}

fun <T> itemWiseHashCode(iterable: Iterable<T>): Int {
var hash = 13
val i = iterable.iterator()
while (i.hasNext()) {
hash = 31 * hash + (i.next()?.hashCode() ?: 0)
}
return hash
}

fun <T> itemWiseHashCode(sequence: Sequence<T>): Int {
return itemWiseHashCode(sequence.asIterable())
}


fun <T> Iterable<T>.subsequences(): Sequence<Sequence<T>> {
return asSequence().subsequences()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ protected constructor(protected val rete: ReteTree) : ClauseCollection {
override fun iterator(): Iterator<Clause> =
rete.clauses.iterator()

override fun toString(): String {
return "${this::class.simpleName}(${this.joinToString(", ")})"
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package it.unibo.tuprolog.collections
import it.unibo.tuprolog.collections.impl.ReteClauseMultiSet
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.core.TermComparator
import it.unibo.tuprolog.utils.itemWiseEquals
import it.unibo.tuprolog.utils.itemWiseHashCode
import kotlin.js.JsName
import kotlin.jvm.JvmStatic

Expand Down Expand Up @@ -59,6 +62,24 @@ interface ClauseMultiSet : ClauseCollection {
@JsName("ofIterable")
fun of(clauses: Iterable<Clause>): ClauseMultiSet =
ReteClauseMultiSet(clauses)

@JvmStatic
@JsName("equals")
fun equals(multiSet1: ClauseMultiSet, multiSet2: ClauseMultiSet): Boolean {
return itemWiseEquals(
multiSet1.sortedWith(TermComparator.DefaultComparator),
multiSet2.sortedWith(TermComparator.DefaultComparator)
)
}

@JvmStatic
@JsName("hashCode")
fun hashCode(multiSet: ClauseMultiSet): Int {
return itemWiseHashCode(
ClauseMultiSet::class,
multiSet.sortedWith(TermComparator.DefaultComparator)
)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package it.unibo.tuprolog.collections
import it.unibo.tuprolog.collections.impl.ReteClauseQueue
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.utils.itemWiseEquals
import it.unibo.tuprolog.utils.itemWiseHashCode
import kotlin.js.JsName
import kotlin.jvm.JvmStatic

Expand Down Expand Up @@ -81,6 +83,21 @@ interface ClauseQueue : ClauseCollection {
@JsName("ofIterable")
fun of(clauses: Iterable<Clause>): ClauseQueue =
ReteClauseQueue(clauses)

@JvmStatic
@JsName("equals")
fun equals(queue1: ClauseQueue, queue2: ClauseQueue): Boolean {
return itemWiseEquals(queue1, queue2)
}

@JvmStatic
@JsName("hashCode")
fun hashCode(queue: ClauseQueue): Int {
return itemWiseHashCode(
ClauseQueue::class,
itemWiseHashCode(queue)
)
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package it.unibo.tuprolog.collections
import it.unibo.tuprolog.collections.impl.MutableReteClauseMultiSet
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.core.TermComparator
import it.unibo.tuprolog.utils.itemWiseEquals
import it.unibo.tuprolog.utils.itemWiseHashCode
import kotlin.js.JsName
import kotlin.jvm.JvmStatic

Expand Down Expand Up @@ -50,6 +53,21 @@ interface MutableClauseMultiSet : ClauseMultiSet {
@JsName("ofIterable")
fun of(clauses: Iterable<Clause>): MutableClauseMultiSet =
MutableReteClauseMultiSet(clauses)

@JvmStatic
@JsName("equals")
fun equals(multiSet1: MutableClauseMultiSet, multiSet2: MutableClauseMultiSet): Boolean {
return ClauseMultiSet.equals(multiSet1, multiSet2)
}

@JvmStatic
@JsName("hashCode")
fun hashCode(multiSet: MutableClauseMultiSet): Int {
return itemWiseHashCode(
MutableClauseMultiSet::class,
multiSet.sortedWith(TermComparator.DefaultComparator)
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package it.unibo.tuprolog.collections
import it.unibo.tuprolog.collections.impl.MutableReteClauseQueue
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.utils.itemWiseEquals
import it.unibo.tuprolog.utils.itemWiseHashCode
import kotlin.js.JsName
import kotlin.jvm.JvmStatic

Expand Down Expand Up @@ -59,6 +61,21 @@ interface MutableClauseQueue : ClauseQueue {
@JsName("ofIterable")
fun of(clauses: Iterable<Clause>): MutableClauseQueue =
MutableReteClauseQueue(clauses)

@JvmStatic
@JsName("equals")
fun equals(queue1: MutableClauseQueue, queue2: MutableClauseQueue): Boolean {
return ClauseQueue.equals(queue1, queue2)
}

@JvmStatic
@JsName("hashCode")
fun hashCode(queue: MutableClauseQueue): Int {
return itemWiseHashCode(
MutableClauseQueue::class,
itemWiseHashCode(queue)
)
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.unibo.tuprolog.collections.impl

import it.unibo.tuprolog.collections.*
import it.unibo.tuprolog.collections.AbstractMutableReteClauseCollection
import it.unibo.tuprolog.collections.MutableClauseMultiSet
import it.unibo.tuprolog.collections.rete.custom.ReteTree
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.theory.TheoryUtils
Expand All @@ -26,4 +26,15 @@ internal class MutableReteClauseMultiSet private constructor(
override fun get(clause: Clause): Sequence<Clause> =
rete.get(clause)

override fun equals(other: Any?): Boolean {
return if (other is MutableClauseMultiSet) {
MutableClauseMultiSet.equals(this, other)
} else {
false
}
}

override fun hashCode(): Int {
return MutableClauseMultiSet.hashCode(this)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.unibo.tuprolog.collections.impl

import it.unibo.tuprolog.collections.AbstractMutableReteClauseCollection
import it.unibo.tuprolog.collections.ClauseQueue
import it.unibo.tuprolog.collections.MutableClauseQueue
import it.unibo.tuprolog.collections.RetrieveResult
import it.unibo.tuprolog.collections.rete.custom.ReteTree
Expand Down Expand Up @@ -48,4 +49,16 @@ internal class MutableReteClauseQueue private constructor(
)
}
}

override fun equals(other: Any?): Boolean {
return if (other is MutableClauseQueue) {
MutableClauseQueue.equals(this, other)
} else {
false
}
}

override fun hashCode(): Int {
return MutableClauseQueue.hashCode(this)
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package it.unibo.tuprolog.collections.impl

import it.unibo.tuprolog.collections.*
import it.unibo.tuprolog.collections.AbstractReteClauseCollection
import it.unibo.tuprolog.collections.ClauseMultiSet
import it.unibo.tuprolog.collections.rete.custom.ReteTree
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.theory.TheoryUtils
import it.unibo.tuprolog.utils.itemWiseHashCode

internal class ReteClauseMultiSet private constructor(
rete: ReteTree
) : ClauseMultiSet, AbstractReteClauseCollection<ReteClauseMultiSet>(rete) {

private val hashCodeCache by lazy {
ClauseMultiSet.hashCode(this)
}

init {
require(!rete.isOrdered)
}
Expand All @@ -29,4 +34,14 @@ internal class ReteClauseMultiSet private constructor(
return ReteClauseMultiSet(rete)
}

override fun equals(other: Any?): Boolean {
return if (other is ClauseMultiSet && other !is MutableClauseMultiSet) {
ClauseMultiSet.equals(this, other)
} else {
false
}
}

override fun hashCode(): Int = hashCodeCache

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ package it.unibo.tuprolog.collections.impl

import it.unibo.tuprolog.collections.AbstractReteClauseCollection
import it.unibo.tuprolog.collections.ClauseQueue
import it.unibo.tuprolog.collections.MutableClauseQueue
import it.unibo.tuprolog.collections.RetrieveResult
import it.unibo.tuprolog.collections.rete.custom.ReteTree
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.theory.TheoryUtils
import it.unibo.tuprolog.utils.itemWiseHashCode

internal class ReteClauseQueue private constructor(
rete: ReteTree
) : ClauseQueue, AbstractReteClauseCollection<ReteClauseQueue>(rete) {

private val hashCodeCache by lazy {
ClauseQueue.hashCode(this)
}

init {
require(rete.isOrdered)
}
Expand All @@ -27,10 +33,6 @@ internal class ReteClauseQueue private constructor(
}
)

override fun retrieveAll(clause: Clause): RetrieveResult<out ReteClauseQueue> {
return super.retrieveAll(clause)
}

override fun addLast(clause: Clause): ReteClauseQueue =
ReteClauseQueue(
rete.deepCopy().apply {
Expand All @@ -56,4 +58,16 @@ internal class ReteClauseQueue private constructor(
override fun newCollectionBuilder(rete: ReteTree): ReteClauseQueue =
ReteClauseQueue(rete)

override fun equals(other: Any?): Boolean {
return if (other is ClauseQueue && other !is MutableClauseQueue) {
ClauseQueue.equals(this, other)
} else {
false
}
}

override fun hashCode(): Int {
return hashCodeCache
}

}

0 comments on commit 360139b

Please sign in to comment.