Skip to content

Instantly share code, notes, and snippets.

@machuz
Last active December 21, 2019 09:15
Show Gist options
  • Save machuz/655686ddf69be0467e233a4af7020db3 to your computer and use it in GitHub Desktop.
Save machuz/655686ddf69be0467e233a4af7020db3 to your computer and use it in GitHub Desktop.
// 1992年: https://ir.nctu.edu.tw/bitstream/11536/3533/1/A1992HE70800003.pdf
// 1997年: http://msn.iecs.fcu.edu.tw/~ccc/profile/publish/ij_paper2/IJ-135.pdf
// 2000年: https://ir.nctu.edu.tw/bitstream/11536/30118/1/000166289900002.pdf
import scala.math.BigInt
sealed class PermissionCompound(protected val value: BigInt) { self =>
def +(that: PermissionCompound): PermissionCompound = PermissionCompound(
self.value * that.value
)
def -(that: PermissionCompound): Option[PermissionCompound] =
if (self.contains(that))
Some(
PermissionCompound(
self.value / that.value
)
)
else
None
def contains(that: PermissionCompound): Boolean =
self.value % that.value == 0
def isDuplicated(that: PermissionCompound): Boolean =
factorize(self.value).count(_ == that.value) >= 2
private def factorize(x: BigInt): List[BigInt] = {
@annotation.tailrec
def loop(x: BigInt, a: BigInt = 2, l: List[BigInt] = Nil): List[BigInt] = a * a > x match {
case false if x % a == 0 => loop(x / a, a, a :: l)
case false => loop(x, a + 1, l)
case true => x :: l
}
loop(x)
}
override def toString: String = s"PermissionCompound($value)"
}
object PermissionCompound {
import PermissionAtom.PermissionAtomNone
def zero: PermissionCompound = PermissionAtomNone
def apply(value: BigInt): PermissionCompound = new PermissionCompound(value)
}
sealed abstract class PermissionAtom(override protected val value: BigInt) extends PermissionCompound(value)
object PermissionAtom {
case object PermissionAtomNone extends PermissionAtom(0)
case object PermissionAtomEgs extends PermissionAtom(2)
case object PermissionAtomEcpSetic extends PermissionAtom(3)
case object PermissionAtomEcpToeic extends PermissionAtom(5)
case object PermissionAtomEcpToeicPersonalCoach extends PermissionAtom(7)
case object PermissionAtom4Skills extends PermissionAtom(11)
case object PermissionAtomBiz extends PermissionAtom(13)
def valueOf(value: BigInt): PermissionCompound = {
value match {
case n if n == 2 => PermissionAtomEgs
case n if n == 3 => PermissionAtomEcpSetic
case n if n == 5 => PermissionAtomEcpToeic
case n if n == 7 => PermissionAtomEcpToeicPersonalCoach
case n if n == 11 => PermissionAtom4Skills
case n if n == 13 => PermissionAtomBiz
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment