Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 1x 1x 1x 1x 1x 1x 1x 1x 11x 2x 9x 1x 1x 1x 1x 1x 9x 1x 8x 1x 1x 1x 1x 1x 21x 1x 23x 21x 21x 21x 21x 21x 23x 23x 1x 13x 12x 4x 2x 2x 8x 1x 1x 14x 3x 11x 1x 9x 8x 3x 5x 1x 1x 9x 3x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 19x | import {type EndoOf} from '#Function'
import {type Branch} from '#tree'
import {Array} from '#util'
import {Number, Predicate, Tuple, pipe} from 'effect'
import {decode} from './decoder.js'
import {encode} from './encoder.js'
import {
codeCount,
computeNodeCount,
fromOrdinal,
toOrdinal,
} from './enumerate.js'
/**
* What is the first prüfer code for the given node count?
* @category codec
* @function
*/
export const getFirstCodeFor = (nodeCount: number): number[] =>
nodeCount < 3
? []
: pipe(nodeCount, codeCount, count => Array.replicate(1, count))
/**
* What is the first prüfer code for the node count of the given code?
* @category codec
* @function
*/
export const getFirstCode: EndoOf<number[]> = code =>
pipe(code, computeNodeCount, getFirstCodeFor)
/**
* What is the first prüfer code for the _next_ node count of the given code?
* @category codec
* @function
*/
export const getNextFirstCode: EndoOf<number[]> = code =>
pipe(code, computeNodeCount, Number.increment, getFirstCodeFor)
/**
* What is the last prüfer code for the given node count?
* @category codec
* @function
*/
export const getLastCodeFor: (n: number) => number[] = nodeCount =>
nodeCount < 3
? []
: pipe(nodeCount, codeCount, count => Array.replicate(nodeCount, count))
/**
* What is the last prüfer code for the node count of the given code?
* @category codec
* @function
*/
export const getLastCode: EndoOf<number[]> = code =>
pipe(code, computeNodeCount, getLastCodeFor)
/**
* What is the last prüfer code for the _previous_ node count of the given code?
* @category codec
* @function
*/
export const getPreviousLastCode: EndoOf<number[]> = code =>
pipe(code, computeNodeCount, Number.decrement, getLastCodeFor)
/**
* Is this the first prüfer code for its node count?
* @category codec
* @function
*/
export const isFirstCode: Predicate.Predicate<number[]> = code =>
Array.isNonEmptyArray(code) ? Array.every(code, n => n === 1) : true
/**
* Is this the final prüfer code for its node count?
* @category codec
* @function
*/
export const isLastCode: Predicate.Predicate<number[]> = code => {
if (Array.isNonEmptyArray(code)) {
const nodeCount = computeNodeCount(code)
return pipe(
code,
Array.every(n => n === nodeCount),
)
} else return true
}
/**
* Get the _previous_ prüfer code for the given code. If the given code is the
* first code for its node count, the last code of the _previous_ node count is
* returned. When node count reaches 2, I.e.: code count is 0, we stop and
* return the input unchanged.
* @category codec
* @function
*/
export const previousCode: EndoOf<number[]> = code =>
Array.isNonEmptyArray(code)
? isFirstCode(code)
? code.length === 1
? []
: getLastCodeFor(computeNodeCount(code) - 1)
: pipe(code, withOrdinal(Tuple.mapFirst(n => n - 1n)))
: []
/**
* Get the _next_ prüfer code for the given code. If the given code is the final
* code for its node count, the first code of the _next_ node count is returned.
* @category codec
* @function
*/
export const nextCode: EndoOf<number[]> = code =>
isLastCode(code)
? getFirstCodeFor(computeNodeCount(code) + 1)
: pipe(code, withOrdinal(Tuple.mapFirst(n => n + 1n)))
/**
* Compute the previous Prüfer code from the given code with wrap-around.
*
* For example, the previous code of `1,1,1` (first code for 5 nodes) will be
* `4,4` (last code for 4 nodes), and the previous code of `4,4` will be `4,3`.
* @category codec
* @function
*/
export const previousCodeWrap: EndoOf<number[]> = code =>
Array.isNonEmptyArray(code)
? isFirstCode(code)
? pipe(code, computeNodeCount, getLastCodeFor)
: previousCode(code)
: []
/**
* Compute the next Prüfer code from the given code with wrap-around.
*
* For example, the next code of `1,4` will be `2,1` and the next code of `4,4`
* will be `1,1`.
* @category codec
* @function
*/
export const nextCodeWrap: EndoOf<number[]> = code =>
isLastCode(code)
? pipe(code, computeNodeCount, getFirstCodeFor)
: nextCode(code)
/**
* Get the _previous_ numeric tree in the ordered set of numeric trees with
* the same node count as the given tree. If the tree is the 1st in its
* ordered set, we decrement node count and return the last tree in this
* ordered set. The process stops when node count reaches 3, after which
* the given tree is returned unchanged.
* @category codec
* @function
*/
export const previousTree: EndoOf<Branch<number>> = self =>
pipe(self, encode(Number.Order), previousCode, decode)
/**
* Get the _next_ numeric tree in the ordered set of numeric trees with
* the same node count as the given tree. If the tree is the last in its
* ordered set, we increment node count and return the first tree in this
* ordered set.
* @category codec
* @function
*/
export const nextTree: EndoOf<Branch<number>> = self =>
pipe(self, encode(Number.Order), nextCode, decode)
/**
* Just like `previousTree` but when first tree is reached we wrap
* around to the last tree with the same node count.
* @category codec
* @function
*/
export const previousTreeWrap: EndoOf<Branch<number>> = self =>
pipe(self, encode(Number.Order), previousCodeWrap, decode)
/**
* Just like `nextTree` but when last tree is reached we wrap
* around to the first tree with the same node count.
* @category codec
* @function
*/
export const nextTreeWrap: EndoOf<Branch<number>> = self =>
pipe(self, encode(Number.Order), nextCodeWrap, decode)
/**
* Run a function over the `[ordinal, nodeCount]` pair of a code then convert it
* back to the same encoding of ordinal/count pair.
* @category codec
* @function
*/
const withOrdinal =
(f: (pair: [ordinal: bigint, nodeCount: number]) => typeof pair) =>
(code: number[]): number[] => {
const [n, nodeCount] = pipe(code, toOrdinal, f)
return fromOrdinal(n, nodeCount)
}
|