Skip to content

Commit

Permalink
refactor: separate SingleRoot and MultipleRoots Quadtrees in their ow…
Browse files Browse the repository at this point in the history
…n files
  • Loading branch information
Jei-sKappa committed Oct 3, 2024
1 parent 968147a commit 7bfece6
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 265 deletions.
2 changes: 2 additions & 0 deletions lib/fast_quadtree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export 'src/quadtree.dart';
export 'src/cached_quadtree.dart';
export 'src/expandable_quadtree.dart';
export 'src/horizontally_expandable_quadtree.dart';
export 'src/multiple_roots_quadtree.dart';
export 'src/quadrant.dart';
export 'src/quadrant_location.dart';
export 'src/single_root_quadtree.dart';
export 'src/vertically_expandable_quadtree.dart';
1 change: 1 addition & 0 deletions lib/src/expandable_quadtree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:fast_quadtree/src/helpers/calculate_quadrant_location_from_rect.
import 'package:fast_quadtree/src/quadrant.dart';
import 'package:fast_quadtree/src/quadrant_location.dart';
import 'package:fast_quadtree/src/quadtree.dart';
import 'package:fast_quadtree/src/single_root_quadtree.dart';
import 'package:fast_quadtree/src/extensions/expand_quadrant.dart';
import 'package:fast_quadtree/src/extensions/move_quadrant.dart';

Expand Down
120 changes: 120 additions & 0 deletions lib/src/multiple_roots_quadtree.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import 'dart:math';
import 'dart:ui';

import 'package:equatable/equatable.dart';
import 'package:fast_quadtree/src/quadrant.dart';
import 'package:fast_quadtree/src/quadtree.dart';

abstract class MultipleRootsQuadtree<T>
with EquatableMixin
implements Quadtree<T> {
MultipleRootsQuadtree(
Quadrant quadrant, {
this.maxItems = 5,
this.maxDepth = 4,
required this.getBounds,
}) {
quadtreeNodes[0] = QuadtreeNode<T>(
quadrant,
tree: this,
);
}

@override
final int maxItems;

@override
final int maxDepth;

@override
double get left => firstNode.quadrant.left;

@override
double get top => firstNode.quadrant.top;

@override
double get width => firstNode.quadrant.width;

@override
double get height => firstNode.quadrant.height;

@override
final Rect Function(T) getBounds;

int _depth = 0;

@override
int get depth => _depth;

@override
set depth(int newDepth) => _depth = newDepth;

int _negativeDepth = 0;

@override
int get negativeDepth => _negativeDepth;

@override
set negativeDepth(int newNegativeDepth) => _negativeDepth = newNegativeDepth;

@override
void communicateNewNodeDepth(int newDepth) => depth = max(depth, newDepth);

final Map<int, QuadtreeNode<T>> quadtreeNodes = {};

QuadtreeNode<T> get firstNode => quadtreeNodes[0]!;

@override
bool insertAll(List<T> items) {
bool valid = true;

for (final item in items) {
valid = insert(item);
}

return valid;
}

@override
void removeAll(List<T> items) {
for (final item in items) {
remove(item);
}
}

@override
void localizedRemoveAll(List<T> items) {
for (final item in items) {
localizedRemove(item);
}
}

@override
List<Quadrant> getAllQuadrants() {
List<Quadrant> results = [];

for (final node in quadtreeNodes.values) {
results.addAll(node.getAllQuadrants());
}

return results;
}

@override
List<T> getAllItems({bool removeDuplicates = true}) {
List<T> results = [];

for (final node in quadtreeNodes.values) {
results.addAll(node.getAllItems(removeDuplicates: removeDuplicates));
}

return results;
}

@override
void clear() {
for (final node in quadtreeNodes.values) {
node.clear();
}
}
}
Loading

0 comments on commit 7bfece6

Please sign in to comment.