Esempio n. 1
0
        /// <summary>
        /// 尾部推出测试
        /// </summary>
        public void PopTest()
        {
            var master = new SortSet <int, int>(0.25, 32);

            for (var i = 0; i < 100; i++)
            {
                master.Add(i, i);
            }

            for (var i = 0; i < 65536; i++)
            {
                Assert.AreEqual(65535 - i, master.Pop());
            }

            Assert.AreEqual(0, master.Count);
        }
Esempio n. 2
0
        public void SequentialAddTest()
        {
            var list = new SortSet <int, int>();

            for (var i = 0; i < 50000; i++)
            {
                list.Add(i, i);
            }

            int n = 0;

            foreach (var v in list)
            {
                Assert.AreEqual(n++, v);
            }
        }
Esempio n. 3
0
        public void SameScoreTest()
        {
            //根据有序集规则,后插入的相同分数将会被优先遍历到
            var master = new SortSet <int, int>(0.25, 32);

            master.Add(10, 10);
            master.Add(90, 10);
            master.Add(20, 10);
            master.Add(80, 10);

            var a = master.ToArray();

            Assert.AreEqual(80, a[0]);
            Assert.AreEqual(20, a[1]);
            Assert.AreEqual(90, a[2]);
            Assert.AreEqual(10, a[3]);
        }
Esempio n. 4
0
        public void ScoreRangeCount()
        {
            var list = new SortSet <int, int>();
            var lst  = new List <int>();
            int num  = 50000;
            var rand = new System.Random();

            for (var i = 0; i < num; i++)
            {
                var s = rand.Next(0, 1000);
                list.Add(i, s);
                if (s <= 100)
                {
                    lst.Add(i);
                }
            }
            Assert.AreEqual(lst.Count, list.GetRangeCount(0, 100));
        }
Esempio n. 5
0
        public void ReversEnumerator()
        {
            int num  = 50000;
            var list = new SortSet <int, int>();

            for (int i = 0; i < num; i++)
            {
                list.Add(i, i);
            }

            int n = 0;

            list.ReverseIterator();
            foreach (var v in list)
            {
                Assert.AreEqual(num - ++n, v);
            }
        }
Esempio n. 6
0
        public void ObjectInsertTest()
        {
            var list = new SortSet <object, int>();
            var objs = new List <object>();

            for (var i = 0; i < 10; i++)
            {
                var o = new object();
                objs.Add(o);
                list.Add(o, i);
            }

            var n = 0;

            foreach (var o in list)
            {
                Assert.AreSame(objs[n++], o);
            }
        }
Esempio n. 7
0
        public void ReversEnumerator()
        {
            int num  = 50000;
            var list = new SortSet <int, int>();

            for (int i = 0; i < num; i++)
            {
                list.Add(i, i);
            }

            int n = 0;
            var e = list.ReversEnumerator();

            while (e.MoveNext())
            {
                var v = e.Current;
                Assert.AreEqual(num - ++n, v);
            }
        }
Esempio n. 8
0
        /// <inheritdoc />
        public bool AddListener(string eventName, Action <EventArgs> listener, int priority = 0)
        {
            if (string.IsNullOrEmpty(eventName) || listener == null)
            {
                return(false);
            }

            if (!listeners.TryGetValue(eventName, out SortSet <Action <EventArgs>, int> collection))
            {
                listeners[eventName] = collection = new SortSet <Action <EventArgs>, int>();
            }
            else if (collection.Contains(listener))
            {
                return(false);
            }

            collection.Add(listener, priority);
            return(true);
        }
Esempio n. 9
0
        public void TestRank()
        {
            var sortSets = new SortSet <int, int>();

            sortSets.Add(1000, 85);
            sortSets.Add(999, 75);
            sortSets.Add(998, 185);
            sortSets.Add(997, 85);
            sortSets.Add(996, 185);
            sortSets.Add(995, 85);

            Assert.AreEqual(1, sortSets.GetRank(995));
            Assert.AreEqual(995, sortSets.GetElementByRank(1));
            Assert.AreEqual(997, sortSets.GetElementByRank(2));
            Assert.AreEqual(1000, sortSets.GetElementByRank(3));
            Assert.AreEqual(996, sortSets.GetElementByRank(4));
            Assert.AreEqual(998, sortSets.GetElementByRank(5));

            Assert.AreEqual(3, sortSets.GetRangeCount(80, 90));
        }
Esempio n. 10
0
        public void GetElementRangeByScore()
        {
            var list = new SortSet <int, int>();

            for (var i = 0; i < 10; i++)
            {
                list.Add(i, i);
            }

            var elements = list.GetElementRangeByScore(3, 8);
            var n        = 3;

            foreach (var e in elements)
            {
                Assert.AreEqual(n++, e);
            }
            var list2 = new SortSet <int, int>();

            Assert.AreEqual(0, list2.GetElementRangeByScore(3, 8).Length);
        }
