| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 24 |
|
| 25 |
// -------------------------------------------------------------------------- |
| 26 |
// --- Comparison Utilities |
| 27 |
// -------------------------------------------------------------------------- |
| 28 |
|
| 29 |
/** |
| 30 |
Data comparisons. |
| 31 |
@packageDocumentation |
| 32 |
@module dome/data/compare |
| 33 |
*/ |
| 34 |
|
| 35 |
import FastCompare from 'react-fast-compare'; |
| 36 |
|
| 37 |
/** |
| 38 |
Interface for comparison functions. |
| 39 |
These function shall fullfill the following contract: |
| 40 |
- `compare(x,y) === 0` shall be an equivalence relation |
| 41 |
(reflexive, symmetric, transitive) |
| 42 |
- `compare(x,y) <= 0` shall be a complete order |
| 43 |
(reflexive, antisymetric, transitive) |
| 44 |
- `compare(x,y) < 0` shall be a complete strict order |
| 45 |
(anti-reflexive, asymetric, transitive) |
| 46 |
*/ |
| 47 |
export interface Order<A> { |
| 48 |
(x: A, y: A): number; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
Deep structural equality. |
| 53 |
Provided by [react-fast-compare](). |
| 54 |
*/ |
| 55 |
export const isEqual = FastCompare; |
| 56 |
|
| 57 |
/** Always returns 0. */ |
| 58 |
export function equal(_x: any, _y: any): 0 { return 0; } |
| 59 |
|
| 60 |
/** Primitive comparison works on this type. */ |
| 61 |
export type BigNum = bigint | number; |
| 62 |
|
| 63 |
/** Detect Non-NaN numbers and big-ints. */ |
| 64 |
export function isBigNum(x: any): x is BigNum { |
| 65 |
return ( |
| 66 |
(typeof (x) === 'bigint') || |
| 67 |
(typeof (x) === 'number' && !Number.isNaN(x)) |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** @internal */ |
| 72 |
function primitive(x: any, y: any): number { |
| 73 |
if (x < y) return -1; |
| 74 |
if (x > y) return 1; |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
Primitive comparison for symbols. |
| 80 |
*/ |
| 81 |
export const symbol: Order<symbol> = primitive; |
| 82 |
|
| 83 |
/** |
| 84 |
Primitive comparison for booleans. |
| 85 |
*/ |
| 86 |
export const boolean: Order<boolean> = primitive; |
| 87 |
|
| 88 |
/** |
| 89 |
Primitive comparison for strings. See also [[alpha]]. |
| 90 |
*/ |
| 91 |
export const string: Order<string> = primitive; |
| 92 |
|
| 93 |
/** |
| 94 |
Primitive comparison for (big) integers (non NaN numbers included). |
| 95 |
*/ |
| 96 |
export const bignum: Order<BigNum> = primitive; |
| 97 |
|
| 98 |
/** |
| 99 |
Primitive comparison for number (NaN included). |
| 100 |
*/ |
| 101 |
export function number(x: number, y: number): number { |
| 102 |
const nx = Number.isNaN(x); |
| 103 |
const ny = Number.isNaN(y); |
| 104 |
if (nx && ny) return 0; |
| 105 |
if (nx && !ny) return -1; |
| 106 |
if (!nx && ny) return 1; |
| 107 |
if (x < y) return -1; |
| 108 |
if (x > y) return 1; |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
Alphabetic comparison for strings. |
| 114 |
Handles case differently than `byString` comparison. |
| 115 |
*/ |
| 116 |
export function alpha(x: string, y: string): number { |
| 117 |
const cmp = primitive(x.toLowerCase(), y.toLowerCase()); |
| 118 |
return cmp !== 0 ? cmp : primitive(x, y); |
| 119 |
} |
| 120 |
|
| 121 |
/** Combine comparison orders in sequence. */ |
| 122 |
export function sequence<A>(...orders: (Order<A> | undefined)[]): Order<A> { |
| 123 |
return (x: A, y: A) => { |
| 124 |
if (x === y) return 0; |
| 125 |
for (let k = 0; k < orders.length; k++) { |
| 126 |
const order = orders[k]; |
| 127 |
if (order) { |
| 128 |
const cmp = order(x, y); |
| 129 |
if (cmp !== 0) return cmp; |
| 130 |
} |
| 131 |
} |
| 132 |
return 0; |
| 133 |
}; |
| 134 |
} |
| 135 |
|
| 136 |
/** Compare optional values. Undefined values come first. */ |
| 137 |
export function option<A>(order: Order<A>): Order<undefined | A> { |
| 138 |
return (x?: A, y?: A) => { |
| 139 |
if (x === undefined && y === undefined) return 0; |
| 140 |
if (x === undefined) return -1; |
| 141 |
if (y === undefined) return 1; |
| 142 |
return order(x, y); |
| 143 |
}; |
| 144 |
} |
| 145 |
|
| 146 |
/** Compare optional values. Undefined values come last. */ |
| 147 |
export function defined<A>(order: Order<A>): Order<undefined | A> { |
| 148 |
return (x?: A, y?: A) => { |
| 149 |
if (x === undefined && y === undefined) return 0; |
| 150 |
if (x === undefined) return 1; |
| 151 |
if (y === undefined) return -1; |
| 152 |
return order(x, y); |
| 153 |
}; |
| 154 |
} |
| 155 |
|
| 156 |
/** Lexicographic comparison of array elements. */ |
| 157 |
export function array<A>(order: Order<A>): Order<A[]> { |
| 158 |
return (x: A[], y: A[]) => { |
| 159 |
if (x === y) return 0; |
| 160 |
const p = x.length; |
| 161 |
const q = y.length; |
| 162 |
const m = p < q ? p : q; |
| 163 |
for (let k = 0; k < m; k++) { |
| 164 |
const cmp = order(x[k], y[k]); |
| 165 |
if (cmp !== 0) return cmp; |
| 166 |
} |
| 167 |
return p - q; |
| 168 |
}; |
| 169 |
} |
| 170 |
|
| 171 |
/** Order by dictionary order. |
| 172 |
Can be used directly with an enum type declaration. |
| 173 |
*/ |
| 174 |
export function byEnum<A extends string>( |
| 175 |
d: { [key: string]: A }, |
| 176 |
): Order<A> { |
| 177 |
const ranks: { [index: string]: number } = {}; |
| 178 |
const values = Object.keys(d); |
| 179 |
const wildcard = values.length; |
| 180 |
values.forEach((C, k) => { ranks[C] = k; }); |
| 181 |
return (x: A, y: A) => { |
| 182 |
if (x === y) return 0; |
| 183 |
const rx = ranks[x] ?? wildcard; |
| 184 |
const ry = ranks[y] ?? wildcard; |
| 185 |
return rx - ry; |
| 186 |
}; |
| 187 |
} |
| 188 |
|
| 189 |
/** Order string enumeration constants. |
| 190 |
`byRank(v1,...,vN)` will order constant following the |
| 191 |
order of arguments. |
| 192 |
Non-listed constants appear at the end, or at the rank |
| 193 |
specified by `'*'`. */ |
| 194 |
export function byRank(...args: string[]): Order<string> { |
| 195 |
const ranks: { [index: string]: number } = {}; |
| 196 |
args.forEach((C, k) => { ranks[C] = k; }); |
| 197 |
const wildcard = ranks['*'] ?? ranks.length; |
| 198 |
return (x: string, y: string) => { |
| 199 |
if (x === y) return 0; |
| 200 |
const rx = ranks[x] ?? wildcard; |
| 201 |
const ry = ranks[y] ?? wildcard; |
| 202 |
if (rx === wildcard && ry === wildcard) |
| 203 |
return primitive(x, y); |
| 204 |
return rx - ry; |
| 205 |
}; |
| 206 |
} |
| 207 |
|
| 208 |
/** Direct or reverse direction. */ |
| 209 |
export function direction<A>(order: Order<A>, reverse = false): Order<A> { |
| 210 |
return (x, y) => (x === y ? 0 : reverse ? order(y, x) : order(x, y)); |
| 211 |
} |
| 212 |
|
| 213 |
/** By projection. */ |
| 214 |
export function lift<A, B>(fn: (x: A) => B, order: Order<B>): Order<A> { |
| 215 |
return (x: A, y: A) => (x === y ? 0 : order(fn(x), fn(y))); |
| 216 |
} |
| 217 |
|
| 218 |
/** Return own property names of its object argument. */ |
| 219 |
export function getKeys<T>(a: T): (keyof T)[] { |
| 220 |
return Object.getOwnPropertyNames(a) as (keyof T)[]; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
Maps each field of `A` to some _optional_ comparison of the associated type. |
| 225 |
Hence, `ByFields<{…, f: T, …}>` is `{…, f?: Order<T>, …}`. |
| 226 |
See [[byFields]] comparison function. |
| 227 |
*/ |
| 228 |
export type ByFields<A> = { |
| 229 |
[P in keyof A]?: Order<A[P]>; |
| 230 |
}; |
| 231 |
|
| 232 |
/** |
| 233 |
Maps each field of `A` to some comparison of the associated type. |
| 234 |
Hence, `ByAllFields<{…, f: T, …}>` is `{…, f: Order<T>, …}`. |
| 235 |
See [[byAllFields]] comparison function. |
| 236 |
*/ |
| 237 |
export type ByAllFields<A> = { |
| 238 |
[P in keyof A]: Order<A[P]>; |
| 239 |
}; |
| 240 |
|
| 241 |
/** |
| 242 |
Object comparison by (some) fields. |
| 243 |
|
| 244 |
Compare objects field by field, using the comparison orders provided by the |
| 245 |
`order` argument. Order of field comparison is taken from the `order` |
| 246 |
argument, not from the compared values. |
| 247 |
|
| 248 |
You may not compare _all_ fields of the compared values. For optional |
| 249 |
fields, you shall provide a comparison function compatible with type |
| 250 |
`undefined`. |
| 251 |
|
| 252 |
It might be difficult for Typescript to typecheck `byFields(…)` expressions |
| 253 |
when dealing with optional types. In such cases, you shall use |
| 254 |
`byFields<A>(…)` and explicitly mention the type of compared values. |
| 255 |
|
| 256 |
Example: |
| 257 |
|
| 258 |
* type foo = { id: number, name?: string, descr?: string } |
| 259 |
* const compare = fields<foo>({ id: number, name: option(alpha) }); |
| 260 |
|
| 261 |
*/ |
| 262 |
export function byFields<A>(order: ByFields<A>): Order<A> { |
| 263 |
return (x: A, y: A) => { |
| 264 |
if (x === y) return 0; |
| 265 |
const fds = getKeys(order); |
| 266 |
for (let k = 0; k < fds.length; k++) { |
| 267 |
const fd = fds[k]; |
| 268 |
const byFd = order[fd]; |
| 269 |
if (byFd !== undefined) { |
| 270 |
const cmp = byFd(x[fd], y[fd]); |
| 271 |
if (cmp !== 0) return cmp; |
| 272 |
} |
| 273 |
} |
| 274 |
return 0; |
| 275 |
}; |
| 276 |
} |
| 277 |
|
| 278 |
/** Complete object comparison. |
| 279 |
This is similar to `byFields()` comparison, but an ordering function must be |
| 280 |
provided for _any_ field (optional or not) of the compared values. |
| 281 |
*/ |
| 282 |
export function byAllFields<A>(order: ByAllFields<A>): Order<A> { |
| 283 |
return (x: A, y: A) => { |
| 284 |
if (x === y) return 0; |
| 285 |
const fds = getKeys<ByFields<A>>(order); |
| 286 |
for (let k = 0; k < fds.length; k++) { |
| 287 |
const fd = fds[k]; |
| 288 |
const byFd = order[fd]; |
| 289 |
const cmp = byFd(x[fd], y[fd]); |
| 290 |
if (cmp !== 0) return cmp; |
| 291 |
} |
| 292 |
return 0; |
| 293 |
}; |
| 294 |
} |
| 295 |
|
| 296 |
export type Dict<A> = undefined | null | { [key: string]: A }; |
| 297 |
|
| 298 |
/** |
| 299 |
Compare dictionaries _wrt_ lexicographic order of entries. |
| 300 |
*/ |
| 301 |
export function dictionary<A>(order: Order<A>): Order<Dict<A>> { |
| 302 |
return (x: Dict<A>, y: Dict<A>) => { |
| 303 |
if (x === y) return 0; |
| 304 |
const dx = x ?? {}; |
| 305 |
const dy = y ?? {}; |
| 306 |
const phi = option(order); |
| 307 |
const fs = Object.getOwnPropertyNames(dx).sort(); |
| 308 |
const gs = Object.getOwnPropertyNames(dy).sort(); |
| 309 |
const p = fs.length; |
| 310 |
const q = gs.length; |
| 311 |
for (let i = 0, j = 0; i < p && j < q;) { |
| 312 |
let a; |
| 313 |
let b; |
| 314 |
const f = fs[i]; |
| 315 |
const g = gs[j]; |
| 316 |
if (f <= g) { a = dx[f]; i++; } |
| 317 |
if (g <= f) { b = dy[g]; j++; } |
| 318 |
const cmp = phi(a, b); |
| 319 |
if (cmp !== 0) return cmp; |
| 320 |
} |
| 321 |
return p - q; |
| 322 |
}; |
| 323 |
} |
| 324 |
|
| 325 |
/** Pair comparison. */ |
| 326 |
export function pair<A, B>(ordA: Order<A>, ordB: Order<B>): Order<[A, B]> { |
| 327 |
return (u, v) => { |
| 328 |
if (u === v) return 0; |
| 329 |
const [x1, y1] = u; |
| 330 |
const [x2, y2] = v; |
| 331 |
const cmp = ordA(x1, x2); |
| 332 |
return cmp !== 0 ? cmp : ordB(y1, y2); |
| 333 |
}; |
| 334 |
} |
| 335 |
|
| 336 |
/** Triple comparison. */ |
| 337 |
export function triple<A, B, C>( |
| 338 |
ordA: Order<A>, |
| 339 |
ordB: Order<B>, |
| 340 |
ordC: Order<C>, |
| 341 |
): Order<[A, B, C]> { |
| 342 |
return (u, v) => { |
| 343 |
if (u === v) return 0; |
| 344 |
const [x1, y1, z1] = u; |
| 345 |
const [x2, y2, z2] = v; |
| 346 |
const cmp1 = ordA(x1, x2); |
| 347 |
if (cmp1 !== 0) return cmp1; |
| 348 |
const cmp2 = ordB(y1, y2); |
| 349 |
if (cmp2 !== 0) return cmp2; |
| 350 |
return ordC(z1, z2); |
| 351 |
}; |
| 352 |
} |
| 353 |
|
| 354 |
/** 4-Tuple comparison. */ |
| 355 |
export function tuple4<A, B, C, D>( |
| 356 |
ordA: Order<A>, |
| 357 |
ordB: Order<B>, |
| 358 |
ordC: Order<C>, |
| 359 |
ordD: Order<D>, |
| 360 |
): Order<[A, B, C, D]> { |
| 361 |
return (u, v) => { |
| 362 |
if (u === v) return 0; |
| 363 |
const [x1, y1, z1, t1] = u; |
| 364 |
const [x2, y2, z2, t2] = v; |
| 365 |
const cmp1 = ordA(x1, x2); |
| 366 |
if (cmp1 !== 0) return cmp1; |
| 367 |
const cmp2 = ordB(y1, y2); |
| 368 |
if (cmp2 !== 0) return cmp2; |
| 369 |
const cmp3 = ordC(z1, z2); |
| 370 |
if (cmp3 !== 0) return cmp3; |
| 371 |
return ordD(t1, t2); |
| 372 |
}; |
| 373 |
} |
| 374 |
|
| 375 |
/** 5-Tuple comparison. */ |
| 376 |
export function tuple5<A, B, C, D, E>( |
| 377 |
ordA: Order<A>, |
| 378 |
ordB: Order<B>, |
| 379 |
ordC: Order<C>, |
| 380 |
ordD: Order<D>, |
| 381 |
ordE: Order<E>, |
| 382 |
): Order<[A, B, C, D, E]> { |
| 383 |
return (u, v) => { |
| 384 |
if (u === v) return 0; |
| 385 |
const [x1, y1, z1, t1, w1] = u; |
| 386 |
const [x2, y2, z2, t2, w2] = v; |
| 387 |
const cmp1 = ordA(x1, x2); |
| 388 |
if (cmp1 !== 0) return cmp1; |
| 389 |
const cmp2 = ordB(y1, y2); |
| 390 |
if (cmp2 !== 0) return cmp2; |
| 391 |
const cmp3 = ordC(z1, z2); |
| 392 |
if (cmp3 !== 0) return cmp3; |
| 393 |
const cmp4 = ordD(t1, t2); |
| 394 |
if (cmp4 !== 0) return cmp4; |
| 395 |
return ordE(w1, w2); |
| 396 |
}; |
| 397 |
} |
| 398 |
|
| 399 |
// -------------------------------------------------------------------------- |
| 400 |
// --- Structural Comparison |
| 401 |
// -------------------------------------------------------------------------- |
| 402 |
|
| 403 |
/* eslint-disable no-shadow */ |
| 404 |
|
| 405 |
/** @internal */ |
| 406 |
enum IRANK { |
| 407 |
UNDEFINED, |
| 408 |
BOOLEAN, SYMBOL, NAN, BIGNUM, |
| 409 |
STRING, |
| 410 |
ARRAY, OBJECT, FUNCTION, |
| 411 |
} |
| 412 |
|
| 413 |
/** @internal */ |
| 414 |
function rank(x: any): IRANK { |
| 415 |
const t = typeof x; |
| 416 |
switch (t) { |
| 417 |
case 'undefined': return IRANK.UNDEFINED; |
| 418 |
case 'boolean': return IRANK.BOOLEAN; |
| 419 |
case 'symbol': return IRANK.SYMBOL; |
| 420 |
case 'number': |
| 421 |
return Number.isNaN(x) ? IRANK.NAN : IRANK.BIGNUM; |
| 422 |
case 'bigint': |
| 423 |
return IRANK.BIGNUM; |
| 424 |
case 'string': return IRANK.STRING; |
| 425 |
case 'function': return IRANK.FUNCTION; |
| 426 |
case 'object': |
| 427 |
return Array.isArray(x) ? IRANK.ARRAY : IRANK.OBJECT; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
Universal structural comparison. |
| 433 |
Values are ordered by _rank_, each being |
| 434 |
associated with some type of values: |
| 435 |
1. undefined values; |
| 436 |
2. booleans; |
| 437 |
3. symbols; |
| 438 |
4. NaN numbers; |
| 439 |
5. non-NaN numbers and bigints; |
| 440 |
6. arrays; |
| 441 |
7. objects; |
| 442 |
8. functions; |
| 443 |
|
| 444 |
For values of same primitive type, primitive ordering is performed. |
| 445 |
|
| 446 |
For array values, lexicographic ordering is performed. |
| 447 |
For object values, lexicographic ordering is performed over their |
| 448 |
properties: they are ordered by name, and recursive structural |
| 449 |
ordering is performed on property values. |
| 450 |
|
| 451 |
All functions are compared equal. |
| 452 |
*/ |
| 453 |
export function structural(x: any, y: any): number { |
| 454 |
if (x === y) return 0; |
| 455 |
if (typeof x === 'symbol' && typeof y === 'symbol') return primitive(x, y); |
| 456 |
if (typeof x === 'boolean' && typeof y === 'boolean') return primitive(x, y); |
| 457 |
if (typeof x === 'string' && typeof y === 'string') return primitive(x, y); |
| 458 |
if (isBigNum(x) && isBigNum(y)) return primitive(x, y); |
| 459 |
if (Array.isArray(x) && Array.isArray(y)) return array(structural)(x, y); |
| 460 |
if (typeof x === 'object' && typeof y === 'object') { |
| 461 |
const fs = Object.getOwnPropertyNames(x).sort(); |
| 462 |
const gs = Object.getOwnPropertyNames(y).sort(); |
| 463 |
const p = fs.length; |
| 464 |
const q = gs.length; |
| 465 |
for (let i = 0, j = 0; i < p && j < q;) { |
| 466 |
let a; |
| 467 |
let b; |
| 468 |
const f = fs[i]; |
| 469 |
const g = gs[j]; |
| 470 |
if (f <= g) { a = x[f]; i++; } |
| 471 |
if (g <= f) { b = y[g]; j++; } |
| 472 |
const cmp = structural(a, b); |
| 473 |
if (cmp !== 0) return cmp; |
| 474 |
} |
| 475 |
return p - q; |
| 476 |
} |
| 477 |
return rank(x) - rank(y); |
| 478 |
} |
| 479 |
|
| 480 |
// -------------------------------------------------------------------------- |