<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using BruTile;
using BruTile.Cache;
using Mapsui.Geometries;
using Mapsui.Providers;

namespace Mapsui.Rendering
{
    public class RenderGetStrategy : IRenderGetStrategy
    {
        public IList<IFeature> GetFeatures(BoundingBox box, double resolution, ITileSchema schema, ITileCache<Feature> memoryCache)
        {
            var dictionary = new Dictionary<TileIndex, IFeature>();
            var levelId = BruTile.Utilities.GetNearestLevel(schema.Resolutions, resolution);
            GetRecursive(dictionary, schema, memoryCache, box.ToExtent(), levelId);
            var sortedFeatures = dictionary.OrderByDescending(t => schema.Resolutions[t.Key.Level].UnitsPerPixel);
            return sortedFeatures.ToDictionary(pair => pair.Key, pair => pair.Value).Values.ToList();
        }
        
        public static void GetRecursive(IDictionary<TileIndex, IFeature> resultTiles, ITileSchema schema,
            ITileCache<Feature> cache, Extent extent, string levelId)
        {
            var resolution = schema.Resolutions[levelId].UnitsPerPixel;
            var tiles = schema.GetTilesInView(extent, resolution);

            foreach (var tileInfo in tiles)
            {
                var feature = cache.Find(tileInfo.Index);
                var levels = schema.Resolutions.Where(r => r.Value.UnitsPerPixel > resolution)
                    .OrderBy(r => r.Value.UnitsPerPixel);
                var nextLevelId = levels.FirstOrDefault().Key;

                if (feature == null)
                {
                    if (nextLevelId != null) GetRecursive(resultTiles, schema, cache, tileInfo.Extent.Intersect(extent), nextLevelId);
                }
                else
                {
                    resultTiles[tileInfo.Index] = feature;
                }
            }
        }
    }
}
<commit_msg>Improved tile retrieval during rendering<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using BruTile;
using BruTile.Cache;
using Mapsui.Geometries;
using Mapsui.Providers;

namespace Mapsui.Rendering
{
    public class RenderGetStrategy : IRenderGetStrategy
    {
        public IList<IFeature> GetFeatures(BoundingBox box, double resolution, ITileSchema schema, ITileCache<Feature> memoryCache)
        {
            var dictionary = new Dictionary<TileIndex, IFeature>();
            var levelId = BruTile.Utilities.GetNearestLevel(schema.Resolutions, resolution);
            GetRecursive(dictionary, schema, memoryCache, box.ToExtent(), levelId);
            var sortedFeatures = dictionary.OrderByDescending(t => schema.Resolutions[t.Key.Level].UnitsPerPixel);
            return sortedFeatures.ToDictionary(pair => pair.Key, pair => pair.Value).Values.ToList();
        }

        public static void GetRecursive(IDictionary<TileIndex, IFeature> resultTiles, ITileSchema schema,
            ITileCache<Feature> cache, Extent extent, string levelId)
        {
            // to improve performance, convert the resolutions to a list so they can be walked up by
            // simply decrementing an index when the level index needs to change
            var resolutions = schema.Resolutions.OrderByDescending(pair => pair.Value.UnitsPerPixel).ToList();
            for (int i = 0; i < resolutions.Count; i++)
            {
                if (levelId == resolutions[i].Key)
                {
                    GetRecursive(resultTiles, schema, cache, extent, resolutions, i);
                    break;
                }
            }
        }

        private static void GetRecursive(IDictionary<TileIndex, IFeature> resultTiles, ITileSchema schema,
            ITileCache<Feature> cache, Extent extent, IList<KeyValuePair<string, Resolution>> resolutions, int resolutionIndex)
        {
            if (resolutionIndex < 0 || resolutionIndex >= resolutions.Count)
                return;

            var tiles = schema.GetTilesInView(extent, resolutions[resolutionIndex].Key);

            foreach (var tileInfo in tiles)
            {
                var feature = cache.Find(tileInfo.Index);

                if (feature == null)
                {
                    GetRecursive(resultTiles, schema, cache, tileInfo.Extent.Intersect(extent), resolutions, resolutionIndex - 1);
                }
                else
                {
                    resultTiles[tileInfo.Index] = feature;
                }
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.08544784211667751, 'fraction_numerical': 0.0006506180871828237, 'mean_word_length': 2.0664893617021276, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 28, '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': '2254937', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1162, 'n_words': 273}