<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HiddenWatermark
{
    internal static class ScaledWatermarkCache
    {
        internal const int CacheSize = 10;

        internal static ConcurrentDictionary<string, int> _count = new ConcurrentDictionary<string, int>();
        internal static ConcurrentDictionary<string, byte[]> _cache = new ConcurrentDictionary<string, byte[]>();

        public static byte[] TryGetScaledWatermark(int width, int height)
        {
            var key = ToKey(width, height);
            _count[key] = _count.ContainsKey(key) ? _count[key] + 1 : 1;
            if (_cache.ContainsKey(key))
            {
                return _cache[key];
            }
            return null;
        }

        public static void AddScaledWatermark(int width, int height, byte[] watermarkBytes)
        {
            var key = ToKey(width, height);
            _cache[key] = watermarkBytes;
            TrimCache();
        }

        private static void TrimCache()
        {
            if (_cache.Count > CacheSize)
            {
                int min = 999;
                foreach (var pair in _cache)
                {
                    if (_count[pair.Key] < min)
                        min = _count[pair.Key];
                }

                foreach (var pair in _cache)
                {
                    if (_count[pair.Key] == min)
                    {
                        byte[] val;
                        _cache.TryRemove(pair.Key, out val);
                        break;
                    }<commit_msg>further leak fix<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HiddenWatermark
{
    internal static class ScaledWatermarkCache
    {
        internal const int CacheSize = 10;

        internal static ConcurrentDictionary<string, int> _count = new ConcurrentDictionary<string, int>();
        internal static ConcurrentDictionary<string, byte[]> _cache = new ConcurrentDictionary<string, byte[]>();

        public static byte[] TryGetScaledWatermark(int width, int height)
        {
            var key = ToKey(width, height);
            _count[key] = _count.ContainsKey(key) ? _count[key] + 1 : 1;
            if (_cache.ContainsKey(key))
            {
                return _cache[key];
            }
            return null;
        }

        public static void AddScaledWatermark(int width, int height, byte[] watermarkBytes)
        {
            var key = ToKey(width, height);
            if (_count[key] >= SmallestCacheCount())
            {
                _cache[key] = watermarkBytes;
                TrimCache();
            }
        }

        private static int SmallestCacheCount()
        {
            if (_cache.Count == 0)
                return 0;
            return _count.Where(c => _cache.ContainsKey(c.Key)).Select(c => c.Value).Min();
        }

        private static void TrimCache()
        {
            if (_cache.Count > CacheSize)
            {
                int min = 999;
                foreach (var pair in _cache)
                {
                    if (_count[pair.Key] < min)
                        min = _count[pair.Key];
                }

                foreach (var pair in _cache)
                {
                    if (_count[pair.Key] == min)
                    {
                        byte[] val;
                        _cache.TryRemove(pair.Key, out val);
                        break;
                    }
{'fraction_non_alphanumeric': 0.0904916261480281, 'fraction_numerical': 0.004321988114532685, 'mean_word_length': 1.4703135423615743, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, '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': '4214313', 'n_tokens_mistral': 973, 'n_tokens_neox': 918, 'n_words': 240}