提交 54010e9d 编写于 作者: J Joao Moreno

range map: refactor

上级 76c72a77
......@@ -75,14 +75,6 @@ function shift({ start, end }: IRange, much: number): IRange {
return { start: start + much, end: end + much };
}
/**
* Concatenates several collections of ranged groups into a single
* collection.
*/
function concat(...groups: IRangedGroup[][]): IRangedGroup[] {
return groups.reduce((r, g) => r.concat(g), [] as IRangedGroup[]);
}
/**
* Consolidates a collection of ranged groups.
*
......@@ -110,32 +102,41 @@ export function consolidate(groups: IRangedGroup[]): IRangedGroup[] {
return result;
}
/**
* Concatenates several collections of ranged groups into a single
* collection.
*/
function concat(...groups: IRangedGroup[][]): IRangedGroup[] {
return consolidate(groups.reduce((r, g) => r.concat(g), [] as IRangedGroup[]));
}
export class RangeMap {
private groups: IRangedGroup[] = [];
splice(index: number, deleteCount: number, ...groups: IGroup[]): void {
const insertCount = groups.reduce((t, r) => t + r.count, 0);
const diff = insertCount - deleteCount;
const before = groupIntersect({ start: 0, end: index }, this.groups);
const after = groupIntersect({ start: index + deleteCount, end: Number.POSITIVE_INFINITY }, this.groups)
.map<IRangedGroup>(g => ({ range: shift(g.range, diff), size: g.size }));
let diff = -deleteCount;
let index2 = index;
const middle = groups
.filter(g => g.count > 0 && g.size > 0)
.map<IRangedGroup>(g => {
const end = index + g.count;
const end = index2 + g.count;
const result = {
range: { start: index, end },
range: { start: index2, end },
size: g.size
};
index = end;
diff += g.count;
index2 = end;
return result;
});
this.groups = consolidate(concat(before, middle, after));
const before = groupIntersect({ start: 0, end: index }, this.groups);
const after = groupIntersect({ start: index + deleteCount, end: Number.POSITIVE_INFINITY }, this.groups)
.map<IRangedGroup>(g => ({ range: shift(g.range, diff), size: g.size }));
this.groups = concat(before, middle, after);
}
get count(): number {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册