addDeduplicate method

void addDeduplicate(
  1. DatastoreBundle other,
  2. bool deduplicate
)

Adds the content of another DatastoreBundle to this one, with an option to deduplicate the elements.

If deduplicate is true, this method will check for duplicates before adding elements, which is more expensive.

Implementation

void addDeduplicate(DatastoreBundle other, bool deduplicate) {
  if (deduplicate) {
    // Hash-based membership test: build a Set from the current content once so
    // each lookup is O(1) instead of the O(n) List.contains() scan. Set.add()
    // returns false when the element is already present, so we only append to
    // the list when the item is genuinely new. This turns the merge from
    // O(n^2) into O(n).
    final Set<PointOfInterest> knownPois = pointOfInterests.toSet();
    for (PointOfInterest poi in other.pointOfInterests) {
      if (knownPois.add(poi)) {
        pointOfInterests.add(poi);
      }
    }
    final Set<Way> knownWays = ways.toSet();
    for (Way way in other.ways) {
      if (knownWays.add(way)) {
        ways.add(way);
      }
    }
  } else {
    pointOfInterests.addAll(other.pointOfInterests);
    ways.addAll(other.ways);
  }
}