Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coll.distinct method removed #1052

Open
wants to merge 4 commits into
base: v6.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/shared/src/main/scala/sigma/data/CGroupElement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ case class CGroupElement(override val wrappedValue: Ecp) extends GroupElement wi

override def toString: String = s"GroupElement(${wrappedValue.showECPoint})"

override def getEncoded: Coll[Byte] =
override def getEncoded: Coll[Byte] = {
Colls.fromArray(GroupElementSerializer.toBytes(wrappedValue))
}

override def isIdentity: Boolean = CryptoFacade.isInfinityPoint(wrappedValue)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ object ReflectionData {
mkMethod(clazz, "reverse", Array[Class[_]]()) { (obj, args) =>
obj.asInstanceOf[Coll[Any]].reverse
},
mkMethod(clazz, "distinct", Array[Class[_]]()) { (obj, args) =>
obj.asInstanceOf[Coll[Any]].distinct
},
mkMethod(clazz, "startsWith", Array[Class[_]](classOf[Coll[_]])) { (obj, args) =>
obj.asInstanceOf[Coll[Any]].startsWith(args(0).asInstanceOf[Coll[Any]])
},
Expand Down
4 changes: 0 additions & 4 deletions data/shared/src/main/scala/sigma/SigmaDataReflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,6 @@ object SigmaDataReflection {
obj.asInstanceOf[SCollectionMethods.type].reverse_eval(args(0).asInstanceOf[MethodCall],
args(1).asInstanceOf[Coll[Any]])(args(2).asInstanceOf[ErgoTreeEvaluator])
},
mkMethod(clazz, "distinct_eval", Array[Class[_]](classOf[MethodCall], classOf[Coll[_]], classOf[ErgoTreeEvaluator])) { (obj, args) =>
obj.asInstanceOf[SCollectionMethods.type].distinct_eval(args(0).asInstanceOf[MethodCall],
args(1).asInstanceOf[Coll[Any]])(args(2).asInstanceOf[ErgoTreeEvaluator])
},
mkMethod(clazz, "startsWith_eval", Array[Class[_]](classOf[MethodCall], classOf[Coll[_]], classOf[Coll[_]], classOf[ErgoTreeEvaluator])) { (obj, args) =>
obj.asInstanceOf[SCollectionMethods.type].startsWith_eval(args(0).asInstanceOf[MethodCall],
args(1).asInstanceOf[Coll[Any]], args(2).asInstanceOf[Coll[Any]])(args(3).asInstanceOf[ErgoTreeEvaluator])
Expand Down
30 changes: 5 additions & 25 deletions data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ object SCollectionMethods extends MethodsContainer with MethodByNameUnapply {
val ReverseMethod = SMethod(this, "reverse",
SFunc(Array(ThisType), ThisType, paramIVSeq), 30, reverseCostKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "")
.withInfo(MethodCall, "Returns inversed collection.")

/** Implements evaluation of Coll.reverse method call ErgoTree node.
* Called via reflection based on naming convention.
Expand All @@ -1210,29 +1210,10 @@ object SCollectionMethods extends MethodsContainer with MethodByNameUnapply {
}
}

private val distinctCostKind = PerItemCost(baseCost = JitCost(60), perChunkCost = JitCost(5), chunkSize = 100)

val DistinctMethod = SMethod(this, "distinct",
SFunc(Array(ThisType), ThisType, paramIVSeq), 31, distinctCostKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "Returns inversed collection.")

/** Implements evaluation of Coll.reverse method call ErgoTree node.
* Called via reflection based on naming convention.
* @see SMethod.evalMethod
*/
def distinct_eval[A](mc: MethodCall, xs: Coll[A])
(implicit E: ErgoTreeEvaluator): Coll[A] = {
val m = mc.method
E.addSeqCost(m.costKind.asInstanceOf[PerItemCost], xs.length, m.opDesc) { () =>
xs.distinct
}
}

private val startsWithCostKind = Zip_CostKind

val StartsWithMethod = SMethod(this, "startsWith",
SFunc(Array(ThisType, ThisType), SBoolean, paramIVSeq), 32, startsWithCostKind)
SFunc(Array(ThisType, ThisType), SBoolean, paramIVSeq), 31, startsWithCostKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "Returns true if this collection starts with given one, false otherwise.",
ArgInfo("prefix", "Collection to be checked for being a prefix of this collection."))
Expand All @@ -1252,7 +1233,7 @@ object SCollectionMethods extends MethodsContainer with MethodByNameUnapply {
private val endsWithCostKind = Zip_CostKind

val EndsWithMethod = SMethod(this, "endsWith",
SFunc(Array(ThisType, ThisType), SBoolean, paramIVSeq), 33, endsWithCostKind)
SFunc(Array(ThisType, ThisType), SBoolean, paramIVSeq), 32, endsWithCostKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "Returns true if this collection ends with given one, false otherwise.",
ArgInfo("suffix", "Collection to be checked for being a suffix of this collection."))
Expand All @@ -1270,7 +1251,7 @@ object SCollectionMethods extends MethodsContainer with MethodByNameUnapply {
}

val GetMethod = SMethod(this, "get",
SFunc(Array(ThisType, SInt), SOption(tIV), Array[STypeParam](tIV)), 34, ByIndex.costKind)
SFunc(Array(ThisType, SInt), SOption(tIV), Array[STypeParam](tIV)), 33, ByIndex.costKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall,
"Returns Some(element) if there is an element at given index, None otherwise.",
Expand Down Expand Up @@ -1299,7 +1280,6 @@ object SCollectionMethods extends MethodsContainer with MethodByNameUnapply {

private val v6Methods = v5Methods ++ Seq(
ReverseMethod,
DistinctMethod,
StartsWithMethod,
EndsWithMethod,
GetMethod
Expand Down Expand Up @@ -1963,7 +1943,7 @@ case object SGlobalMethods extends MonoTypeMethods {
CBigInt(Autolykos2PowValidation.hitForVersion2ForMessageWithChecks(k, msg.toArray, nonce.toArray, h.toArray, N).bigInteger)
}

private val deserializeCostKind = PerItemCost(baseCost = JitCost(30), perChunkCost = JitCost(20), chunkSize = 32)
private val deserializeCostKind = PerItemCost(baseCost = JitCost(100), perChunkCost = JitCost(32), chunkSize = 32)

lazy val deserializeToMethod = SMethod(
this, "deserializeTo", SFunc(Array(SGlobal, SByteArray), tT, Array(paramT)), 4, deserializeCostKind, Seq(tT))
Expand Down
8 changes: 1 addition & 7 deletions docs/LangSpec.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,7 @@ class Context {
* @return A new $coll with all elements of this $coll in reversed order.
*/
def reverse: Coll[T]

/** Builds a new $coll from this $coll without any duplicate elements.
*
* @return A new $coll which contains the first occurrence of every element of this $coll.
*/
def distinct: Coll[T]


}

/** Represents data of the block headers available in scripts. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,6 @@ trait GraphBuilding extends Base with DefRewriting { IR: IRContext =>
xs.getOrElse(i, d)
case SCollectionMethods.ReverseMethod.name =>
xs.reverse
case SCollectionMethods.DistinctMethod.name =>
xs.distinct
case SCollectionMethods.StartsWithMethod.name =>
val ys = asRep[Coll[t]](argsV(0))
xs.startsWith(ys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,6 @@ object GraphIRReflection {
mkMethod(clazz, "reverse", Array[Class[_]]()) { (obj, _) =>
obj.asInstanceOf[ctx.Coll[Any]].reverse
},
mkMethod(clazz, "distinct", Array[Class[_]]()) { (obj, _) =>
obj.asInstanceOf[ctx.Coll[Any]].distinct
},
mkMethod(clazz, "startsWith", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.Coll[Any]].startsWith(args(0).asInstanceOf[ctx.Ref[ctx.Coll[Any]]])
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import sigma.compiler.ir.{Base, IRContext}
def slice(from: Ref[Int], until: Ref[Int]): Ref[Coll[A]];
def append(other: Ref[Coll[A]]): Ref[Coll[A]];
def reverse: Ref[Coll[A]]
def distinct: Ref[Coll[A]]
def startsWith(ys: Ref[Coll[A]]): Ref[Boolean];
def endsWith(ys: Ref[Coll[A]]): Ref[Boolean];
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,6 @@ class CollCls extends EntityObject("Coll") {
true, false, element[Coll[A]]))
}

override def distinct: Ref[Coll[A]] = {
asRep[Coll[A]](mkMethodCall(self,
CollClass.getMethod("distinct"),
Array[AnyRef](),
true, false, element[Coll[A]]))
}

def startsWith(ys: Ref[Coll[A]]): Ref[Boolean] = {
asRep[Boolean](mkMethodCall(self,
CollClass.getMethod("startsWith", classOf[Sym]),
Expand Down Expand Up @@ -362,13 +355,6 @@ class CollCls extends EntityObject("Coll") {
true, true, element[Coll[A]]))
}

def distinct: Ref[Coll[A]] = {
asRep[Coll[A]](mkMethodCall(source,
CollClass.getMethod("distinct"),
Array[AnyRef](),
true, true, element[Coll[A]]))
}

def startsWith(ys: Ref[Coll[A]]): Ref[Boolean] = {
asRep[Boolean](mkMethodCall(source,
CollClass.getMethod("startsWith", classOf[Sym]),
Expand Down
29 changes: 0 additions & 29 deletions sc/shared/src/test/scala/sigma/LanguageSpecificationV6.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2186,35 +2186,6 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>
)
}

property("Coll.distinct") {
val f = newFeature[Coll[Int], Coll[Int]](
{ (xs: Coll[Int]) => xs.distinct },
"""{(xs: Coll[Int]) => xs.distinct }""".stripMargin,
FuncValue(
Array((1, SCollectionType(SInt))),
MethodCall.typed[Value[SCollection[SInt.type]]](
ValUse(1, SCollectionType(SInt)),
SCollectionMethods.DistinctMethod.withConcreteTypes(Map(STypeVar("IV") -> SInt)),
IndexedSeq(),
Map()
)
),
sinceVersion = VersionContext.V6SoftForkVersion
)

verifyCases(
Seq(
Coll(1, 2) -> Expected(ExpectedResult(Success(Coll(1, 2)), None)),
Coll(1, 1, 2) -> Expected(ExpectedResult(Success(Coll(1, 2)), None)),
Coll(1, 2, 2) -> Expected(ExpectedResult(Success(Coll(1, 2)), None)),
Coll(2, 2, 2) -> Expected(ExpectedResult(Success(Coll(2)), None)),
Coll(3, 1, 2, 2, 2, 4, 4, 1) -> Expected(ExpectedResult(Success(Coll(3, 1, 2, 4)), None)),
Coll[Int]() -> Expected(ExpectedResult(Success(Coll[Int]()), None))
),
f
)
}

property("Coll.startsWith") {
val f = newFeature[(Coll[Int], Coll[Int]), Boolean](
{ (xs: (Coll[Int], Coll[Int])) => xs._1.startsWith(xs._2) },
Expand Down
25 changes: 2 additions & 23 deletions sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import sigma.eval.EvalSettings
import sigma.exceptions.{CostLimitException, InterpreterException}
import sigma.serialization.ErgoTreeSerializer.DefaultSerializer
import sigmastate.{CrossVersionProps, Plus}
import sigmastate.utils.Helpers.TryOps
import sigmastate.utils.Helpers.{TryOps, decodeGroupElement}


/** Regression tests with ErgoTree related test vectors.
Expand Down Expand Up @@ -577,36 +577,15 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit with C
MInfo(8, FilterMethod),
MInfo(9, AppendMethod),
MInfo(10, ApplyMethod),
/* TODO soft-fork: https://github.com/ScorexFoundation/sigmastate-interpreter/issues/479
BitShiftLeftMethod,
BitShiftRightMethod,
BitShiftRightZeroedMethod,
*/
MInfo(14, IndicesMethod),
MInfo(15, FlatMapMethod),
MInfo(19, PatchMethod),
MInfo(20, UpdatedMethod),
MInfo(21, UpdateManyMethod),
/*TODO soft-fork: https://github.com/ScorexFoundation/sigmastate-interpreter/issues/479
UnionSetsMethod,
DiffMethod,
IntersectMethod,
PrefixLengthMethod,
*/
MInfo(26, IndexOfMethod),
/* TODO soft-fork: https://github.com/ScorexFoundation/sigmastate-interpreter/issues/479
LastIndexOfMethod,
FindMethod,
*/
MInfo(29, ZipMethod)
/* TODO soft-fork: https://github.com/ScorexFoundation/sigmastate-interpreter/issues/479
DistinctMethod,
StartsWithMethod,
EndsWithMethod,
MapReduceMethod,
*/
) ++ (if (isV6Activated) {
Seq(MInfo(30, ReverseMethod), MInfo(31, DistinctMethod), MInfo(32, StartsWithMethod), MInfo(33, EndsWithMethod), MInfo(34, GetMethod))
Seq(MInfo(30, ReverseMethod), MInfo(31, StartsWithMethod), MInfo(32, EndsWithMethod), MInfo(33, GetMethod))
} else Seq.empty), true)
},
{ import SOptionMethods._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1487,27 +1487,6 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("Coll.distinct"){
def reverseTest() = test("distinct", env, ext,
"""{
| val c1 = Coll(1, 2, 3, 3, 2)
| val c2 = Coll(3, 2, 1)
|
| val h1 = Coll(INPUTS(0), INPUTS(0))
| val h2 = Coll(INPUTS(0))
|
| c1.distinct.reverse == c2 && h1.distinct == h2
| }""".stripMargin,
null
)

if(VersionContext.current.isV6SoftForkActivated) {
reverseTest()
} else {
an[sigma.validation.ValidationException] shouldBe thrownBy(reverseTest())
}
}

property("Coll.startsWith"){
def reverseTest() = test("distinct", env, ext,
"""{
Expand Down
Loading