懒得勤快 %!s(int64=5) %!d(string=hai) anos
pai
achega
d62f4fe536

+ 1 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -2,7 +2,7 @@
 
   <PropertyGroup>
     <TargetFramework>netcoreapp3.0</TargetFramework>
-    <Version>2.2.7.6</Version>
+    <Version>2.2.8</Version>
     <Authors>懒得勤快</Authors>
     <Company>masuit.com</Company>
     <Description>包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。

BIN=BIN
Masuit.Tools/Properties/AssemblyInfo.cs


+ 8 - 8
Masuit.Tools/RandomSelector/Algorithm/BinarySearchOptimizer.cs

@@ -12,18 +12,18 @@ namespace Masuit.Tools.RandomSelector.Algorithm
         /// <returns></returns>
         public static int[] GetCumulativeWeights<T>(List<WeightedItem<T>> items)
         {
-            int TotalWeight = 0;
-            int Index = 0;
-            var ResultArray = new int[items.Count + 1];
+            int totalWeight = 0;
+            int index = 0;
+            var resultArray = new int[items.Count + 1];
 
-            foreach (var Item in items)
+            foreach (var item in items)
             {
-                TotalWeight += Item.Weight;
-                ResultArray[Index] = TotalWeight;
-                Index++;
+                totalWeight += item.Weight;
+                resultArray[index] = totalWeight;
+                index++;
             }
 
-            return ResultArray;
+            return resultArray;
         }
     }
 }

+ 11 - 11
Masuit.Tools/RandomSelector/Algorithm/MultipleSelector.cs

@@ -16,19 +16,19 @@ namespace Masuit.Tools.RandomSelector.Algorithm
         internal List<T> Select(int count)
         {
             Validate(ref count);
-            var Items = new List<WeightedItem<T>>(WeightedSelector.Items);
-            var ResultList = new List<T>();
+            var items = new List<WeightedItem<T>>(WeightedSelector.Items);
+            var resultList = new List<T>();
 
             do
             {
-                var Item = WeightedSelector.Options.AllowDuplicates ? Select(Items) : SelectWithLinearSearch(Items);
-                ResultList.Add(Item.Value);
+                var item = WeightedSelector.Options.AllowDuplicates ? Select(items) : SelectWithLinearSearch(items);
+                resultList.Add(item.Value);
                 if (!WeightedSelector.Options.AllowDuplicates)
                 {
-                    Items.Remove(Item);
+                    items.Remove(item);
                 }
-            } while (ResultList.Count < count);
-            return ResultList;
+            } while (resultList.Count < count);
+            return resultList;
         }
 
         private void Validate(ref int count)
@@ -38,16 +38,16 @@ namespace Masuit.Tools.RandomSelector.Algorithm
                 throw new InvalidOperationException("筛选个数必须大于0");
             }
 
-            var Items = WeightedSelector.Items;
+            var items = WeightedSelector.Items;
 
-            if (Items.Count == 0)
+            if (items.Count == 0)
             {
                 throw new InvalidOperationException("没有元素可以被筛选");
             }
 
-            if (!WeightedSelector.Options.AllowDuplicates && Items.Count < count)
+            if (!WeightedSelector.Options.AllowDuplicates && items.Count < count)
             {
-                count = Items.Count;
+                count = items.Count;
             }
         }
     }

+ 16 - 18
Masuit.Tools/RandomSelector/Algorithm/SelectorBase.cs

@@ -7,18 +7,16 @@ namespace Masuit.Tools.RandomSelector.Algorithm
     internal abstract class SelectorBase<T>
     {
         protected readonly WeightedSelector<T> WeightedSelector;
-        private readonly System.Random _rng;
 
         internal SelectorBase(WeightedSelector<T> weightedSelector)
         {
             WeightedSelector = weightedSelector;
-            _rng = new System.Random();
         }
 
         protected int GetSeed(List<WeightedItem<T>> items)
         {
-            var TopRange = items.Sum(i => i.Weight) + 1;
-            return _rng.Next(1, TopRange);
+            var sum = items.Sum(i => i.Weight) + 1;
+            return new Random().Next(1, sum);
         }
 
         /// <summary>
@@ -31,8 +29,8 @@ namespace Masuit.Tools.RandomSelector.Algorithm
                 throw new InvalidOperationException("没有元素可以筛选");
             }
 
-            var Seed = GetSeed(items);
-            return BinarySearch(items, Seed);
+            var seed = GetSeed(items);
+            return BinarySearch(items, seed);
         }
 
         /// <summary>
@@ -46,8 +44,8 @@ namespace Masuit.Tools.RandomSelector.Algorithm
                 throw new InvalidOperationException("没有元素可以筛选");
             }
 
-            var Seed = GetSeed(items);
-            return LinearSearch(items, Seed);
+            var seed = GetSeed(items);
+            return LinearSearch(items, seed);
         }
 
         /// <summary>
