Skip to content

Commit 79f8132

Browse files
committed
bug fix
1 parent 6e09b48 commit 79f8132

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

QuadTrees/Common/QuadTreeCommon.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,11 @@ public bool RemoveAll(Func<TObject,bool> whereExpr)
274274
if (!whereExpr(kv.Key)) continue;
275275
set.Add(kv.Value);
276276
}
277-
var parents = new HashSet<TNode>();
278277
foreach (var s in set)
279278
{
280279
var owner = s.Owner;
281280
if (owner.Parent != null)
282281
{
283-
parents.Add(owner.Parent);
284282
Debug.Assert(owner.Parent.GetChildren().Any((a) => a == owner));
285283
}
286284
bool qtRemoved = owner.Remove(s);
@@ -289,22 +287,22 @@ public bool RemoveAll(Func<TObject,bool> whereExpr)
289287
Debug.Assert(dictRemoved);
290288
Debug.Assert(WrappedDictionary.Count == QuadTreePointRoot.Count);
291289
}
290+
var ret = set.Count != 0;
292291
Debug.Assert(WrappedDictionary.Count == QuadTreePointRoot.Count);
293-
do
292+
var done = new HashSet<TNode>();
293+
foreach (var qto in set)
294294
{
295-
var parents2 = new HashSet<TNode>();
296-
foreach (var p in parents)
295+
while (qto.Owner != null && done.Add(qto.Owner))
297296
{
298-
if (p.CleanThis() && p.Parent != null)
297+
if (qto.Owner.CleanThis())
299298
{
300-
parents2.Add(p.Parent);
299+
qto.Owner = qto.Owner.Parent;
301300
}
302301
}
303-
parents = parents2;
304-
} while (parents.Count != 0);
302+
}
305303

306304
Debug.Assert(WrappedDictionary.Count == QuadTreePointRoot.Count);
307-
return set.Count != 0;
305+
return ret;
308306
}
309307

310308
#endregion

QuadTrees/Common/QuadTreeNodeCommon.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,12 @@ internal bool CleanThis()
382382
}
383383
Debug.Assert(origCount + child._objectCount == Count);
384384
}
385-
child.Clear();
386385
if (child._objects != null)
387386
{
388387
Debug.Assert(child._objects.Take(child._objectCount).All(a => a.Owner != child));
389388
}
389+
child.Clear();
390+
Debug.Assert(child.IsEmpty);
390391
}
391392
else if (emptyChildren != 0 && !HasAtleast(MaxOptimizeDeletionReAdd))
392393
{
@@ -401,7 +402,7 @@ internal bool CleanThis()
401402
{
402403
c.Parent = null;
403404
}
404-
Clear();
405+
ClearRecursive();
405406
AddBulk(buffer.Keys.ToArray(), (a) => buffer[a]);
406407
#if DEBUG
407408
Debug.Assert(_objects == null || _objects.All((a) => a == null || a.Owner != oldOwners[a.Data] || a.Owner == this));
@@ -413,11 +414,19 @@ internal bool CleanThis()
413414
}
414415
Debug.Assert(Count == beforeCount);
415416
Debug.Assert(_objects == null || _objects.All((a) => a == null || a.Owner == this));
416-
return true;
417417
}
418418
return true;
419419
}
420420

421+
private void ClearRecursive()
422+
{
423+
foreach (var child in GetChildren())
424+
{
425+
child.ClearRecursive();
426+
}
427+
Clear();
428+
}
429+
421430
private UInt32 EncodeMorton2(UInt32 x, UInt32 y)
422431
{
423432
return (Part1By1(y) << 1) + Part1By1(x);
@@ -853,7 +862,7 @@ public void GetAllObjects(Action<QuadTreeObject<T,TNode>> put)
853862

854863
for (int i = 0; i < _objectCount; i++)
855864
{
856-
Debug.Assert(_objects[i].Owner == this);
865+
if (_objects[i].Owner != this) break;//todo: better?
857866
put(_objects[i]);
858867
}
859868
}

0 commit comments

Comments
 (0)