-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
212 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
json-sick-scala/src/main/scala/izumi/sick/tables/DeduplicatingRefTableBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package izumi.sick.tables | ||
|
||
import izumi.sick.model.Ref.RefVal | ||
|
||
import scala.collection.mutable | ||
|
||
class DeduplicatingRefTableBuilder[V]( | ||
val name: String, | ||
reverse: mutable.HashMap[V, RefVal], | ||
) extends GenericRefTableBuilder[V] { | ||
|
||
private var count = 0 | ||
|
||
def insert(v: V): RefVal = { | ||
reverse.get(v) match { | ||
case Some(value) => | ||
value | ||
case None => | ||
val k = count | ||
reverse.put(v, k) | ||
count += 1 | ||
k | ||
} | ||
} | ||
|
||
def enumerate(): Map[RefVal, V] = { | ||
reverse.map(_.swap).toMap | ||
} | ||
|
||
def isEmpty: Boolean = reverse.isEmpty | ||
|
||
def size: Int = reverse.size | ||
|
||
def freeze(): RefTableRO[V] = new RefTableRO[V](name, reverse.map(_.swap).toMap) | ||
|
||
def rewrite(mapping: V => V): DeduplicatingRefTableBuilder[V] = { | ||
new DeduplicatingRefTableBuilder[V]( | ||
name, | ||
reverse.view.map { case (k, v) => mapping(k) -> v }.to(mutable.HashMap), | ||
) | ||
} | ||
|
||
override def toString: String = { | ||
s"""$name: | ||
|${reverse.map { case (v, k) => s"$k --> $v" }.mkString("\n")}""".stripMargin | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
json-sick-scala/src/main/scala/izumi/sick/tables/GenericRefTableBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package izumi.sick.tables | ||
|
||
import izumi.sick.model.Ref.RefVal | ||
|
||
import scala.collection.mutable | ||
|
||
trait GenericRefTableBuilder[V] { | ||
def name: String | ||
def insert(v: V): RefVal | ||
def enumerate(): Map[RefVal, V] | ||
def isEmpty: Boolean | ||
def size: Int | ||
def freeze(): RefTableRO[V] | ||
def rewrite(mapping: V => V): GenericRefTableBuilder[V] | ||
} | ||
|
||
object GenericRefTableBuilder { | ||
def apply[V](name: String, dedup: Boolean): GenericRefTableBuilder[V] = { | ||
if (dedup) { | ||
val reverse = mutable.HashMap.empty[V, RefVal] | ||
new DeduplicatingRefTableBuilder[V](name, reverse) | ||
} else { | ||
val content = mutable.HashMap.empty[RefVal, V] | ||
new QuickRefTableBuilder[V](name, content) | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
json-sick-scala/src/main/scala/izumi/sick/tables/QuickRefTableBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package izumi.sick.tables | ||
|
||
import izumi.sick.model.Ref.RefVal | ||
|
||
import scala.collection.mutable | ||
|
||
class QuickRefTableBuilder[V]( | ||
val name: String, | ||
content: mutable.HashMap[RefVal, V], | ||
) extends GenericRefTableBuilder[V] { | ||
|
||
private var count = 0 | ||
|
||
def insert(v: V): RefVal = { | ||
val k = count | ||
content.put(k, v) | ||
count += 1 | ||
k | ||
} | ||
|
||
def enumerate(): Map[RefVal, V] = { | ||
content.toMap | ||
} | ||
|
||
def isEmpty: Boolean = content.isEmpty | ||
|
||
def size: Int = content.size | ||
|
||
def freeze(): RefTableRO[V] = new RefTableRO[V](name, content.toMap) | ||
|
||
def rewrite(mapping: V => V): QuickRefTableBuilder[V] = { | ||
new QuickRefTableBuilder[V]( | ||
name, | ||
content.view.map { case (k, v) => k -> mapping(v) }.to(mutable.HashMap), | ||
) | ||
} | ||
|
||
override def toString: String = { | ||
s"""$name: | ||
|${content.map { case (k, v) => s"$k --> $v" }.mkString("\n")}""".stripMargin | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 0 additions & 110 deletions
110
json-sick-scala/src/main/scala/izumi/sick/tables/RefTableRW.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.