Skip to content

Commit 3c2eec0

Browse files
author
Dr. Brandon Wiley
committed
Shorter implementations
1 parent 11592e5 commit 3c2eec0

File tree

2 files changed

+38
-148
lines changed

2 files changed

+38
-148
lines changed
Lines changed: 20 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,32 @@
11
package org.operatorfoundation.codex.symbols
22

33
class CallAny : Symbol {
4-
override fun size(): Int {
5-
return 26 + 10 + 1
6-
}
4+
companion object {
5+
private val charToValue = mapOf(
6+
"A" to 0, "B" to 1, "C" to 2, "D" to 3, "E" to 4, "F" to 5,
7+
"G" to 6, "H" to 7, "I" to 8, "J" to 9, "K" to 10, "L" to 11,
8+
"M" to 12, "N" to 13, "O" to 14, "P" to 15, "Q" to 16, "R" to 17,
9+
"S" to 18, "T" to 19, "U" to 20, "V" to 21, "W" to 22, "X" to 23,
10+
"Y" to 24, "Z" to 25, "0" to 26, "1" to 27, "2" to 28, "3" to 29,
11+
"4" to 30, "5" to 31, "6" to 32, "7" to 33, "8" to 34, "9" to 35,
12+
" " to 36
13+
)
714

8-
override fun toString(): String {
9-
return "CallAny"
15+
private val valueToChar = charToValue.map { (k, v) -> v to k }.toMap()
1016
}
1117

18+
override fun size(): Int = 26 + 10 + 1
19+
20+
override fun toString(): String = "CallAny"
21+
1222
override fun decode(encodedValue: ByteArray): Int {
13-
return when (encodedValue.toString()) {
14-
"A" -> 0
15-
"B" -> 1
16-
"C" -> 2
17-
"D" -> 3
18-
"E" -> 4
19-
"F" -> 5
20-
"G" -> 6
21-
"H" -> 7
22-
"I" -> 8
23-
"J" -> 9
24-
"K" -> 10
25-
"L" -> 11
26-
"M" -> 12
27-
"N" -> 13
28-
"O" -> 14
29-
"P" -> 15
30-
"Q" -> 16
31-
"R" -> 17
32-
"S" -> 18
33-
"T" -> 19
34-
"U" -> 20
35-
"V" -> 21
36-
"W" -> 22
37-
"X" -> 23
38-
"Y" -> 24
39-
"Z" -> 25
40-
"0" -> 26
41-
"1" -> 27
42-
"2" -> 28
43-
"3" -> 29
44-
"4" -> 30
45-
"5" -> 31
46-
"6" -> 32
47-
"7" -> 33
48-
"8" -> 34
49-
"9" -> 35
50-
" " -> 36
51-
else -> throw IllegalArgumentException("CallAny, bad value: $encodedValue")
52-
}
23+
return charToValue[encodedValue.toString()]
24+
?: throw IllegalArgumentException("CallAny, bad value: $encodedValue")
5325
}
5426

5527
override fun encode(numericValue: Int): ByteArray {
56-
return when (numericValue) {
57-
0 -> "A".toByteArray()
58-
1 -> "B".toByteArray()
59-
2 -> "C".toByteArray()
60-
3 -> "D".toByteArray()
61-
4 -> "E".toByteArray()
62-
5 -> "F".toByteArray()
63-
6 -> "G".toByteArray()
64-
7 -> "H".toByteArray()
65-
8 -> "I".toByteArray()
66-
9 -> "J".toByteArray()
67-
10 -> "K".toByteArray()
68-
11 -> "L".toByteArray()
69-
12 -> "M".toByteArray()
70-
13 -> "N".toByteArray()
71-
14 -> "O".toByteArray()
72-
15 -> "P".toByteArray()
73-
16 -> "Q".toByteArray()
74-
17 -> "R".toByteArray()
75-
18 -> "S".toByteArray()
76-
19 -> "T".toByteArray()
77-
20 -> "U".toByteArray()
78-
21 -> "V".toByteArray()
79-
22 -> "W".toByteArray()
80-
23 -> "X".toByteArray()
81-
24 -> "Y".toByteArray()
82-
25 -> "Z".toByteArray()
83-
26 -> "0".toByteArray()
84-
27 -> "1".toByteArray()
85-
28 -> "2".toByteArray()
86-
29 -> "3".toByteArray()
87-
30 -> "4".toByteArray()
88-
31 -> "5".toByteArray()
89-
32 -> "6".toByteArray()
90-
33 -> "7".toByteArray()
91-
34 -> "8".toByteArray()
92-
35 -> "9".toByteArray()
93-
36 -> " ".toByteArray()
94-
else -> throw IllegalArgumentException("Unknown value $numericValue for CallAny")
95-
}
28+
return (valueToChar[numericValue]
29+
?: throw IllegalArgumentException("Unknown value $numericValue for CallAny"))
30+
.toByteArray()
9631
}
9732
}
Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,30 @@
11
package org.operatorfoundation.codex.symbols
22

33
class CallLetter : Symbol {
4-
override fun size(): Int {
5-
return 26
6-
}
4+
companion object {
5+
private val charToValue = mapOf(
6+
"A" to 0, "B" to 1, "C" to 2, "D" to 3, "E" to 4, "F" to 5,
7+
"G" to 6, "H" to 7, "I" to 8, "J" to 9, "K" to 10, "L" to 11,
8+
"M" to 12, "N" to 13, "O" to 14, "P" to 15, "Q" to 16, "R" to 17,
9+
"S" to 18, "T" to 19, "U" to 20, "V" to 21, "W" to 22, "X" to 23,
10+
"Y" to 24, "Z" to 25
11+
)
712

8-
override fun toString(): String {
9-
return "CallLetter"
13+
private val valueToChar = charToValue.map { (k, v) -> v to k }.toMap()
1014
}
1115

16+
override fun size(): Int = 26
17+
18+
override fun toString(): String = "CallLetter"
19+
1220
override fun decode(encodedValue: ByteArray): Int {
13-
return when (encodedValue.toString()) {
14-
"A" -> 0
15-
"B" -> 1
16-
"C" -> 2
17-
"D" -> 3
18-
"E" -> 4
19-
"F" -> 5
20-
"G" -> 6
21-
"H" -> 7
22-
"I" -> 8
23-
"J" -> 9
24-
"K" -> 10
25-
"L" -> 11
26-
"M" -> 12
27-
"N" -> 13
28-
"O" -> 14
29-
"P" -> 15
30-
"Q" -> 16
31-
"R" -> 17
32-
"S" -> 18
33-
"T" -> 19
34-
"U" -> 20
35-
"V" -> 21
36-
"W" -> 22
37-
"X" -> 23
38-
"Y" -> 24
39-
"Z" -> 25
40-
else -> throw IllegalArgumentException("CallLetter, bad value: $encodedValue")
41-
}
21+
return charToValue[encodedValue.toString()]
22+
?: throw IllegalArgumentException("CallLetter, bad value: $encodedValue")
4223
}
4324

4425
override fun encode(numericValue: Int): ByteArray {
45-
return when (numericValue) {
46-
0 -> "A".toByteArray()
47-
1 -> "B".toByteArray()
48-
2 -> "C".toByteArray()
49-
3 -> "D".toByteArray()
50-
4 -> "E".toByteArray()
51-
5 -> "F".toByteArray()
52-
6 -> "G".toByteArray()
53-
7 -> "H".toByteArray()
54-
8 -> "I".toByteArray()
55-
9 -> "J".toByteArray()
56-
10 -> "K".toByteArray()
57-
11 -> "L".toByteArray()
58-
12 -> "M".toByteArray()
59-
13 -> "N".toByteArray()
60-
14 -> "O".toByteArray()
61-
15 -> "P".toByteArray()
62-
16 -> "Q".toByteArray()
63-
17 -> "R".toByteArray()
64-
18 -> "S".toByteArray()
65-
19 -> "T".toByteArray()
66-
20 -> "U".toByteArray()
67-
21 -> "V".toByteArray()
68-
22 -> "W".toByteArray()
69-
23 -> "X".toByteArray()
70-
24 -> "Y".toByteArray()
71-
25 -> "Z".toByteArray()
72-
else -> throw IllegalArgumentException("Unknown value $numericValue for CallLetter")
73-
}
26+
return (valueToChar[numericValue]
27+
?: throw IllegalArgumentException("Unknown value $numericValue for CallLetter"))
28+
.toByteArray()
7429
}
7530
}

0 commit comments

Comments
 (0)