Optimize presets.match

Previous implementation was linear in the number of
presets. This should be near constant time.
This commit is contained in:
John Firebaugh
2013-05-13 20:24:23 -07:00
parent 525d37c647
commit 9502729f5e
6 changed files with 83 additions and 26 deletions
+1
View File
@@ -246,6 +246,7 @@
<script src="spec/modes/add_point.js"></script>
<script src="spec/presets.js"></script>
<script src="spec/presets/preset.js"></script>
<script src="spec/presets/collection.js"></script>
<script src="spec/presets/category.js"></script>
+1
View File
@@ -87,6 +87,7 @@
<script src="spec/modes/add_point.js"></script>
<script src="spec/presets.js"></script>
<script src="spec/presets/preset.js"></script>
<script src="spec/presets/collection.js"></script>
<script src="spec/presets/category.js"></script>
+38
View File
@@ -0,0 +1,38 @@
describe("iD.presets", function() {
var p = {
other: {
tags: {},
geometry: ['point', 'vertex', 'line', 'area']
},
residential: {
tags: {
highway: 'residential'
},
geometry: ['line']
},
park: {
tags: {
leisure: 'park'
},
geometry: ['point', 'area']
}
};
var c = iD.presets().load({presets: p}),
w = iD.Way({tags: { highway: 'residential'}}),
g = iD.Graph().replace(w);
describe("#match", function() {
it("returns a collection containing presets matching a geometry and tags", function() {
var way = iD.Way({tags: { highway: 'residential'}}),
graph = iD.Graph([way]);
expect(c.match(way, graph).id).to.eql('residential');
});
it("returns an other preset when no tags match", function() {
var way = iD.Way({tags: {foo: 'bar'}}),
graph = iD.Graph([way]);
expect(c.match(way, graph).id).to.eql('other');
});
});
});
-6
View File
@@ -36,12 +36,6 @@ describe("iD.presets.Collection", function() {
});
});
describe("#matchTags", function() {
it("returns a new collection only containing presets matching an entity's tags", function() {
expect(c.matchTags(w, g)).to.eql(p.residential);
});
});
describe("#search", function() {
it("filters presets by name", function() {
expect(c.search("resid").collection.indexOf(p.residential) >= 0).to.eql(true);