Пример #1
0
        /**
         * Collect bucket statistics
         *
         * @returns {string[]} Array of strings that can be written to a file
         * @api public
         */
        public string[] CollectStatistics()
        {
            List<string> lines = new List<string>();
            List<int> chain = new List<int>();
            Dictionary<int, int> chain_count = new Dictionary<int, int>();

            float chain_avg = 0F;
            float total_length_avg = 0F;
            float total_length = 0F;
            float non_zero_length_buckets = 0F;

            for (int i = 20; i < MAX_BUCKETS; ++i)
            {
                // if overflow is set
                if (this.table[i].overflow != -1)
                {
                    // if overflow is greater than table size, do some weird math stuff.
                    // this accounts for overflows of overflows.
                    // else add to the list normally.
                    if (this.table[i].overflow + 1 >= PRIMARY_TABLE_SIZE)
                    {
                        chain.Add((this.table[i].overflow + 1) - (PRIMARY_TABLE_SIZE - 1));
                    }
                    else
                    {
                        chain.Add(this.table[i].overflow + 1);
                    }
                }
                else
                {
                    //if no overflow is set, just add 0 (not -1)
                    chain.Add(0);
                }
                // if the chain_count dicitionary contains this key and overflow is also set
                if (!chain_count.ContainsKey(i) && this.table[i].overflow != -1)
                {
                    // ternary statement. this math accounts for overflow of overflows.
                    int index = (this.table[i].overflow + 1 >= PRIMARY_TABLE_SIZE) ?
                        (this.table[i].overflow + 1) - (PRIMARY_TABLE_SIZE - 1) : this.table[i].overflow + 1;

                    // counts number of instance index appears in the dictionary
                    chain_count[index] = chain.Count(v => v == index);

                    // add to the collission chain average
                    chain_avg += chain_count[index];
                }
            }

            // calculate the collission chain avrage
            chain_avg /= chain_count.Count();

            for (int i = 0; i < PRIMARY_TABLE_SIZE; ++i)
            {
                // grab the total length of each bucket
                total_length += this.table[i].count;

                // write to the list, which then will be returned and can be used to write to a file.
                lines.Add("Bucket " + (i + 1) + ":");
                lines.Add("\tTotal Length: " + this.table[i].count);
                lines.Add("\n");

                // if count is greater than 1,
                // +1 to the number of buckets which have a more than one slot filled
                // the requirements state not to cound zero length buckets in the average caluclations
                if (this.table[i].count != 0)
                {
                    ++non_zero_length_buckets;
                }
            }

            // calculate the average total length of all primary buckets
            total_length_avg = total_length / non_zero_length_buckets;

            lines.Add("Total Length Average is " + total_length_avg);
            lines.Add("Collission Chain Average is " + chain_avg);

            return lines.ToArray();
        }