Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sorted map always using insertion order when created as mutable #70

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions packages/map/src/internals/primitives/unwrap.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Associative, isObject, unwrap, unwrapKey } from '@collectable/core';
import { Associative, unwrap, unwrapKey } from '@collectable/core';
import { HashMapStructure } from '../HashMap';
import { identity, iterator } from './index';
import { Leaf } from '../nodes';

export function unwrapInto<K, V> (target: Associative<V>, map: HashMapStructure<K, V>): Associative<V> {
var it = iterator(map._root, identity);
var it = iterator(map._root, identity);
var current: IteratorResult<Leaf<K, V>>;
while(!(current = it.next()).done) {
var entry = current.value;
var value = entry.value;
target[unwrapKey(entry.key)] = isObject(value) ? unwrap<V>(value) : value;
target[unwrapKey(entry.key)] = unwrap<V>(value);
}
return target;
}
3 changes: 2 additions & 1 deletion packages/red-black-tree/src/functions/fromKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export function fromKeys<K> (compare: ComparatorFn<K>, keys: K[]|RedBlackTreeStr
}
}
else {
const it = isRedBlackTree<K>(keys) ? iterateKeysFromFirst(keys) : keys[Symbol.iterator]();
const iterable = isRedBlackTree<K>(keys) ? iterateKeysFromFirst(keys) : keys;
const it = iterable[Symbol.iterator]();
var current: IteratorResult<K>;
while(!(current = it.next()).done) {
set(current.value, null, tree);
Expand Down
1 change: 1 addition & 0 deletions packages/sorted-map/src/functions/empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function empty<K, V, U> (
mutable = false;
}
else {
compare = <ComparatorFn<SortedMapEntry<K, V, U>>>arg1;
mutable = isMutationContext(arg0) || isBoolean(arg0) ? arg0 : false;
}
return emptySortedMap<K, V, U>(compare, select, mutable);
Expand Down
33 changes: 32 additions & 1 deletion packages/sorted-map/tests/functions/empty.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import test from 'ava';
import { empty, size } from '../../src';
import { ComparatorFn, numericCompare } from '@frptools/corelib';
import { empty, set, size, SortedMapEntry, firstValue, lastValue } from '../../src';

interface Item {
id: number;
time: number;
}

test('returns a map with size 0', t => {
t.is(size(empty()), 0);
Expand All @@ -9,3 +15,28 @@ test('always returns the same set instance if called with no arguments', t => {
const a = empty(), b = empty();
t.is(a, b);
});

test('sorts added items', t => {
const compare: ComparatorFn<SortedMapEntry<number, Item, number>> = (x, y) => numericCompare(x.view, y.view);
const map = empty<number, Item, number>(true, compare, x => x.time);

const now = new Date();
for (let i = 0; i < 15; i++) {
const item = {
id: (i + 1),
time: new Date(now.getFullYear(), now.getMonth(), now.getDate() - (i + 1)).valueOf()
};

set(item.id, item, map);
}

t.deepEqual(firstValue(map), {
id: 15,
time: new Date(now.getFullYear(), now.getMonth(), now.getDate() - 15).valueOf()
});

t.deepEqual(lastValue(map), {
id: 1,
time: new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1).valueOf()
});
});