Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C#

Project Tool

Rate me:
Please Sign up or sign in to vote.
4.69/5 (10 votes)
23 Sep 2007CPOL3 min read 54.6K   1.7K   73  
Backup your C# solution and projects for archive or source code sharing. Temporary or unused files are excluded.
using System;
using System.Collections;
using System.Collections.Generic;

namespace QiHe.CodeLib
{
    /// <summary>
    /// Algorithm ��ժҪ˵����
    /// </summary>
    public class Algorithm
    {
        public static bool ArrayEqual(byte[] bytes, byte[] data)
        {
            if (bytes.Length == data.Length)
            {
                return ArrayEqual(bytes, data, bytes.Length);
            }
            else
            {
                return false;
            }
        }

        public static bool ArrayEqual(byte[] a, byte[] b, int count)
        {
            for (int i = 0; i < count; i++)
            {
                if (a[i] != b[i])
                {
                    return false;
                }
            }
            return true;
        }

        public static int[] ArrayIndice(int length)
        {
            int[] indice = new int[length];
            for (int i = 0; i < length; i++)
            {
                indice[i] = i;
            }
            return indice;
        }
        /// <summary>
        /// look for the maximum length of the sublists
        /// </summary>
        /// <param name="list">list of lists</param>
        /// <returns>the maximum length</returns>
        public static int Maximum(ArrayList list)
        {
            int max = 0;
            foreach (ICollection sublist in list)
            {
                if (max < sublist.Count)
                {
                    max = sublist.Count;
                }
            }
            return max;
        }
        public static int Sum(IList<int> list)
        {
            int sum = 0;
            foreach (int n in list)
            {
                sum += n;
            }
            return sum;
        }

        public static void CopyArray(byte[] src, byte[] dest, int start)
        {
            int j = 0;
            for (int i = start; i < start + dest.Length; i++)
            {
                dest[j++] = src[i];
            }
        }


        public static int Maximum(IEnumerable<int> list)
        {
            int max = int.MinValue;
            foreach (int num in list)
            {
                if (max < num)
                {
                    max = num;
                }
            }
            return max;
        }

        public static int Minimum(IEnumerable<int> list)
        {
            int min = int.MaxValue;
            foreach (int num in list)
            {
                if (min > num)
                {
                    min = num;
                }
            }
            return min;
        }

        public static uint Minimum(IEnumerable<uint> list)
        {
            uint min = uint.MaxValue;
            foreach (uint num in list)
            {
                if (min > num)
                {
                    min = num;
                }
            }
            return min;
        }

        public static int PosofMaximum(IList<int> list)
        {
            int index = 0;
            int max = list[0];
            for (int i = 1; i < list.Count; i++)
            {
                if (list[i] > max)
                {
                    max = list[i];
                    index = i;
                }
            }
            return index;
        }

        public static int PosofMinimum(IList<int> list)
        {
            int index = 0;
            int min = list[0];
            for (int i = 1; i < list.Count; i++)
            {
                if (list[i] < min)
                {
                    min = list[i];
                    index = i;
                }
            }
            return index;
        }

        public static List<int> PosofMaximums(IList<int> list)
        {
            int max = int.MinValue;
            List<int> maxIndice = new List<int>();
            for (int index = 0; index < list.Count; index++)
            {
                int num = list[index];
                if (num > max)
                {
                    max = num;
                    maxIndice.Clear();
                }
                if (num == max)
                {
                    maxIndice.Add(index);
                }
            }
            return maxIndice;
        }

        public static List<int> PosofMinimums(IList<int> list)
        {
            int min = int.MaxValue;
            List<int> minIndice = new List<int>();
            for (int index = 0; index < list.Count; index++)
            {
                int num = list[index];
                if (num < min)
                {
                    min = num;
                    minIndice.Clear();
                }
                if (num == min)
                {
                    minIndice.Add(index);
                }
            }
            return minIndice;
        }

        public static List<T> Flat<T>(List<List<T>> groups)
        {
            List<T> items = new List<T>();
            foreach (List<T> group in groups)
            {
                items.AddRange(group);
            }
            return items;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect YunCheDa Hangzhou
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions