当前位置: 首页>>代码示例>>C#>>正文


C# OrderedBag.Add方法代码示例

本文整理汇总了C#中OrderedBag.Add方法的典型用法代码示例。如果您正苦于以下问题:C# OrderedBag.Add方法的具体用法?C# OrderedBag.Add怎么用?C# OrderedBag.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在OrderedBag的用法示例。


在下文中一共展示了OrderedBag.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetNeighbourhood

        private static ICollection<Path> GetNeighbourhood()
        {
            OrderedBag<Path> neighbourhood = new OrderedBag<Path>();

            neighbourhood.Add(new Path(new House("1"), new House("2"), 2));
            neighbourhood.Add(new Path(new House("1"), new House("3"), 22));
            neighbourhood.Add(new Path(new House("1"), new House("10"), 7));
            neighbourhood.Add(new Path(new House("2"), new House("10"), 12));
            neighbourhood.Add(new Path(new House("2"), new House("9"), 4));
            neighbourhood.Add(new Path(new House("2"), new House("3"), 1));
            neighbourhood.Add(new Path(new House("3"), new House("5"), 7));
            neighbourhood.Add(new Path(new House("4"), new House("3"), 9));
            neighbourhood.Add(new Path(new House("10"), new House("8"), 12));
            neighbourhood.Add(new Path(new House("8"), new House("6"), 17));
            neighbourhood.Add(new Path(new House("8"), new House("7"), 8));
            neighbourhood.Add(new Path(new House("5"), new House("7"), 9));
            neighbourhood.Add(new Path(new House("6"), new House("5"), 18));
            neighbourhood.Add(new Path(new House("4"), new House("5"), 7));
            neighbourhood.Add(new Path(new House("4"), new House("6"), 13));
            neighbourhood.Add(new Path(new House("4"), new House("9"), 4));
            neighbourhood.Add(new Path(new House("8"), new House("9"), 5));
            neighbourhood.Add(new Path(new House("4"), new House("8"), 6));

            return neighbourhood;
        }
开发者ID:deyantodorov,项目名称:TelerikAcademy,代码行数:25,代码来源:TvCompanyProgram.cs

示例2: Calc

    static long Calc(long n)
    {
        if (n == 1)
            return 0;

        var queue = new OrderedBag<State>((pair1, pair2) =>
            pair1.Value.CompareTo(pair2.Value)
        );

        queue.Add(new State(n, 0));

        while (queue.Count != 0)
        {
            var currentState = queue.RemoveFirst();

            if (currentState.Key == 1)
                return currentState.Value;

            for (int power = 2; power < 60; power++)
            {
                var powerBase = Math.Pow(currentState.Key, 1.0 / power);

                var nextNumber = (long)Math.Round(powerBase);
                var nextSteps = Math.Abs(Pow(nextNumber, power) - currentState.Key);

                var nextState = new State(nextNumber, currentState.Value + nextSteps + 1);
                queue.Add(nextState);
            }
        }

        throw new ArgumentException();
    }
开发者ID:dgrigorov,项目名称:TelerikAcademy-1,代码行数:32,代码来源:Program.cs

示例3: Dijkstra

    private static IList<int> Dijkstra(int start)
    {
        var distances = Enumerable.Repeat(int.MaxValue, graph.Count).ToArray();

        var queue = new OrderedBag<Node>((node1, node2) =>
            node1.Distance.CompareTo(node2.Distance)
        );

        distances[start] = 0;
        queue.Add(new Node(start, 0));

        while (queue.Count != 0)
        {
            var currentNode = queue.RemoveFirst();

            foreach (var neighborNode in graph[currentNode.To])
            {
                int currentDistance = distances[currentNode.To] + neighborNode.Distance;

                if (currentDistance < distances[neighborNode.To])
                {
                    distances[neighborNode.To] = currentDistance;
                    queue.Add(new Node(neighborNode.To, currentDistance));
                }
            }

            // Removing repeating is actually slower?
        }

        return distances;
    }
