-
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
4 changed files
with
54 additions
and
6 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
54 changes: 51 additions & 3 deletions
54
bewl2/src/main/scala/com/fdilke/bewl2/sets/permutations/Permutation.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 |
---|---|---|
@@ -1,8 +1,56 @@ | ||
package com.fdilke.bewl2.sets.permutations | ||
|
||
class Permutation[T]( | ||
map: Map[T, T] | ||
class Permutation[A]( | ||
map: Map[A, A] | ||
): | ||
() | ||
def domain: Set[A] = | ||
map.keySet | ||
def apply(a: A): A = | ||
map.get(a).getOrElse( | ||
throw new IllegalArgumentException(s"key $a not found in map $map") | ||
) | ||
|
||
class Cycle[A]( | ||
cycle: Seq[A] | ||
) { | ||
if (cycle.toSet.size < cycle.length) | ||
throw new IllegalArgumentException("cycle repeats") | ||
|
||
def toMap: Map[A, A] = | ||
val x: Seq[(A, A)] = cycle.indices.map { i => | ||
cycle(i) -> cycle( (i + 1) % cycle.length ) | ||
} | ||
x.toMap[A, A] | ||
} | ||
|
||
class OngoingPermutation[A]( | ||
cycles: Seq[Cycle[A]] | ||
): | ||
def toPermutation: Permutation[A] = | ||
new Permutation( | ||
cycles map { | ||
_.toMap | ||
} reduce { | ||
_ ++ _ | ||
} | ||
) | ||
|
||
def apply(a: A, tail: A*): OngoingPermutation[A] = | ||
new OngoingPermutation( | ||
cycles :+ Cycle(a +: tail) | ||
) | ||
|
||
object Permutation: | ||
def apply[A](a: A, tail: A*): OngoingPermutation[A] = | ||
new OngoingPermutation( | ||
Seq[Cycle[A]]( | ||
Cycle(a +: tail) | ||
) | ||
) | ||
|
||
implicit def convertOngoing[A]( | ||
ongoing: OngoingPermutation[A] | ||
): Permutation[A] = | ||
ongoing.toPermutation | ||
|
||
|
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