Resolving issues #16 and #20

This commit is contained in:
Song Minjae
2017-04-24 02:23:13 +09:00
parent 6399c2d66b
commit 5cd5ebbea3
21 changed files with 158 additions and 76 deletions

View File

@@ -7,6 +7,9 @@ import java.util.HashSet
/**
* Created by minjaesong on 16-02-15.
*/
typealias FactionID = Int
class Faction(name: String) : Comparable<Faction> {
var factionName: String = name
@@ -14,7 +17,7 @@ class Faction(name: String) : Comparable<Faction> {
lateinit var factionNeutral: HashSet<String>
lateinit var factionHostile: HashSet<String>
lateinit var factionFearful: HashSet<String>
var referenceID: Long = generateUniqueID()
var referenceID: FactionID = generateUniqueID()
init {
factionAmicable = HashSet<String>()
@@ -59,10 +62,11 @@ class Faction(name: String) : Comparable<Faction> {
factionFearful.remove(faction)
}
private fun generateUniqueID(): Long {
var ret: Long
/** Valid range: -2147483648..-1 (all the negative number) */
private fun generateUniqueID(): Int {
var ret: Int
do {
ret = HQRNG().nextLong().or(0x80000000L).and(0xFFFFFFFFL) // guaranteed to be 2147483648..4294967295
ret = HQRNG().nextInt(2147483647).plus(1).unaryMinus()
} while (FactionCodex.hasFaction(ret)) // check for collision
return ret
}

View File

@@ -9,7 +9,7 @@ import java.util.*
object FactionCodex {
val factionContainer = ArrayList<Faction>()
fun hasFaction(ID: Long): Boolean =
fun hasFaction(ID: FactionID): Boolean =
if (factionContainer.size == 0)
false
else
@@ -22,7 +22,7 @@ object FactionCodex {
insertionSortLastElem(factionContainer) // we can do this as we are only adding single actor
}
fun getFactionByID(ID: Long): Faction {
fun getFactionByID(ID: FactionID): Faction {
if (factionContainer.size == 0) throw IllegalArgumentException("Faction with ID $ID does not exist.")
val index = factionContainer.binarySearch(ID)
@@ -45,7 +45,7 @@ object FactionCodex {
arr[j + 1] = x
}
private fun ArrayList<Faction>.binarySearch(ID: Long): Int {
private fun ArrayList<Faction>.binarySearch(ID: FactionID): Int {
// code from collections/Collections.kt
var low = 0
var high = factionContainer.size - 1