molstar
Version:
A comprehensive macromolecular library.
48 lines • 1.47 kB
JavaScript
/**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { Column } from '../db';
var GroupingImpl = /** @class */ (function () {
function GroupingImpl(getKey) {
this.getKey = getKey;
this.map = new Map();
this.keys = [];
this.groups = [];
}
GroupingImpl.prototype.add = function (a) {
var key = this.getKey(a);
if (!!this.map.has(key)) {
var group = this.map.get(key);
group[group.length] = a;
}
else {
var group = [a];
this.map.set(key, group);
this.keys[this.keys.length] = key;
this.groups[this.groups.length] = group;
}
};
GroupingImpl.prototype.getGrouping = function () {
return { keys: this.keys, groups: this.groups, map: this.map };
};
return GroupingImpl;
}());
export function Grouper(getKey) {
return new GroupingImpl(getKey);
}
export function groupBy(values, getKey) {
var gs = Grouper(getKey);
if (Column.is(values)) {
var v = values.value;
for (var i = 0, _i = values.rowCount; i < _i; i++)
gs.add(v(i));
}
else {
for (var i = 0, _i = values.length; i < _i; i++)
gs.add(values[i]);
}
return gs.getGrouping();
}
//# sourceMappingURL=grouping.js.map