<commit_before>using Buddhabrot.Core.Boundary.Corners;

namespace Buddhabrot.Core.Boundary;

public static class BoundaryCalculator
{
    public static IReadOnlyList<(RegionId Region, RegionType Type)>
        FindBoundaryAndFilaments(
            BoundaryParameters boundaryParameters,
            CancellationToken cancelToken = default)
    {
        var cornerComputer = new RegionCorners(boundaryParameters);
        HashSet<RegionId> visitedRegions = new();
        Queue<RegionId> regionsToCheck = new();
        regionsToCheck.Enqueue(new RegionId(0, 0));

        var returnList = new List<(RegionId, RegionType)>();

        while (regionsToCheck.Any() && !cancelToken.IsCancellationRequested)
        {
            var region = regionsToCheck.Dequeue();

            if (visitedRegions.Contains(region))
                continue;

            var corners = cornerComputer.GetRegionCorners(region);

            bool containsFilament = corners.ContainsBorder || cornerComputer.DoesRegionContainFilaments(region);

            var regionType = (corners.ContainsBorder, containsFilament) switch
            {
                (true, _) => RegionType.Border,
                (false, true) => RegionType.Filament,
                _ => RegionType.Empty,
            };

            visitedRegions.Add(region);

            if (regionType != RegionType.Empty)
            {
                returnList.Add((region, regionType));

                AddRegionToCheck(region.MoveUp());
                AddRegionToCheck(region.MoveDown());
                AddRegionToCheck(region.MoveLeft());
                AddRegionToCheck(region.MoveRight());

                AddRegionToCheck(region.MoveUpLeft());
                AddRegionToCheck(region.MoveUpRight());
                AddRegionToCheck(region.MoveDownLeft());
                AddRegionToCheck(region.MoveDownRight());
            }
        }

        return returnList;

        void AddRegionToCheck(RegionId region)
        {
            if (region.X >= 0 &&
                region.Y >= 0 &&
                region.X < (boundaryParameters.Divisions.QuadrantDivisions * 2) &&
                region.Y < boundaryParameters.Divisions.QuadrantDivisions &&
                !visitedRegions.Contains(region))
            {
                regionsToCheck.Enqueue(region);
            }
        }
    }
}<commit_msg>Switched BoundaryCalculator to stack for minimal benefit<commit_after>using Buddhabrot.Core.Boundary.Corners;

namespace Buddhabrot.Core.Boundary;

public static class BoundaryCalculator
{
    public static IReadOnlyList<(RegionId Region, RegionType Type)>
        FindBoundaryAndFilaments(
            BoundaryParameters boundaryParameters,
            CancellationToken cancelToken = default)
    {
        var cornerComputer = new RegionCorners(boundaryParameters);
        HashSet<RegionId> visitedRegions = new();
        Stack<RegionId> regionsToCheck = new();
        regionsToCheck.Push(new RegionId(0, 0));

        var returnList = new List<(RegionId, RegionType)>();

        while (regionsToCheck.Any() && !cancelToken.IsCancellationRequested)
        {
            var region = regionsToCheck.Pop();

            if (visitedRegions.Contains(region))
                continue;

            var corners = cornerComputer.GetRegionCorners(region);

            bool containsFilament = corners.ContainsBorder || cornerComputer.DoesRegionContainFilaments(region);

            var regionType = (corners.ContainsBorder, containsFilament) switch
            {
                (true, _) => RegionType.Border,
                (false, true) => RegionType.Filament,
                _ => RegionType.Empty,
            };

            visitedRegions.Add(region);

            if (regionType != RegionType.Empty)
            {
                returnList.Add((region, regionType));

                AddRegionToCheck(region.MoveUp());
                AddRegionToCheck(region.MoveDown());
                AddRegionToCheck(region.MoveLeft());
                AddRegionToCheck(region.MoveRight());

                AddRegionToCheck(region.MoveUpLeft());
                AddRegionToCheck(region.MoveUpRight());
                AddRegionToCheck(region.MoveDownLeft());
                AddRegionToCheck(region.MoveDownRight());
            }
        }

        return returnList;

        void AddRegionToCheck(RegionId region)
        {
            if (region.X >= 0 &&
                region.Y >= 0 &&
                region.X < (boundaryParameters.Divisions.QuadrantDivisions * 2) &&
                region.Y < boundaryParameters.Divisions.QuadrantDivisions &&
                !visitedRegions.Contains(region))
            {
                regionsToCheck.Push(region);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09421661409043113, 'fraction_numerical': 0.002103049421661409, 'mean_word_length': 2.0235219326128417, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5557430', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1168, 'n_words': 198}