开发者ID:dgrigorov,项目名称:TelerikAcademy-1,代码行数:31,代码来源:Program.cs

示例4: Main

        public static void Main(string[] args)
        {
            for (int i = 0; i < 4; i++)
            {
                var start = new KeyValuePair<BigInteger, int>(1, 0);
                BigInteger desired = BigInteger.Parse(Console.ReadLine());
                var results = new OrderedBag<KeyValuePair<BigInteger, int>>((x, y) => x.Value.CompareTo(y.Value));
                results.Add(start);

                var current = start;

                HashSet<BigInteger> used = new HashSet<BigInteger>();
                while (current.Key != desired)
                {
                    current = results.RemoveFirst();
                    if (!used.Contains(current.Key))
                    {
                        results.Add(new KeyValuePair<BigInteger, int>(current.Key + 1, current.Value + 1));

                        if (current.Key != 1)
                        {
                            BigInteger toPower = 1;
                            List<BigInteger> powers = new List<BigInteger>();
                            while (toPower <= desired - 1)
                            {
                                toPower *= current.Key;
                                powers.Add(toPower);
                            }

                            powers.Reverse();

                            foreach (var item in powers)
                            {
                                if (item <= desired + 1 && !used.Contains(item))
                                {
                                    if (!used.Contains(item))
                                    {
                                        results.Add(new KeyValuePair<BigInteger, int>(item, current.Value + 1));
                                    }

                                    if (!used.Contains(item - 1))
                                    {
                                        results.Add(new KeyValuePair<BigInteger, int>(item - 1, current.Value + 2));
                                    }
                                }
                            }
                        }

                        used.Add(current.Key);
                    }
                }

                Console.WriteLine(current.Value);
            }
        }
开发者ID:ilkodzhambazov,项目名称:Telerik-Academy,代码行数:55,代码来源:Program.cs

示例5: Run

        static public Questionnaire Run()
        {
            Questionnaire questionnaire = new Questionnaire();
            OrderedBag<Grille> OPEN = new OrderedBag<Grille>();
            OrderedBag<Grille> CLOSE = new OrderedBag<Grille>();
            Grille S = new Grille();
            OPEN.Add(S);
            while (OPEN.Count != 0)
            {
                Grille n = OPEN.RemoveFirst();
                CLOSE.Add(n);
                questionnaire.solutionsExplorer.Add(n.getStringEtat());
                if (n.getDistanceSolution() == 0)
                {
                    questionnaire.solutionMot = n.getSolutionMot();
                    questionnaire.solutionVisuelle = n.getSolutionVisuelle();
                    for (int i = 0; i < questionnaire.solutionVisuelle.Count; i++)
                    {

                        Console.Write("\n---Étape" + i + "----\n" +  questionnaire.solutionVisuelle[i] + "----------");
                    }
                    return questionnaire;
                }
                foreach (Grille nPrime in n.getListSuccessor())
                {
                    questionnaire.etatExplorer.Add(nPrime.getStringEtat());
                    if (Contient(OPEN, nPrime) != -1)
                    {
                        int position = Contient(OPEN, nPrime);
                        if (nPrime.getTotalDistance() < OPEN[position].getTotalDistance())
                        {
                            OPEN.Remove(OPEN[position]);
                            OPEN.Add(nPrime);
                        }
                    }
                    else if (Contient(CLOSE, nPrime) != -1)
                    {
                        int position = Contient(CLOSE, nPrime);
                        if (nPrime.getTotalDistance() < CLOSE[position].getTotalDistance())
                        {
                            CLOSE.Remove(CLOSE[position]);
                            OPEN.Add(nPrime);
                        }
                    }
                    else // Ni dans Close , ni dans OPEN
                    {
                        OPEN.Add(nPrime);
                    }
                }
            }
            questionnaire.solutionMot = "Aucun chemin possible";
            return questionnaire;
        }
开发者ID:LordCatzorz,项目名称:IFT-4102-TP1,代码行数:53,代码来源:Solutionneur.cs

示例6: GetMinimalConnectionCost

        public static long GetMinimalConnectionCost(string[] input)
        {
            var graph = new Dictionary<int, List<Edge>>();

            foreach (var edge in input)
            {
                var parts = edge.Split();
                var from = int.Parse(parts[0]);
                var to = int.Parse(parts[1]);
                var cost = int.Parse(parts[2]);

                InitializeIfNeeded(graph, from);
                InitializeIfNeeded(graph, to);

                graph[from].Add(new Edge(from, to, cost));
                graph[to].Add(new Edge(to, from, cost));
            }

            var startNode = graph.First().Key;

            var priorityQueue = new OrderedBag<Edge>();

            foreach (var neighbour in graph[startNode])
            {
                priorityQueue.Add(neighbour);
            }

            var cables = new List<Edge>();

            var visitedNodes = new HashSet<int> { startNode };

            while (priorityQueue.Count > 0)
            {
                var min = priorityQueue.RemoveFirst();

                if (visitedNodes.Contains(min.To))
                {
                    continue;
                }

                cables.Add(min);

                visitedNodes.Add(min.To);

                foreach (var neighbour in graph[min.To])
                {
                    priorityQueue.Add(neighbour);
                }
            }

            return cables.Sum(c => c.Cost);
        }
开发者ID:RuzmanovDev,项目名称:TelerikAcademy,代码行数:52,代码来源:TvCompany.cs

示例7: Main

        static void Main()
        {
            OrderedBag<Product> products = new OrderedBag<Product>();

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            for (int i = 1; i < 500000; i++)
            {
                Product p = new Product();
                p.Name = "Prodcut" + i;
                p.Price = GetRandomNumber(35, 599) * i * GetRandomNumber(3, 5) / GetRandomNumber(2, 4);
                products.Add(p);
            }

            stopwatch.Stop();
            Console.WriteLine("Create and Add 500k products: {0}", stopwatch.Elapsed);

            List<Product> prodRange = new List<Product>();
            stopwatch.Reset();
            stopwatch.Restart();

            for (int i = 1; i <= 10000; i++)
            {
                int min = GetRandomNumber(35, 599) * i * GetRandomNumber(3, 5) / GetRandomNumber(2, 4);
                int max = GetRandomNumber(35, 599) * i * 13 * GetRandomNumber(3, 5);

                prodRange.AddRange(products.Range(new Product() { Price = min }, true, new Product() { Price = max }, true).Take(20));
            }

            stopwatch.Stop();
            Console.WriteLine("Search for 10k random price ranges: {0}", stopwatch.Elapsed);
        }
开发者ID:nzhul,项目名称:TelerikAcademy,代码行数:33,代码来源:Program.cs

示例8: Main

        public static void Main()
        {
            var products = new OrderedBag<Product>();
            var builder = new StringBuilder();
            int n = int.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                var productPriceTokens = Console.ReadLine().Split();
                var name = productPriceTokens[0];
                var price = float.Parse(productPriceTokens[1]);

                products.Add(new Product(name, price));
            }

            var pricesTokens = Console.ReadLine().Split();
            var lower = float.Parse(pricesTokens[0]);
            var upper = float.Parse(pricesTokens[1]);

            var subrangeProducts = products.Range(new Product(string.Empty, lower), true, new Product(string.Empty, upper), true);

            foreach (var product in subrangeProducts)
            {
                Console.WriteLine(product.ToString());
            }
        }
开发者ID:Zakkgard,项目名称:Data-Structures-and-Algorithms,代码行数:26,代码来源:ProductsPriceRange.cs