Esempio n. 11
0
        public void EmptyListForeach()
        {
            var master = new SortSet <int, int>();

            foreach (var v in master)
            {
                Assert.Fail();
                if (v < 0)
                {
                }
            }

            master.ReverseIterator();
            foreach (var v in master)
            {
                Assert.Fail();
                if (v < 0)
                {
                }
            }
        }
Esempio n. 12
0
        public void ClearTest()
        {
            var master = new SortSet <int, int>(0.25, 32);

            for (var i = 0; i < 65536; i++)
            {
                master.Add(i, i);
            }
            master.Clear();
            for (var i = 0; i < 65536; i++)
            {
                master.Add(i, i);
            }

            for (var i = 0; i < 65536; i++)
            {
                Assert.AreEqual(i, master.GetRank(i));
            }

            Assert.AreEqual(65536, master.Count);
        }
Esempio n. 13
0
        public void GetElementByRank()
        {
            var num  = 50000;
            var list = new SortSet <int, int>();

            for (var i = 0; i < num; i++)
            {
                list.Add(i, i);
            }
            var rand = new Random();

            for (var i = 0; i < Math.Min(num, 100); i++)
            {
                var rank = rand.Next(0, num);
                var val  = list.GetElementByRank(rank);

                if (rank != val)
                {
                    Assert.Fail();
                }
            }
        }
Esempio n. 14
0
        public void TestIterationsDeleted()
        {
            var master = new SortSet <int, int>(0.25, 32);

            for (var i = 0; i < 100; i++)
            {
                master.Add(i, i);
            }

            var assertList = new List <int>()
            {
                0, 10, 20, 30, 40, 50, 60, 70, 80, 90
            };
            var tmpList = new List <int>();

            foreach (var i in master)
            {
                tmpList.Add(i);
                master.Remove(i);
                master.Remove(i + 1);
                master.Remove(i + 2);
                master.Remove(i + 3);
                master.Remove(i + 4);
                master.Remove(i + 5);
                master.Remove(i + 6);
                master.Remove(i + 7);
                master.Remove(i + 8);
                master.Remove(i + 9);
            }

            var n = 0;

            foreach (var v in tmpList)
            {
                Assert.AreEqual(assertList[n++], v);
            }

            Assert.AreEqual(assertList.Count, tmpList.Count);
        }
Esempio n. 15
0
        public void ToArray()
        {
            var master = new SortSet <int, int>(0.25, 32);

            master.Add(10, 10);
            master.Add(90, 90);
            master.Add(20, 20);
            master.Add(80, 80);
            master.Add(0, 0);
            master.Add(40, 40);
            master.Add(50, 50);
            master.Add(60, 60);
            master.Add(30, 30);
            master.Add(70, 70);

            var i = 0;

            foreach (var e in master.ToArray())
            {
                Assert.AreEqual(i++ *10, e);
            }
        }
Esempio n. 16
0
 /// <summary>
 /// 构造一个配置容器
 /// </summary>
 public Config()
 {
     locators             = new SortSet <IConfigLocator, int>();
     typeStringConverters = new Dictionary <Type, ITypeStringConverter>
     {
         { typeof(bool), new BoolStringConverter() },
         { typeof(byte), new ByteStringConverter() },
         { typeof(char), new CharStringConverter() },
         { typeof(DateTime), new DateTimeStringConverter() },
         { typeof(decimal), new DecimalStringConverter() },
         { typeof(double), new DoubleStringConverter() },
         { typeof(Enum), new EnumStringConverter() },
         { typeof(short), new Int16StringConverter() },
         { typeof(int), new Int32StringConverter() },
         { typeof(long), new Int64StringConverter() },
         { typeof(sbyte), new SByteStringConverter() },
         { typeof(float), new SingleStringConverter() },
         { typeof(string), new StringStringConverter() },
         { typeof(ushort), new UInt16StringConverter() },
         { typeof(uint), new UInt32StringConverter() },
         { typeof(ulong), new UInt64StringConverter() }
     };
 }
Esempio n. 17
0
        public void TestCustomComparer()
        {
            var list = new SortSet <object, int>(new PriorityComparer());

            for (var i = 0; i < 10; i++)
            {
                list.Add(i, i);
            }

            Assert.AreEqual(9, list.Shift());
            Assert.AreEqual(8, list.Shift());
            Assert.AreEqual(7, list.Shift());
            Assert.AreEqual(6, list.Shift());
            Assert.AreEqual(5, list.Shift());
            Assert.AreEqual(4, list.Shift());
            Assert.AreEqual(3, list.Shift());
            Assert.AreEqual(2, list.Shift());
            Assert.AreEqual(1, list.Shift());
            Assert.AreEqual(0, list.Shift());

            list = new SortSet <object, int>();
            for (var i = 0; i < 10; i++)
            {
                list.Add(i, i);
            }

            Assert.AreEqual(0, list.Shift());
            Assert.AreEqual(1, list.Shift());
            Assert.AreEqual(2, list.Shift());
            Assert.AreEqual(3, list.Shift());
            Assert.AreEqual(4, list.Shift());
            Assert.AreEqual(5, list.Shift());
            Assert.AreEqual(6, list.Shift());
            Assert.AreEqual(7, list.Shift());
            Assert.AreEqual(8, list.Shift());
            Assert.AreEqual(9, list.Shift());
        }
