/// <summary> /// Tries to mark an object. Returns false if the object is already in the cycle detector, /// ie, it has already been encountered (which implies that it's part of a cycle). /// </summary> public bool TryMark(object obj) { if (obj == null) { return(true); } if (obj.GetType().IsPrimitive) { return(true); } if (obj.GetType().IsValueType) { return(true); } if (_objects.IsEmpty) { _objects = fiOption.Just(Factory.GetInstance()); } if (IsCycle(obj)) { return(false); } _objects.Value.Add(obj); return(true); }
/// <summary> /// Decrease the recursion / nesting depth. /// </summary> public void Exit() { if (_depth == 0) { throw new InvalidOperationException("Mismatched Enter/Exit"); } --_depth; if (_depth == 0 && _objects.HasValue) { Factory.ReuseInstance(_objects.Value); _objects = fiOption <HashSet <object> > .Empty; } }
public fiCycleDetector(params fiCycleDetector[] parents) { _parents = parents; _objects = fiOption <HashSet <object> > .Empty; }