示例9: Main

        public static void Main()
        {
            myWatch.Start();
            
            Random randomGen = new Random();
            OrderedBag<Product> myBag = new OrderedBag<Product>();

            for (int i = 0; i < 500000; i++)
            {
                myBag.Add(new Product("Product" + i, randomGen.Next(1, 1000000)));
            }

            int start = 0;
            int end = 1000;
            for (int i = 0; i < 10000; i++)
            {
                var subBag = myBag.Range(new Product(string.Empty, start), true, new Product(string.Empty, end), true);
                IList<Product> firstTwenty = GetFirstResults(subBag);

                ////Console.WriteLine("Results");
                ////foreach (Product product in firstTwenty)
                ////{
                ////    Console.WriteLine(product);
                ////}

                start += 10;
                end += 10;
            }

            myWatch.Stop();
            Console.WriteLine("END");
            Console.WriteLine("Time: {0}", myWatch.Elapsed);
        }
开发者ID:HansS,项目名称:TelerikAcademy-homework,代码行数:33,代码来源:Program.cs

示例10: Main

        static void Main()
        {
            OrderedBag<Product> bag = new OrderedBag<Product>();
            Random rand = new Random();
            for (int i = 0; i < CountProducts; i++)
            {
                bag.Add(GetRandomProduct(rand));
            }

            decimal minPrice = 0M;
            for (int i = 0; i < PriceSearches; i++)
            {
                Console.ForegroundColor = ConsoleColor.DarkMagenta;
                decimal maxPrice = minPrice + 100M;
                var result = GetProductsInPriceRange(bag, minPrice, maxPrice);
                Console.WriteLine(new string('$', 79));
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("First 20 Products in Price Range {0:C} - {1:C}: ", minPrice, maxPrice);
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                if (result.Count() < 1) { Console.Write("None\n"); }
                Console.WriteLine(string.Join(" | ", result));
                minPrice += 100;
                Thread.Sleep(500);
            }
        }
开发者ID:Boyan1912,项目名称:Data-Structures-And-Algorithms,代码行数:25,代码来源:Program.cs

示例11: Main

    internal static void Main()
    {
        var myStore = new OrderedBag<Product>();
        var ran = new Random();
        for (int i = 0; i < 500000; i++)
        {
            myStore.Add(new Product("prod." + ran.Next(1, 500001), ran.Next(1, 10011) / 100m));
        }

        var result = new StringBuilder();
        for (int i = 0; i < 10000; i++)
        {
            var prodList = myStore.Range(new Product(string.Empty, i + 1), true, new Product(string.Empty, i + 2), true);
            var counter = 0;
            foreach (var item in prodList)
            {
                if (counter == 20)
                {
                    break;
                }

                result.AppendFormat("{3}. Product: {0} Price: {1} BGN {2}", item.Name, item.Price, Environment.NewLine, counter + 1);
                counter++;
            }
        }

        Console.WriteLine(result);
    }
开发者ID:kalinalazarova1,项目名称:TelerikAcademy,代码行数:28,代码来源:Best20PricedProducts.cs

示例12: Main

    static void Main()
    {
        Console.WriteLine("Generating product's list...");
        var produkts = new OrderedBag<Product>();
        Random rnd = new Random();
        for (int i = 0; i < 500000; i++)
        {
            int price = rnd.Next(1, 100001);
            string name = "product-" + i;
            Product product = new Product(name, price);
            produkts.Add(product);
        }

        Console.WriteLine("Searching 20 products in range 10257 - 11336 lv");
        Predicate<Product> isProduktInRange = p => p.Price >= 10257 && p.Price <= 11336;
        var range = produkts.FindAll(isProduktInRange);
        int count = 0;
        foreach (var item in range)
        {
            Console.WriteLine(item);
            ++count;
            if (count == 20)
            {
                return;
            }
        }
    }
开发者ID:milen-vm,项目名称:Data-Structures,代码行数:27,代码来源:ProductsInPriceRange.cs