@@ -58,13 +56,13 @@ namespace Masuit.Tools.RandomSelector.Algorithm
         /// <returns></returns>
         private WeightedItem<T> LinearSearch(IEnumerable<WeightedItem<T>> items, int seed)
         {
-            var RunningCount = 0;
-            foreach (var Item in items)
+            var count = 0;
+            foreach (var item in items)
             {
-                RunningCount += Item.Weight;
-                if (seed <= RunningCount)
+                count += item.Weight;
+                if (seed <= count)
                 {
-                    return Item;
+                    return item;
                 }
             }
 
@@ -79,15 +77,15 @@ namespace Masuit.Tools.RandomSelector.Algorithm
         /// <returns></returns>
         private WeightedItem<T> BinarySearch(List<WeightedItem<T>> items, int seed)
         {
-            int Index = Array.BinarySearch(WeightedSelector.CumulativeWeights, seed);
+            int index = Array.BinarySearch(WeightedSelector.CumulativeWeights, seed);
 
-            //如果存在接近匹配项,二进制搜索返回的负数会比搜索的第1个索引少1。
-            if (Index < 0)
+            //如果存在接近匹配项,二进制搜索返回的负数会比搜索的第1个索引少1。
+            if (index < 0)
             {
-                Index = -Index - 1;
+                index = -index - 1;
             }
 
-            return items[Index];
+            return items[index];
         }
     }
 }

+ 2 - 3
Masuit.Tools/RandomSelector/Algorithm/SingleSelector.cs

@@ -14,13 +14,12 @@ namespace Masuit.Tools.RandomSelector.Algorithm
 
         internal T Select()
         {
-            var Items = WeightedSelector.Items;
-            if (Items.Count == 0)
+            if (WeightedSelector.Items.Count == 0)
             {
                 throw new InvalidOperationException("没有元素可以筛选");
             }
 
-            return Select(Items).Value;
+            return Select(WeightedSelector.Items).Value;
         }
     }
 }

+ 2 - 2
Masuit.Tools/RandomSelector/Extensions.cs

@@ -20,12 +20,12 @@ namespace Masuit.Tools.RandomSelector
             return selector.Items.OrderBy(item => item.Weight).ToList();
         }
 
-        public static T WeightedItem<T>(this List<WeightedItem<T>> list)
+        public static T WeightedItem<T>(this IEnumerable<WeightedItem<T>> list)
         {
             return new WeightedSelector<T>(list).Select();
         }
 
-        public static List<T> WeightedItem<T>(this List<WeightedItem<T>> list, int count)
+        public static List<T> WeightedItem<T>(this IEnumerable<WeightedItem<T>> list, int count)
         {
             return new WeightedSelector<T>(list).SelectMultiple(count);
         }

+ 12 - 7
Masuit.Tools/RandomSelector/WeightedSelector.cs

@@ -32,7 +32,12 @@ namespace Masuit.Tools.RandomSelector
 
         public WeightedSelector(List<WeightedItem<T>> items, SelectorOptions options = null) : this(options)
         {
-            Items = items;
+            Add(items);
+        }
+
+        public WeightedSelector(IEnumerable<WeightedItem<T>> items, SelectorOptions options = null) : this(options)
+        {
+            Add(items);
         }
 
         /// <summary>
@@ -61,9 +66,9 @@ namespace Masuit.Tools.RandomSelector
         /// <param name="items"></param>
         public void Add(IEnumerable<WeightedItem<T>> items)
         {
-            foreach (var Item in items)
+            foreach (var item in items)
             {
-                Add(Item);
+                Add(item);
             }
         }
 
@@ -93,8 +98,8 @@ namespace Masuit.Tools.RandomSelector
         public T Select()
         {
             CalculateCumulativeWeights();
-            var Selector = new SingleSelector<T>(this);
-            return Selector.Select();
+            var selector = new SingleSelector<T>(this);
+            return selector.Select();
         }
 
         /// <summary>
@@ -103,8 +108,8 @@ namespace Masuit.Tools.RandomSelector
         public List<T> SelectMultiple(int count)
         {
             CalculateCumulativeWeights();
-            var Selector = new MultipleSelector<T>(this);
-            return Selector.Select(count);
+            var selector = new MultipleSelector<T>(this);
+            return selector.Select(count);
         }
 
         /// <summary>

+ 1 - 1
Masuit.Tools/package.nuspec

@@ -4,7 +4,7 @@
     <!--*-->
     <id>Masuit.Tools</id>
     <!--*-->
-    <version>2.2.7.6</version>
+    <version>2.2.8</version>
     <title>Masuit.Tools</title>
     <!--*-->
     <authors>masuit.com</authors>

+ 19 - 2
Test/Program.cs

@@ -1,5 +1,7 @@
-using Masuit.Tools.Win32;
+using Masuit.Tools.RandomSelector;
 using System;
+using System.Collections.Generic;
+using System.Linq;
 
 namespace Test
 {
@@ -7,7 +9,22 @@ namespace Test
     {
         static void Main(string[] args)
         {
-            Console.WriteLine(Windows.GetLocalUsedIP());
+            var result = new List<string>();
+            for (int i = 0; i < 1000; i++)
+            {
+                result.Add(new List<WeightedItem<string>>()
+                {
+                    new WeightedItem<string>("A", 1),
+                    new WeightedItem<string>("B", 3),
+                    new WeightedItem<string>("C", 4),
+                    new WeightedItem<string>("D", 4),
+                }.WeightedItem());
+            }
+
+            foreach (var g in result.GroupBy(s => s).OrderByDescending(g => g.Count()))
+            {
+                Console.WriteLine(g.Key + ":" + g.Count());
+            }
         }
     }
 }