Skip to content

Commit

Permalink
prepare for worker
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Nov 17, 2018
1 parent cf573c3 commit 1263505
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
6 changes: 4 additions & 2 deletions src/model/RankColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ export default class RankColumn extends Column {
return -1;
}
const groups = ranking.getGroups();
let offset = 0;
for (const group of groups) {
const rank = group.index2pos[row.i];
if (typeof rank === 'number') {
return rank + 1; // starting with 1
if (typeof rank === 'number' && !isNaN(rank)) {
return rank + 1 + offset; // starting with 1
}
offset += group.order.length;
}
return -1;
}
Expand Down
69 changes: 36 additions & 33 deletions src/provider/LocalDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Column, {
import Ranking from '../model/Ranking';
import ACommonDataProvider from './ACommonDataProvider';
import {IDataProviderOptions, IStatsBuilder} from './interfaces';
import {sortComplex} from './sort';
import {sortComplex, sort2indices} from './sort';
import {range} from 'd3-array';
import ADataProvider from './ADataProvider';

Expand All @@ -28,6 +28,11 @@ export interface ILocalDataProviderOptions {
jumpToSearchResult: boolean;
}

interface ISortHelper {
group: IGroup;
rows: {i: number, sort: ICompareValue[]}[];
}

/**
* a data provider based on an local array
*/
Expand Down Expand Up @@ -130,7 +135,7 @@ export default class LocalDataProvider extends ACommonDataProvider {
super.cleanUpRanking(ranking);
}

sortImpl(ranking: Ranking): IOrderedGroup[] {
sortImpl(ranking: Ranking): IOrderedGroup[] | Promise<IOrderedGroup[]> {
if (this._data.length === 0) {
return [];
}
Expand Down Expand Up @@ -163,7 +168,7 @@ export default class LocalDataProvider extends ACommonDataProvider {
return [Object.assign({order, index2pos}, defaultGroup)];
}

const groups = new Map<string, {group: IGroup, rows: {r: IDataRow, sort: ICompareValue[]}[]}>();
const groups = new Map<string, ISortHelper>();

for (const r of this._dataRows) {
if (filter && !filter(r)) {
Expand All @@ -173,9 +178,9 @@ export default class LocalDataProvider extends ACommonDataProvider {
const groupKey = group.name.toLowerCase();
const sort = ranking.toCompareValue(r);
if (groups.has(groupKey)) {
groups.get(groupKey)!.rows.push({r, sort});
groups.get(groupKey)!.rows.push({i: r.i, sort});
} else {
groups.set(groupKey, {group, rows: [{r, sort}]});
groups.set(groupKey, {group, rows: [{i: r.i, sort}]});
}
}

Expand All @@ -185,37 +190,35 @@ export default class LocalDataProvider extends ACommonDataProvider {

const types = ranking.toCompareValueType();

const groupHelper = Array.from(groups.values()).map((g) => {
if (isSortedBy) {
sortComplex(g.rows, types);
}
return Promise.all(Array.from(groups.values()).map((g) => {
const group = g.group;
return Promise.resolve(g.rows)
// sort -> worker
.then((rows) => !isSortedBy ? rows : sortComplex(rows, types))
// to indices -> worker
.then((rows) => sort2indices(rows, this._data.length))
// to group info
.then(({order, index2pos}) => {
const o: IOrderedGroup = Object.assign({order, index2pos}, group);

// compute sort group value
let sort: ICompareValue[] | null = null;
if (isGroupedSortedBy) {
const groupData = Object.assign({rows: Array.from(order).map((d) => this._data[d])}, group);
sort = ranking.toGroupCompareValue(groupData);
}
return {o, sort};
});
})).then((groupHelper) => {

// sort groups
if (isGroupedSortedBy) {
const groupData = Object.assign({rows: g.rows.map((d) => d.r)}, g.group);
return {g, sort: ranking.toGroupCompareValue(groupData)};
sortComplex(<{sort: ICompareValue[]}[]>groupHelper, ranking.toGroupCompareValueType());
} else {
groupHelper.sort((a, b) => a.o.name.toLowerCase().localeCompare(b.o.name.toLowerCase()));
}
return {g, sort: null};
});

// sort groups
if (isGroupedSortedBy) {
sortComplex(<{sort: ICompareValue[]}[]>groupHelper, ranking.toGroupCompareValueType());
} else {
groupHelper.sort((a, b) => a.g.group.name.toLowerCase().localeCompare(b.g.group.name.toLowerCase()));
}

let offset = 0;
return groupHelper.map(({g}) => {
//store the ranking index and create an argsort version, i.e. rank 0 -> index i
const order = chooseByLength(g.rows.length);
const index2pos = chooseByLength(this._data.length);

for (let i = 0; i < g.rows.length; ++i) {
const ri = g.rows[i].r.i;
order[i] = ri;
index2pos[ri] = offset + i;
}
offset += g.rows.length;
return Object.assign({order, index2pos}, g.group);
return groupHelper.map((d) => d.o);
});
}

Expand Down
17 changes: 16 additions & 1 deletion src/provider/sort.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ICompareValue, ECompareValueType} from '../model/Column';
import {FIRST_IS_NAN, FIRST_IS_MISSING} from '../model/missing';
import {chooseByLength} from '../model';

const missingFloat = FIRST_IS_NAN > 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
const missingInt = FIRST_IS_MISSING > 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
Expand Down Expand Up @@ -54,7 +55,7 @@ function toFunction(f: {asc: boolean, v: ECompareValueType}): (a: any, b: any)=
}
}

export function sortComplex(arr: {sort: ICompareValue[]}[], comparators: {asc: boolean, v: ECompareValueType}[]) {
export function sortComplex<T extends {sort: ICompareValue[]}>(arr: T[], comparators: {asc: boolean, v: ECompareValueType}[]) {
if (arr.length < 2) {
return arr;
}
Expand Down Expand Up @@ -85,4 +86,18 @@ export function sortComplex(arr: {sort: ICompareValue[]}[], comparators: {asc: b
return 0;
});
}
return arr;
}

export function sort2indices(arr: {i: number}[], rawLength: number) {
//store the ranking index and create an argsort version, i.e. rank 0 -> index i
const order = chooseByLength(arr.length);
const index2pos = chooseByLength(rawLength);

for (let i = 0; i < arr.length; ++i) {
const ri = arr[i].i;
order[i] = ri;
index2pos[ri] = i;
}
return {order, index2pos};
}

0 comments on commit 1263505

Please sign in to comment.