示例13: GetData

        private static void GetData(
            ref SortedDictionary<string, OrderedBag<Student>> listOfCourses,
            string fileName)
        {
            string line;
            using (StreamReader reader = new StreamReader(fileName))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    var entry = line.Split('|');

                    var fName = entry[0].Trim();
                    var lName = entry[1].Trim();
                    var course = entry[2].Trim();

                    Student student = new Student(fName, lName, course);

                    if (!listOfCourses.ContainsKey(course))
                    {
                        OrderedBag<Student> listOfStudents = new OrderedBag<Student>();
                        listOfStudents.Add(student);
                        listOfCourses.Add(course, listOfStudents);
                    }
                    else
                    {
                        listOfCourses[course].Add(student);
                    }
                }
            }
        }
开发者ID:niki-funky,项目名称:Telerik_Academy,代码行数:30,代码来源:Program.cs

示例14: Main

        static void Main(string[] args)
        {
            OrderedBag<ComparableKeyValuePair<string, int>> catalog =
                new OrderedBag<ComparableKeyValuePair<string, int>>();

            for (int i = 0; i < 25; i++)
            {
                ComparableKeyValuePair<string, int> item = new ComparableKeyValuePair<string, int>(i.ToString(), i);
                catalog.Add(item);
            }

            ComparableKeyValuePair<string, int> downRange = new ComparableKeyValuePair<string, int>("3", 3);
            ComparableKeyValuePair<string, int> upRange = new ComparableKeyValuePair<string, int>("23", 23);

            OrderedBag<ComparableKeyValuePair<string, int>>.View range = catalog.Range(downRange, true, upRange, true);
            Console.WriteLine("from 3");
            foreach (ComparableKeyValuePair<string, int> item in range)
            {
                Console.WriteLine(item.Value);
            }
            Console.WriteLine("to 23");


            // Test for 500 000 products and 10 000 price searches.

            catalog =
                new OrderedBag<ComparableKeyValuePair<string, int>>();

            Stopwatch timer = new Stopwatch();

            timer.Start();

            for (int i = 0; i <= 500000; i++)
            {
                ComparableKeyValuePair<string, int> item = new ComparableKeyValuePair<string, int>(i.ToString(), i);
                catalog.Add(item);
            }
            Console.WriteLine("Adding 500 000 elements: " + timer.Elapsed.TotalSeconds + " sec");
           

            OrderedBag<ComparableKeyValuePair<string, int>>.View bigRange = null;

            ComparableKeyValuePair<string, int> from = new ComparableKeyValuePair<string, int>("400000", 400000);
            ComparableKeyValuePair<string, int> to = new ComparableKeyValuePair<string, int>("410000", 410000);

            timer.Reset();
            timer.Start();

            for (int i = 10000; i <= 400000; i = i + 20)
            {
                bigRange = catalog.Range(from, true, to, false);

                from = new ComparableKeyValuePair<string, int>((i - 20).ToString(), (i - 20));
                to = new ComparableKeyValuePair<string, int>(i.ToString(), i);
            }

            Console.WriteLine("10 000 searches (200 000 to 400 000):" + timer.Elapsed.TotalMilliseconds + " ms");

            timer.Stop();
        }
开发者ID:vaster,项目名称:Telerik.vasko,代码行数:60,代码来源:Program.cs

示例15: Main

    static void Main()
    {
        const int PRODUCT_SEARCH = 20;

        OrderedBag<Item> bag = new OrderedBag<Item>();
        Random rand = new Random();

        for (int i = 0; i < 500000; i++)
        {
            bag.Add(new Item("ItemID: " + rand.Next(), rand.Next()));
        }

        var find = bag.Range(new Item("test", 0), true, new Item("test", 10000), true);

        int count = find.Count;
        if (count > PRODUCT_SEARCH)
        {
            count = PRODUCT_SEARCH;
        }

        for (int i = 0; i < count; i++)
        {
            Console.WriteLine("{0} ==== Price: {1}", find[i].Name, find[i].Price);
        }
    }
开发者ID:hristian-dimov,项目名称:TelerikAcademy,代码行数:25,代码来源:Program.cs


注:本文中的OrderedBag.Add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。