Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/1566.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- **BREAKING CHANGES:** `types/byte_set`: `toRangeMap` takes only the set and
returns `RangeMap<boolean>` instead of taking a state name and returning
`RangeMap<SortedSet<string>>`. Callers label the ranges themselves
15 changes: 14 additions & 1 deletion fjs/fsm/module.f.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,22 @@ const mergeOp = { union: sortedSetUnion(cmp), equal: equal(strictEqual), def: []
/** @type {(s: string) => (set: SortedSet<string>) => boolean} */
const hasState = s => set => !isEmpty(intersect(cmp)([s])(set))

/**
* Labels a byte set's ranges with the rule they lead to: a range inside the set
* transitions to `ruleOut`, one outside transitions nowhere.
*
* `byte_set.toRangeMap` answers only whether each range is in the set; which
* state that means is a DFA question, so it is answered here.
*
* @type {(ruleOut: string) => (entry: Entry<boolean>) => Entry<SortedSet<string>>}
*/
const labelRange = ruleOut => ([inSet, max]) => [inSet ? [ruleOut] : [], max]

/** @type {(set: SortedSet<string>) => Fold<_Rule, RangeMap<SortedSet<string>>>} */
const foldOp = set => ([ruleIn, bs, ruleOut]) => rm => {
if (hasState(ruleIn)(set)) { return merge(mergeOp)(rm)(toRangeMap(bs)(ruleOut)) }
if (hasState(ruleIn)(set)) {
return merge(mergeOp)(rm)(map(labelRange(ruleOut))(toRangeMap(bs)))
}
return rm
}

Expand Down
20 changes: 14 additions & 6 deletions fjs/types/byte_set/module.f.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @module
*
* @import { RangeMap } from '../range_map/types.ts'
* @import { SortedSet } from '../sorted_set/types.ts'
* @import { ByteSet } from './types.ts'
*/

Expand Down Expand Up @@ -57,12 +56,21 @@ export const unset = n => s => difference(s)(one(n))

const counter = reverse(countdown(256))

/** @type {(n: ByteSet) => (s: string) => (i: number) => RangeMap<SortedSet<string>>} */
const toRangeMapOp = n => s => i => {
/** @type {(n: ByteSet) => (i: number) => RangeMap<boolean>} */
const toRangeMapOp = n => i => {
const current = has(i + 1)(n)
const prev = has(i)(n)
return current === prev ? null : [[prev ? [s] : [], i]]
return current === prev ? null : [[prev, i]]
}

/** @type {(n: ByteSet) => (s: string) => RangeMap<SortedSet<string>>} */
export const toRangeMap = n => s => flat(map(toRangeMapOp(n)(s))(counter))
/**
* Re-expresses the set as a range map: one entry per membership boundary,
* carrying whether the range up to that byte is in the set.
*
* The payload is the set's own answer — `boolean` — and nothing more. A caller
* that needs ranges labelled with something else maps over the result; that
* labelling belongs to the caller, not to a byte set.
*
* @type {(n: ByteSet) => RangeMap<boolean>}
*/
export const toRangeMap = n => flat(map(toRangeMapOp(n))(counter))
14 changes: 7 additions & 7 deletions fjs/types/byte_set/proof.f.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@ export const proof = {
},
toRangeMap: [
() => {
const result = stringify(toArray(toRangeMap(empty)('a')))
const result = stringify(toArray(toRangeMap(empty)))
assertEq(result, '[]')
},
() => {
const s = set(0)(empty)
const result = stringify(toArray(toRangeMap(s)('a')))
assertEq(result, '[[["a"],0]]')
const result = stringify(toArray(toRangeMap(s)))
assertEq(result, '[[true,0]]')
},
() => {
const s = setRange([1, 2])(empty)
const result = stringify(toArray(toRangeMap(s)('a')))
assertEq(result, '[[[],0],[["a"],2]]')
const result = stringify(toArray(toRangeMap(s)))
assertEq(result, '[[false,0],[true,2]]')
},
() => {
const result = stringify(toArray(toRangeMap(universe)('a')))
assertEq(result, '[[["a"],255]]')
const result = stringify(toArray(toRangeMap(universe)))
assertEq(result, '[[true,255]]')
},
]
}
44 changes: 0 additions & 44 deletions fjs/types/byte_set/todo/torangemap-payload.md

This file was deleted.

Loading