Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
掘金者说
vscode
提交
01a4a6db
V
vscode
项目概览
掘金者说
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
01a4a6db
编写于
2月 02, 2016
作者:
J
Joao Moreno
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
range map: consolidate, docs
上级
fa937974
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
127 addition
and
33 deletion
+127
-33
src/vs/base/parts/list/rangeMap.ts
src/vs/base/parts/list/rangeMap.ts
+70
-27
src/vs/base/parts/list/test/rangeMap.test.ts
src/vs/base/parts/list/test/rangeMap.test.ts
+57
-6
未找到文件。
src/vs/base/parts/list/rangeMap.ts
浏览文件 @
01a4a6db
...
...
@@ -18,6 +18,10 @@ export interface IRangedGroup {
size
:
number
;
}
/**
* Returns the intersection between two ranges as a range itself.
* Returns `null` if the intersection is empty.
*/
export
function
intersect
(
one
:
IRange
,
other
:
IRange
):
IRange
{
if
(
one
.
start
>=
other
.
end
||
other
.
start
>=
one
.
end
)
{
return
null
;
...
...
@@ -33,6 +37,10 @@ export function intersect(one: IRange, other: IRange): IRange {
return
{
start
,
end
};
}
/**
* Returns the intersection between a ranged group and a range.
* Returns `[]` if the intersection is empty.
*/
export
function
groupIntersect
(
range
:
IRange
,
groups
:
IRangedGroup
[]):
IRangedGroup
[]
{
const
result
:
IRangedGroup
[]
=
[];
...
...
@@ -60,14 +68,48 @@ export function groupIntersect(range: IRange, groups: IRangedGroup[]): IRangedGr
return
result
;
}
/**
* Shifts a range by that `much`.
*/
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.
*
* Consolidation is the process of merging consecutive ranged groups
* that share the same `size`.
*/
export
function
consolidate
(
groups
:
IRangedGroup
[]):
IRangedGroup
[]
{
const
result
:
IRangedGroup
[]
=
[];
let
previousGroup
:
IRangedGroup
=
null
;
for
(
const
group
of
groups
)
{
const
start
=
group
.
range
.
start
;
const
end
=
group
.
range
.
end
;
const
size
=
group
.
size
;
if
(
previousGroup
&&
size
===
previousGroup
.
size
)
{
previousGroup
.
range
.
end
=
end
;
continue
;
}
previousGroup
=
{
range
:
{
start
,
end
},
size
};
result
.
push
(
previousGroup
);
}
return
result
;
}
export
class
RangeMap
{
private
groups
:
IRangedGroup
[]
=
[];
...
...
@@ -93,7 +135,7 @@ export class RangeMap {
return
result
;
});
this
.
groups
=
con
cat
(
before
,
middle
,
after
);
this
.
groups
=
con
solidate
(
concat
(
before
,
middle
,
after
)
);
}
get
count
():
number
{
...
...
@@ -110,41 +152,42 @@ export class RangeMap {
return
this
.
groups
.
reduce
((
t
,
g
)
=>
t
+
(
g
.
size
*
(
g
.
range
.
end
-
g
.
range
.
start
)),
0
);
}
//
indexAt(position: number): number {
//
let index = 0;
//
let size = 0;
indexAt
(
position
:
number
):
number
{
let
index
=
0
;
let
size
=
0
;
// for (const range of this._range
s) {
// const newSize = size + (range.count * range
.size);
for
(
const
group
of
this
.
group
s
)
{
const
newSize
=
size
+
((
group
.
range
.
end
-
group
.
range
.
start
)
*
group
.
size
);
//
if (position < newSize) {
// return index + Math.floor((position - size) / range
.size);
//
}
if
(
position
<
newSize
)
{
return
index
+
Math
.
floor
((
position
-
size
)
/
group
.
size
);
}
// index += range
.size;
//
size = newSize;
//
}
index
+=
group
.
size
;
size
=
newSize
;
}
//
return -1;
//
}
return
-
1
;
}
//
positionAt(index: number): number {
//
let position = 0;
//
let count = 0;
positionAt
(
index
:
number
):
number
{
let
position
=
0
;
let
count
=
0
;
// for (const range of this._ranges) {
// const newCount = count + range.count;
for
(
const
group
of
this
.
groups
)
{
const
groupCount
=
group
.
range
.
end
-
group
.
range
.
start
;
const
newCount
=
count
+
groupCount
;
//
if (index < newCount) {
// return position + ((index - count) * range
.size);
//
}
if
(
index
<
newCount
)
{
return
position
+
((
index
-
count
)
*
group
.
size
);
}
// position += range.count * range
.size;
//
count = newCount;
//
}
position
+=
groupCount
*
group
.
size
;
count
=
newCount
;
}
//
return -1;
//
}
return
-
1
;
}
dispose
()
{
this
.
groups
=
null
;
...
...
src/vs/base/parts/list/test/rangeMap.test.ts
浏览文件 @
01a4a6db
...
...
@@ -4,7 +4,13 @@
*--------------------------------------------------------------------------------------------*/
import
assert
=
require
(
'
assert
'
);
import
{
RangeMap
,
intersect
,
groupIntersect
,
IRangedGroup
}
from
'
../rangeMap
'
;
import
{
RangeMap
,
intersect
,
groupIntersect
,
IRangedGroup
,
consolidate
}
from
'
../rangeMap
'
;
suite
(
'
RangeMap
'
,
()
=>
{
var
rangeMap
:
RangeMap
;
...
...
@@ -92,11 +98,56 @@ suite('RangeMap', () => {
);
});
// test('splice', () => {
// rangeMap.splice(0, 0, { count: 5, size: 1 });
// rangeMap.splice(2, 0, { count: 5, size: 2 });
// console.log(rangeMap.groups);
// });
test
(
'
consolidate
'
,
()
=>
{
assert
.
deepEqual
(
consolidate
([]),
[]);
assert
.
deepEqual
(
consolidate
([{
range
:
{
start
:
0
,
end
:
10
},
size
:
1
}]),
[{
range
:
{
start
:
0
,
end
:
10
},
size
:
1
}]
);
assert
.
deepEqual
(
consolidate
([
{
range
:
{
start
:
0
,
end
:
10
},
size
:
1
},
{
range
:
{
start
:
10
,
end
:
20
},
size
:
1
}
]),
[{
range
:
{
start
:
0
,
end
:
20
},
size
:
1
}]
);
assert
.
deepEqual
(
consolidate
([
{
range
:
{
start
:
0
,
end
:
10
},
size
:
1
},
{
range
:
{
start
:
10
,
end
:
20
},
size
:
1
},
{
range
:
{
start
:
20
,
end
:
100
},
size
:
1
}
]),
[{
range
:
{
start
:
0
,
end
:
100
},
size
:
1
}]
);
assert
.
deepEqual
(
consolidate
([
{
range
:
{
start
:
0
,
end
:
10
},
size
:
1
},
{
range
:
{
start
:
10
,
end
:
20
},
size
:
5
},
{
range
:
{
start
:
20
,
end
:
30
},
size
:
10
}
]),
[
{
range
:
{
start
:
0
,
end
:
10
},
size
:
1
},
{
range
:
{
start
:
10
,
end
:
20
},
size
:
5
},
{
range
:
{
start
:
20
,
end
:
30
},
size
:
10
}
]
);
assert
.
deepEqual
(
consolidate
([
{
range
:
{
start
:
0
,
end
:
10
},
size
:
1
},
{
range
:
{
start
:
10
,
end
:
20
},
size
:
2
},
{
range
:
{
start
:
20
,
end
:
100
},
size
:
2
}
]),
[
{
range
:
{
start
:
0
,
end
:
10
},
size
:
1
},
{
range
:
{
start
:
10
,
end
:
100
},
size
:
2
}
]
);
});
test
(
'
empty
'
,
()
=>
{
assert
.
equal
(
rangeMap
.
size
,
0
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录