示例#1
0
        public void AddOrChange(int level, double size)
        {
            List<CachedSize> levelList;
            if (cache.ContainsKey(level)) { levelList = cache[level]; }
            else
            {
                levelList = new List<CachedSize>(5);
                cache.Add(level, levelList);
            }

            CachedSize cachedSize = null;
            foreach (var s in levelList)
            {
                if (s.IsEqual(size))
                {
                    cachedSize = s;
                    break;
                }
            }

            if (cachedSize == null)
            {
                // if list is full, replace item with lowest count, to give other items a chance
                if (levelList.Count > 4)
                {
                    cachedSize = new CachedSize { OccuranceCounter = int.MaxValue, Size = size };
                    int indexToReplace = 0;
                    int smallestCounter = int.MaxValue;
                    for (int i = 0; i < 5; i++)
                    {
                        if (levelList[i].OccuranceCounter < smallestCounter) indexToReplace = i;
                    }
                    levelList[indexToReplace].OccuranceCounter = 1;
                    levelList[indexToReplace].Size = size;
                    cachedSize = levelList[indexToReplace];
                }
                else
                {
                    // add new size to list
                    cachedSize = new CachedSize { OccuranceCounter = 1, Size = size };
                    levelList.Add(cachedSize);
                }
            }
            else
            {
                // prevent overflow
                if(cachedSize.OccuranceCounter == int.MaxValue)
                {
                    foreach (var s in levelList)
                    {
                        s.OccuranceCounter = s.OccuranceCounter / 2;
                    }
                }

                // count occurance up
                cachedSize.OccuranceCounter++;
            }
        }
示例#2
0
            public double GetEstimate(int level)
            {
                if (cache.TryGetValue(level, out var cachedLevel))
                {
                    var maxUsedSize = new CachedSize {
                        OccuranceCounter = 0
                    };
                    foreach (var s in cachedLevel)
                    {
                        if (maxUsedSize.OccuranceCounter < s.OccuranceCounter)
                        {
                            maxUsedSize = s;
                        }
                    }

                    return(maxUsedSize.Size);
                }

                return(0);
            }
示例#3
0
        public double GetEstimate(int level)
        {
            if (cache.ContainsKey(level))
            {
                CachedSize maxUsedSize = new CachedSize {
                    OccuranceCounter = 0
                };
                foreach (var s in cache[level])
                {
                    if (maxUsedSize.OccuranceCounter < s.OccuranceCounter)
                    {
                        maxUsedSize = s;
                    }
                }

                return(maxUsedSize.Size);
            }

            return(0);
        }
示例#4
0
            public void AddOrChange(int level, double size)
            {
                List <CachedSize> levelList;

                if (cache.TryGetValue(level, out var cachedLevels))
                {
                    levelList = cachedLevels;
                }
                else
                {
                    levelList = new List <CachedSize>(5);
                    cache.Add(level, levelList);
                }

                CachedSize cachedSize = null;

                foreach (var s in levelList)
                {
                    if (s.IsEqual(size))
                    {
                        cachedSize = s;
                        break;
                    }
                }

                if (cachedSize == null)
                {
                    // if list is full, replace item with lowest count, to give other items a chance
                    if (levelList.Count > 4)
                    {
                        cachedSize = new CachedSize {
                            OccuranceCounter = int.MaxValue, Size = size
                        };
                        var indexToReplace  = 0;
                        var smallestCounter = int.MaxValue;
                        for (var i = 0; i < 5; i++)
                        {
                            if (levelList[i].OccuranceCounter < smallestCounter)
                            {
                                indexToReplace = i;
                            }
                        }
                        levelList[indexToReplace].OccuranceCounter = 1;
                        levelList[indexToReplace].Size             = size;
                        cachedSize = levelList[indexToReplace];
                    }
                    else
                    {
                        // add new size to list
                        cachedSize = new CachedSize {
                            OccuranceCounter = 1, Size = size
                        };
                        levelList.Add(cachedSize);
                    }
                }
                else
                {
                    // prevent overflow
                    if (cachedSize.OccuranceCounter == int.MaxValue)
                    {
                        foreach (var s in levelList)
                        {
                            s.OccuranceCounter = s.OccuranceCounter / 2;
                        }
                    }

                    // count occurance up
                    cachedSize.OccuranceCounter++;
                }
            }
示例#5
0
        public double GetEstimate(int level)
        {
            if (cache.ContainsKey(level))
            {
                CachedSize maxUsedSize = new CachedSize { OccuranceCounter = 0 };
                foreach (var s in cache[level])
                {
                    if (maxUsedSize.OccuranceCounter < s.OccuranceCounter) maxUsedSize = s;
                }

                return maxUsedSize.Size;
            }

            return 0;
        }