Skip to content
Snippets Groups Projects
Commit 9d688388 authored by Alexander Rose's avatar Alexander Rose
Browse files

added OrderedSet.toString

parent 6c68cebc
Branches
Tags
No related merge requests found
/** /**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
*/ */
...@@ -17,6 +17,7 @@ export const min = Tuple.fst; ...@@ -17,6 +17,7 @@ export const min = Tuple.fst;
export function max(i: Tuple) { return Tuple.snd(i) - 1; } export function max(i: Tuple) { return Tuple.snd(i) - 1; }
export function size(i: Tuple) { return Tuple.snd(i) - Tuple.fst(i); } export function size(i: Tuple) { return Tuple.snd(i) - Tuple.fst(i); }
export const hashCode = Tuple.hashCode; export const hashCode = Tuple.hashCode;
export const toString = Tuple.toString;
export function has(int: Tuple, v: number) { return Tuple.fst(int) <= v && v < Tuple.snd(int); } export function has(int: Tuple, v: number) { return Tuple.fst(int) <= v && v < Tuple.snd(int); }
/** Returns the index of `x` in `set` or -1 if not found. */ /** Returns the index of `x` in `set` or -1 if not found. */
......
/** /**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
*/ */
...@@ -36,6 +36,8 @@ export function end(set: OrderedSetImpl) { return I.is(set) ? I.end(set) : S.end ...@@ -36,6 +36,8 @@ export function end(set: OrderedSetImpl) { return I.is(set) ? I.end(set) : S.end
export function hashCode(set: OrderedSetImpl) { return I.is(set) ? I.hashCode(set) : S.hashCode(set); } export function hashCode(set: OrderedSetImpl) { return I.is(set) ? I.hashCode(set) : S.hashCode(set); }
// TODO: possibly add more hash functions to allow for multilevel hashing. // TODO: possibly add more hash functions to allow for multilevel hashing.
export function toString(set: OrderedSetImpl) { return I.is(set) ? I.toString(set) : S.toString(set); }
export function areEqual(a: OrderedSetImpl, b: OrderedSetImpl) { export function areEqual(a: OrderedSetImpl, b: OrderedSetImpl) {
if (I.is(a)) { if (I.is(a)) {
if (I.is(b)) return I.areEqual(a, b); if (I.is(b)) return I.areEqual(a, b);
......
/** /**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
*/ */
...@@ -35,6 +35,11 @@ export function hashCode(xs: Nums) { ...@@ -35,6 +35,11 @@ export function hashCode(xs: Nums) {
if (s > 2) return hash4(s, xs[0], xs[s - 1], xs[s >> 1]); if (s > 2) return hash4(s, xs[0], xs[s - 1], xs[s >> 1]);
return hash3(s, xs[0], xs[s - 1]); return hash3(s, xs[0], xs[s - 1]);
} }
export function toString(xs: Nums) {
const s = xs.length;
if (s > 5) return `[${xs[0]}, ${xs[1]}, ..., ${xs[s - 1]}], length ${s}`;
return `[${(xs as number[]).join(', ')}]`;
}
/** Returns the index of `x` in `set` or -1 if not found. */ /** Returns the index of `x` in `set` or -1 if not found. */
export function indexOf(xs: Nums, v: number) { export function indexOf(xs: Nums, v: number) {
......
...@@ -34,6 +34,8 @@ namespace Interval { ...@@ -34,6 +34,8 @@ namespace Interval {
export const size: <T extends number = number>(interval: Interval<T>) => number = Impl.size as any; export const size: <T extends number = number>(interval: Interval<T>) => number = Impl.size as any;
/** Hash code describing the interval */ /** Hash code describing the interval */
export const hashCode: <T extends number = number>(interval: Interval<T>) => number = Impl.hashCode as any; export const hashCode: <T extends number = number>(interval: Interval<T>) => number = Impl.hashCode as any;
/** String representation of the interval */
export const toString: <T extends number = number>(interval: Interval<T>) => string = Impl.toString as any;
/** Test if two intervals are identical */ /** Test if two intervals are identical */
export const areEqual: <T extends number = number>(a: Interval<T>, b: Interval<T>) => boolean = Impl.areEqual as any; export const areEqual: <T extends number = number>(a: Interval<T>, b: Interval<T>) => boolean = Impl.areEqual as any;
......
/** /**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
*/ */
...@@ -8,7 +8,6 @@ import * as Base from './impl/ordered-set' ...@@ -8,7 +8,6 @@ import * as Base from './impl/ordered-set'
import Interval from './interval' import Interval from './interval'
import SortedArray from './sorted-array'; import SortedArray from './sorted-array';
/** test */
namespace OrderedSet { namespace OrderedSet {
export const Empty: OrderedSet = Base.Empty as any; export const Empty: OrderedSet = Base.Empty as any;
export const ofSingleton: <T extends number = number>(value: T) => OrderedSet<T> = Base.ofSingleton as any; export const ofSingleton: <T extends number = number>(value: T) => OrderedSet<T> = Base.ofSingleton as any;
...@@ -62,10 +61,12 @@ namespace OrderedSet { ...@@ -62,10 +61,12 @@ namespace OrderedSet {
OrderedSet.forEach(set, v => array.push(v)) OrderedSet.forEach(set, v => array.push(v))
return array return array
} }
export function toString<T extends number = number>(set: OrderedSet<T>): string {
return Base.toString(set)
}
} }
/** Represents bla */
type OrderedSet<T extends number = number> = SortedArray<T> | Interval<T> type OrderedSet<T extends number = number> = SortedArray<T> | Interval<T>
export default OrderedSet export default OrderedSet
\ No newline at end of file
/** /**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
*/ */
...@@ -31,6 +31,7 @@ namespace SortedArray { ...@@ -31,6 +31,7 @@ namespace SortedArray {
export const max: <T extends number = number>(array: SortedArray<T>) => T = Impl.max as any; export const max: <T extends number = number>(array: SortedArray<T>) => T = Impl.max as any;
export const size: <T extends number = number>(array: SortedArray<T>) => number = Impl.size as any; export const size: <T extends number = number>(array: SortedArray<T>) => number = Impl.size as any;
export const hashCode: <T extends number = number>(array: SortedArray<T>) => number = Impl.hashCode as any; export const hashCode: <T extends number = number>(array: SortedArray<T>) => number = Impl.hashCode as any;
export const toString: <T extends number = number>(array: SortedArray<T>) => string = Impl.toString as any;
export const areEqual: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => boolean = Impl.areEqual as any; export const areEqual: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => boolean = Impl.areEqual as any;
export const areIntersecting: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => boolean = Impl.areIntersecting as any; export const areIntersecting: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => boolean = Impl.areIntersecting as any;
......
/** /**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
*/ */
...@@ -73,6 +73,11 @@ namespace IntTuple { ...@@ -73,6 +73,11 @@ namespace IntTuple {
_float64[0] = t as any; _float64[0] = t as any;
return hash2(_int32[0], _int32[1]); return hash2(_int32[0], _int32[1]);
} }
export function toString(t: IntTuple) {
_float64[0] = t as any;
return `(${_int32[0]}, ${_int32[1]})`;
}
} }
export default IntTuple export default IntTuple
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment