public HashTable() { list = new List[hashConstant]; for (int i = 0; i < hashConstant; ++i) { list[i] = new List(); } }
/// <summary> /// Creating of a hash table /// </summary> public HashTable() { list = new List[n]; for (int i = 0; i < n; ++i) { list[i] = new List(); } }
public HashTable(IHash hash) { this.hash = hash; list = new List[hashConstant]; for (int i = 0; i < hashConstant; ++i) { list[i] = new List(); } }
// Default constructor public Table() { this.table = new List<Bucket>(); // Allocates MaxBucket amount of buckets for each hash table. for (int i = 0; i < MAX_BUCKETS; ++i) { this.table.Add(new Bucket()); } }
// Default constructor public Bucket() { this.count = 0; this.overflow = -1; this.slots = new List<Dictionary<string, Slot>>(); // Each Bucket contains 3 slot objects, represented here by a list. for (int i = 0; i < MAX_SLOTS; ++i) { this.slots.Add(new Dictionary<string, Slot>()); } }
public ICollection<PersonInfo> Find(string name) { var matches = this.byName.Keys.Where(x => Regex.IsMatch(x, name, RegexOptions.IgnoreCase)).ToList(); var result = new List<PersonInfo>(); foreach (var key in matches) { foreach (var item in this.byName[key]) { result.Add(item); } } return result; }
public static void Main(string[] args) { Console.WriteLine ("Enter some text"); var input = Console.ReadLine (); var charsDictionary = new HashTable<string, int>(100); var charStrings = new List<string>(input.Length); foreach(var aChar in input) { charStrings.Add(aChar.ToString()); } foreach (var charString in charStrings) { if (charsDictionary.ContainsKey (charString)) { charsDictionary [charString]++; } else { charsDictionary [charString] = 1; } } foreach (var charString in charsDictionary) { Console.WriteLine ("{0}: {1} time/s", charString.Key, charString.Value); } }
/** * 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(); }
/** * Searches and retrieves matches using the values from the param * * @param {string[]} Array of items to search for * @returns {string[]} Results of the search in an array, which can be used to write to a file * @api public */ public string[] Search(string[] searches) { bool found; int bucket_no; int slot_no; List<String> lines = new List<String>(); lines.Add(String.Format("{0,45}", "Search and Retrieval Report")); lines.Add(String.Format("{0,38}", "Transactions")); lines.Add(String.Format("{0,37}", "Search Key\t\tBucket/Slot\t\tRecord")); foreach (string keys in searches) { // hashed key string key = this.Hash(keys.Substring(0, 10)); found = false; bucket_no = 0; foreach (Bucket item in this.table) { slot_no = 0; foreach(Dictionary<string, Slot> dict in item.slots) { foreach (KeyValuePair<string, Slot> kv in item.slots[slot_no]) { // if the two un-hashed keys are equal if (item.slots[slot_no][kv.Key].key == keys.Substring(0, 10)) { // found found = true; lines.Add(String.Format("{0,16}{1,16}/{2}\t\t{3}", item.slots[slot_no][kv.Key].key, bucket_no + 1, slot_no + 1, item.slots[slot_no][kv.Key].value )); } } // manual iteration ++slot_no; } // manual iteration ++bucket_no; } // if nothing was found if (!found) { lines.Add(String.Format("{0,16}{1,16}/{2,0}\t\t{3,0}", keys.Substring(0, 10), "N", "A", "Record not found" )); } } return lines.ToArray(); }
/** * Generates a report for of the Hash Table contents * * @returns {string[]} A string array which can be written to a file. * @api public */ public string[] GenerateReport() { List<String> lines = new List<String>(); int table_no = 0; int slot_no; lines.Add(String.Format("{0, 37}", "Hash Table")); lines.Add(String.Format("{0, 41}", "Verification Report")); lines.Add(String.Format("{0, 39}", "Before Report")); foreach (Bucket item in this.table) { lines.Add(String.Format("Bucket {0}", table_no + 1)); slot_no = 0; foreach (Dictionary<string, Slot> dict in item.slots) { foreach (KeyValuePair<string, Slot> kv in item.slots[slot_no]) { // if there is a key in the slot if (item.slots[slot_no][kv.Key].key != null) { lines.Add(String.Format("\tSlot {0}: {1}{2}", slot_no + 1, item.slots[slot_no][kv.Key].key, item.slots[slot_no][kv.Key].value )); } } // there must be no key if (item.slots[slot_no].Count == 0) { lines.Add(String.Format("\tSlot {0}: None", slot_no + 1)); } // manual iteration ++slot_no; } // if an overflow pointer is set if (this.table[table_no].overflow != -1) { lines.Add(String.Format("\tOverflow Pointer: {0}", this.table[table_no].overflow + 1)); } else { lines.Add(String.Format("\tOveflow Pointer: {0}", "None")); } lines.Add(String.Format("{0}", "\n")); // manual iteration ++table_no; } return lines.ToArray(); }