Esempio n. 1
0
        /// <summary>
        /// Initializes hashtable with given equality comparer of T and capacity equal to smallest prime number bigger than provided value.
        /// </summary>
        public HashTable(int capacity, EqualityComparer <T> equalityComparer)
        {
            if (equalityComparer == null)
            {
                throw new ArgumentNullException("Comparer is null");
            }
            this.equalityComparer = equalityComparer;
            if (capacity < 0)
            {
                throw new ArgumentException("Capacity can't be negative");
            }

            // set buckets capacity to next smallest prime
            if (capacity < 17)
            {
                capacity = 17;
            }
            else
            {
                ulong cap = NumericAlgorithms.FindNextPrime((ulong)capacity);
                capacity = cap > int.MaxValue ? int.MaxValue : (int)cap;        //int.MaxValue is prime
            }
            bucketsCapacity = capacity;
            buckets         = new LinkedList <T> [bucketsCapacity];
        }
Esempio n. 2
0
        public void reverse_test_5()
        {
            int input = 0;
            NumericAlgorithms algos = new NumericAlgorithms();
            int actual   = algos.Reverse(input);
            int expected = 0;

            Assert.AreEqual(expected, actual);
        }
Esempio n. 3
0
 private void primeTestBtn_Click(object sender, RoutedEventArgs e)
 {
     try {
         resultPrimeTextBlock.Text = NumericAlgorithms.IsPrime(ulong.Parse(primeTxtBox.Text)).ToString();
     }
     catch (Exception ex) {
         MessageBox.Show(ex.Message);
     }
 }
Esempio n. 4
0
        public void reverse_test_4()
        {
            int input = 670045235;
            NumericAlgorithms algos = new NumericAlgorithms();
            int actual   = algos.Reverse(input);
            int expected = 532540076;

            Assert.AreEqual(expected, actual);
        }
Esempio n. 5
0
 private void NRootButton_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         resultExpTxtBlock.Text = NumericAlgorithms.NRoot(double.Parse(aExpTxtBox.Text), double.Parse(bExpTxtBox.Text)).ToString();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Esempio n. 6
0
 private void ExpButton_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         resultExpTxtBlock.Text = NumericAlgorithms.ExponentBySquaring(decimal.Parse(aExpTxtBox.Text), uint.Parse(bExpTxtBox.Text)).ToString();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Esempio n. 7
0
 private void LCMByGCDButton_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         resultGCDTxtBlock.Text = NumericAlgorithms.LeastCommonMultipleByGCD(uint.Parse(aTxtBox.Text), uint.Parse(bTxtBox.Text)).ToString();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Esempio n. 8
0
 private void ConvertBtn_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         convertResultTxtBlock.Text = NumericAlgorithms.BaseConverter(numberTxtBox.Text, ushort.Parse(fromBaseTxtBox.Text), ushort.Parse(toBaseTxtBox.Text));
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Esempio n. 9
0
        private void NSumOfPower2Button_Click(object sender, RoutedEventArgs e)
        {
            uint i = 0;

            if (!uint.TryParse(NSumofPower2TxtBox.Text, out i))
            {
                MessageBox.Show("Choose positive integer and try again");
                return;
            }
            var numbers = NumericAlgorithms.SumOfNumbersOfPower2(i);

            NSumOfPower2TxtBlock.Text = numbers.ToSimbolSeperatedString(" + ");
        }
Esempio n. 10
0
        private void findPrimesBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                primeNumberstxtBox.Text = "";
                long   n      = long.Parse(primeFactorsTxtBox.Text);
                long[] primes = NumericAlgorithms.FindPrimes(n);

                primeNumberstxtBox.Text      = Utility.ToSimbolSeperatedString(primes);
                resultPrimeFactorsBlock.Text = primes.LongLength.ToString() + " primes, approximate number N / lnN =" + (n / (long)Math.Log(n)).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Esempio n. 11
0
 private void primeFactorsBtn_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         var list = NumericAlgorithms.PrimeFactors(ulong.Parse(primeFactorsTxtBox.Text));
         if (list.Count == 0)
         {
             MessageBox.Show("The number does not have factors");
             return;
         }
         resultPrimeFactorsBlock.Text = Utility.ToSimbolSeperatedString(list);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Esempio n. 12
0
        /// <summary>
        /// Initializes hashtable with given equality comparer and capacity equal to smallest prime number bigger than collection.count
        /// </summary>
        public HashTable(IEnumerable <T> collection, EqualityComparer <T> equalityComparer)
        {
            if (equalityComparer == null)
            {
                throw new ArgumentNullException("Comparer is null");
            }
            this.equalityComparer = equalityComparer;

            // set buckets capacity to next smallest prime
            if (collection == null)
            {
                throw new ArgumentNullException("Collection is null");
            }
            if (collection is IList <T> )
            {
                bucketsCapacity = (collection as IList <T>).Count;
            }
            else
            {
                int cap = 0;
                foreach (var item in collection)
                {
                    cap++;
                }
                bucketsCapacity = cap;
            }
            if (bucketsCapacity < 17)
            {
                bucketsCapacity = 17;
            }
            else
            {
                ulong cap = NumericAlgorithms.FindNextPrime((ulong)bucketsCapacity);
                bucketsCapacity = cap > int.MaxValue ? int.MaxValue : (int)cap;        //int.MaxValue is prime
            }
            buckets = new LinkedList <T> [bucketsCapacity];

            // add collection elements to hashtable
            foreach (var item in collection)
            {
                this.Add(item);
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Adds new item to hashtable. Might cause a reallocation.
        /// </summary>
        public void Add(T item)
        {
            int hashCode    = equalityComparer.GetHashCode(item);
            int bucketIndex = hashCode % bucketsCapacity;

            if (buckets[bucketIndex] == null)
            {
                buckets[bucketIndex] = new LinkedList <T>();
                bucketsCount++;
            }
            buckets[bucketIndex].Add(item);
            elementsCount++;

            //-------------------------- reallocate the buckets collection if required
            // get new bucket capacity if required
            if (LoadFactor <= MaxLoadFactor)
            {
                return;
            }
            ulong newBucketsCapacity = 17;

            if (bucketsCapacity < int.MaxValue / 2)
            {
                newBucketsCapacity = (ulong)bucketsCapacity * 2;
                newBucketsCapacity = NumericAlgorithms.FindNextPrime((ulong)newBucketsCapacity);
            }
            else
            {
                newBucketsCapacity = int.MaxValue;  //its prime
            }
            // for each value in hash table get new index in new reallocated array, copy that value and count new buckets
            var newBuckets      = new LinkedList <T> [newBucketsCapacity];
            int newBucketsCount = 0;

            for (int i = 0; i < buckets.Length; i++)
            {
                if (buckets[i] != null)
                {
                    var list = buckets[i];
                    if (list.Count != 0)
                    {
                        foreach (T value in list)
                        {
                            hashCode = equalityComparer.GetHashCode(value);
                            int newBucketIndex = hashCode % (int)newBucketsCapacity;
                            if (newBuckets[newBucketIndex] == null)
                            {
                                newBuckets[newBucketIndex] = new LinkedList <T>();
                                newBucketsCount++;
                            }
                            newBuckets[newBucketIndex].Add(value);
                        }
                    }
                }
            }

            // copy new values
            buckets         = newBuckets;
            bucketsCapacity = (int)newBucketsCapacity;
            bucketsCount    = newBucketsCount;
        }