Skip to content

Commit

Permalink
createHitmap: refactored to make it more efficient, use SplayTreeMap …
Browse files Browse the repository at this point in the history
…so items are ordered by key
  • Loading branch information
kevmoo committed May 19, 2015
1 parent 5ee179d commit d292a21
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions lib/src/hitmap.dart
@@ -1,31 +1,30 @@
library coverage.hitmap;

import 'dart:async';
import 'dart:collection';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';

/// Creates a single hitmap from a raw json object. Throws away all entries that
/// are not resolvable.
Map createHitmap(List<Map> json) {
Map<String, Map<int, int>> hitMap = {};
Map<String, Map<int, int>> hitMap = new SplayTreeMap<String, Map<int, int>>();

addToMap(source, line, count) {
if (!hitMap[source].containsKey(line)) {
hitMap[source][line] = 0;
}
hitMap[source][line] += count;
void addToMap(Map<int, int> map, int line, int count) {
var oldCount = map.putIfAbsent(line, () => 0);
map[line] = count + oldCount;
}

json.forEach((Map e) {
for (Map e in json) {
var source = e['source'];
if (source == null) {
// Couldnt resolve import, so skip this entry.
return;
}
if (!hitMap.containsKey(source)) {
hitMap[source] = {};
// Couldn't resolve import, so skip this entry.
continue;
}

var childHitMap =
hitMap.putIfAbsent(source, () => new SplayTreeMap<int, int>());
var hits = e['hits'];
// hits is a flat array of the following format:
// [ <line|linerange>, <hitcount>,...]
Expand All @@ -35,19 +34,19 @@ Map createHitmap(List<Map> json) {
var k = hits[i];
if (k is num) {
// Single line.
addToMap(source, k, hits[i + 1]);
addToMap(childHitMap, k, hits[i + 1]);
}
if (k is String) {
// Linerange. We expand line ranges to actual lines at this point.
var splitPos = k.indexOf('-');
int start = int.parse(k.substring(0, splitPos));
int end = int.parse(k.substring(splitPos + 1, k.length));
for (var j = start; j <= end; j++) {
addToMap(source, j, hits[i + 1]);
addToMap(childHitMap, j, hits[i + 1]);
}
}
}
});
}
return hitMap;
}

Expand Down

0 comments on commit d292a21

Please sign in to comment.