Skip to content

Commit d9a849c

Browse files
authored
change getLinePos from O(n/2) to O(log(N)) (#439)
* change getLinePos from O(n/2) to O(log(N)) * refactor gruntfile * added hxnodejs to travisCI
1 parent 896c656 commit d9a849c

File tree

13 files changed

+123
-26
lines changed

13 files changed

+123
-26
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ matrix:
1515
- haxe: 3.4.7
1616
install:
1717
- haxelib git tokentree https://github.com/HaxeCheckstyle/tokentree master src
18+
- haxelib git hxnodejs https://github.com/HaxeFoundation/hxnodejs.git master
1819
- haxelib install haxeparser 3.3.0
1920
- haxelib install compiletime 2.6.0
2021
- haxelib install hxargs 3.0.2
@@ -26,6 +27,7 @@ matrix:
2627
- haxelib git hxparse https://github.com/simn/hxparse
2728
- haxelib git haxeparser https://github.com/simn/haxeparser
2829
- haxelib git mcover https://github.com/massiveinteractive/mcover master src
30+
- haxelib git hxnodejs https://github.com/HaxeFoundation/hxnodejs.git master
2931
- haxelib install mconsole
3032
- haxelib install compiletime 2.6.0
3133
- haxelib install hxargs 3.0.2

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## dev branch / next version (2.x.x)
22

3+
- Refactored `Checker.getLinePos` to use binary search, reduces runtime from O(N/2) to O(log N) [#439](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/439)
4+
35
## version 2.4.1 (2018-06-14)
46

57
- Added JSON schemas for `checkstyle.json` and `checkstyle-exclude.json` file formats [#431](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/431) + [#432](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/432) + [#433](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/433)

buildAll.hxml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ buildDebug.hxml
66

77
--next
88

9+
buildJS.hxml
10+
11+
--next
12+
913
buildSchema.hxml
1014

1115
--next

gruntfile.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,8 @@ module.exports = function(grunt) {
1212
"haxelib install tokentree"
1313
}
1414
},
15-
1615
haxe: haxeOptions(),
17-
18-
zip: {
19-
"haxe-checkstyle.zip": [
20-
"src/**",
21-
"resources/sample-config.json", "resources/logo.png", "resources/codeclimate_pr.png",
22-
"haxelib.json", "run.n", "README.md", "CHANGES.md"
23-
]
24-
}
16+
zip: zipIt()
2517
});
2618

2719
grunt.loadNpmTasks("grunt-haxe");
@@ -48,4 +40,18 @@ function haxeOptions() {
4840
hxml: "buildTelemetry.hxml"
4941
}
5042
};
43+
}
44+
45+
function zipIt() {
46+
return {
47+
release: {
48+
src: [
49+
"src/**",
50+
"resources/sample-config.json", "resources/logo.png", "resources/codeclimate_pr.png",
51+
"haxelib.json", "run.n", "README.md", "CHANGES.md", "LICENCE.md"
52+
],
53+
dest: "haxe-checkstyle.zip",
54+
compression: "DEFLATE",
55+
}
56+
};
5157
}

src/checkstyle/Checker.hx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import checkstyle.reporter.ReporterManager;
1414
import tokentree.TokenTreeBuilder;
1515

1616
class Checker {
17+
static inline var BAD_OFFSET:String = "Bad offset";
1718

1819
public var file:CheckFile;
1920
public var lines:Array<String>;
@@ -33,6 +34,7 @@ class Checker {
3334
checks = [];
3435
baseDefines = [];
3536
defineCombinations = [];
37+
linesIdx = [];
3638
}
3739

3840
public function addCheck(check:Check) {
@@ -64,10 +66,27 @@ class Checker {
6466
}
6567

6668
public function getLinePos(off:Int):LinePos {
67-
for (i in 0...linesIdx.length) {
68-
if (linesIdx[i].l <= off && linesIdx[i].r >= off) return { line:i, ofs: off - linesIdx[i].l };
69+
var lowerBound:Int = 0;
70+
var upperBound:Int = linesIdx.length - 1;
71+
if (linesIdx.length <= 0) throw BAD_OFFSET;
72+
if (off < 0) throw BAD_OFFSET;
73+
if (off > linesIdx[upperBound].r) throw BAD_OFFSET;
74+
while (true) {
75+
if (lowerBound > upperBound) throw BAD_OFFSET;
76+
var center:Int = lowerBound + Math.floor((upperBound - lowerBound) / 2);
77+
var matchLeft:Bool = linesIdx[center].l <= off;
78+
var matchRight:Bool = linesIdx[center].r >= off;
79+
if (matchLeft && matchRight) return { line: center, ofs: off - linesIdx[center].l };
80+
if (matchLeft) {
81+
lowerBound = center + 1;
82+
continue;
83+
}
84+
if (matchRight) {
85+
upperBound = center - 1;
86+
continue;
87+
}
6988
}
70-
throw "Bad offset";
89+
throw BAD_OFFSET;
7190
}
7291

7392
public function getString(off:Int, off2:Int):String {

test/TestSuite.hx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import checks.CheckTestCase;
2+
import config.ConfigParserTest;
3+
import config.ExcludeManagerTest;
24
import detect.DetectCodingStyleTest;
5+
import misc.CheckerTest;
36
#if !eval
47
import misc.ThreadTest;
58
#end
6-
import config.ConfigParserTest;
7-
import config.ExcludeManagerTest;
89

910
class TestSuite extends massive.munit.TestSuite {
1011

@@ -14,6 +15,7 @@ class TestSuite extends massive.munit.TestSuite {
1415
CompileTime.importPackage("checks");
1516
CompileTime.importPackage("misc");
1617

18+
add(CheckerTest);
1719
add(ConfigParserTest);
1820
add(ExcludeManagerTest);
1921
add(DetectCodingStyleTest);

test/checks/CheckTestCase.hx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@ package checks;
22

33
import byte.ByteData;
44

5-
import haxe.PosInfos;
6-
75
import checkstyle.CheckMessage;
86
import checkstyle.CheckFile;
97
import checkstyle.reporter.IReporter;
108
import checkstyle.reporter.ReporterManager;
119
import checkstyle.Checker;
1210
import checkstyle.checks.Check;
1311

14-
import massive.munit.Assert;
15-
1612
class CheckTestCase<T:String> {
1713

1814
static inline var FILE_NAME:String = "Test.hx";

test/config/ConfigParserTest.hx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package config;
22

3-
import massive.munit.Assert;
4-
53
import checkstyle.config.Config;
64
import checkstyle.config.ConfigParser;
75
import checkstyle.utils.ConfigUtils;

test/config/ExcludeManagerTest.hx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package config;
22

3-
import massive.munit.Assert;
4-
53
import checkstyle.checks.type.DynamicCheck;
64
import checkstyle.checks.type.ReturnCheck;
75

test/detect/DetectCodingStyleTest.hx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ import checkstyle.checks.whitespace.WhitespaceCheckBase.WhitespacePolicy;
5353
import checkstyle.checks.whitespace.WrapCheckBase.WrapCheckBaseOption;
5454
import checkstyle.detect.DetectCodingStyle;
5555

56-
import massive.munit.Assert;
57-
5856
class DetectCodingStyleTest {
5957

6058
// checkstyle.checks.block

0 commit comments

Comments
 (0)