Esempio n. 18
0
        public void RemoveRangeByScore()
        {
            var list = new SortSet <int, int>();

            for (var i = 0; i < 10; i++)
            {
                list.Add(i, i);
            }
            list.RemoveRangeByScore(3, 8);
            Assert.AreEqual(0, list.GetElementByRank(0));
            Assert.AreEqual(1, list.GetElementByRank(1));
            Assert.AreEqual(2, list.GetElementByRank(2));
            Assert.AreEqual(9, list.GetElementByRank(3));
            for (var i = 3; i < 9; i++)
            {
                list.Add(i, i);
            }
            list.Add(33, 3);
            list.RemoveRangeByScore(3, 3);
            Assert.AreEqual(0, list.GetElementByRank(0));
            Assert.AreEqual(1, list.GetElementByRank(1));
            Assert.AreEqual(2, list.GetElementByRank(2));
            Assert.AreEqual(4, list.GetElementByRank(3));
        }
Esempio n. 19
0
        public void GetElementByRankEmptyTest()
        {
            var master = new SortSet <int, int>();

            Assert.AreEqual(0, master.GetElementByRank(100));
        }
Esempio n. 20
0
        /// <inheritdoc />
        public bool GC(int ttl, int maxSize)
        {
            if (!Enable)
            {
                return(false);
            }

            cacheCollected = true;

            var candidates  = new SortSet <IMetaData, DateTime>();
            var directories = new Queue <string>();

            void AddCandidate(string file)
            {
                var meta = fileSystem.GetMetaData(file);

                candidates.Add(meta, meta.LastAccessTime);
            }

            if (!fileSystem.Exists(CacheDirectory, FileSystemOptions.Directory))
            {
                return(true);
            }

            var contents = fileSystem.GetContents(CacheDirectory);

            Array.ForEach(contents.GetDirectories(), directories.Enqueue);
            Array.ForEach(contents.GetFiles(), AddCandidate);

            while (directories.Count > 0)
            {
#pragma warning disable S4158 // bug: Empty collections should not be accessed or iterated
                contents = fileSystem.GetContents(directories.Dequeue());
#pragma warning restore S4158

                Array.ForEach(contents.GetDirectories(), directories.Enqueue);
                Array.ForEach(contents.GetFiles(), AddCandidate);
            }

            var freeSpace    = 0L;
            var deletedFiles = 0;

            // gc with ttl.
            var expire = DateTime.Now.AddSeconds(-ttl);
            foreach (var candidate in candidates)
            {
                if (candidate.LastAccessTime >= expire)
                {
                    // The sorset will have sorted the modification time.
                    break;
                }

                fileSystem.Delete(candidate.Path);
                candidates.Remove(candidate);
                freeSpace += candidate.Size;
                deletedFiles++;
            }

            void PromptFree()
            {
                io.WriteError($"Cache garbage collection completed, delete {deletedFiles} files, free {AbstractHelper.FormatMemory(freeSpace)} space.");
            }

            // gc with maxSize
            var totalSize = fileSystem.GetMetaData(CacheDirectory).Size;
            if (totalSize < maxSize)
            {
                PromptFree();
                return(true);
            }

            foreach (var candidate in candidates)
            {
                if (totalSize < maxSize)
                {
                    break;
                }

                fileSystem.Delete(candidate.Path);
                totalSize -= candidate.Size;
                freeSpace += candidate.Size;
                deletedFiles++;
            }

            PromptFree();
            return(true);
        }
Esempio n. 21
0
 /// <summary>
 /// 构建一个计时器管理器
 /// </summary>
 /// <param name="timeManager">时间管理器</param>
 public TimerManager([Inject(Required = true)] ITimeManager timeManager)
 {
     this.timeManager = timeManager;
     executeList      = new SortSet <TimerQueue, int>();
     timerQueue       = new Stack <TimerQueue>();
 }
Esempio n. 22
0
        /// <summary>
        /// 转换到指定目标并且删除
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="sortset">有序集</param>
        /// <param name="obj">对象</param>
        /// <returns>是否成功</returns>
        private bool ConvertAndRemove <T>(SortSet <T, int> sortset, object obj) where T : class
        {
            var ele = obj as T;

            return(ele != null && sortset.Remove(ele));
        }
Esempio n. 23
0
        public void EmptyListScoreRangeCount()
        {
            var list = new SortSet <int, int>();

            Assert.AreEqual(0, list.GetRangeCount(0, 100));
        }
Esempio n. 24
0
 public void Init()
 {
     sortset = new SortSet <int, int>();
 }