/////////////////////////////////////////////////////////////////////////////// // // Microsoft Research Singularity // // Copyright (c) Microsoft Corporation. All rights reserved. // // File: DHashtable.sg // // Note: // A hashtable customized for FAT directory hashing. Table entries // are composed of . // // Earlier versions of this file implemented a chain based // Hashtable in place. The code was updated to use open address hashing // as it has lower memory overhead and faster lookup when the load is // is low. // namespace Microsoft.Singularity.Services.Fat.Fs { using System; using System.Diagnostics; /// Specialized hashtable for storing filename /// hashes for directories. internal sealed class DHashtable { public const int MaxKeyValue = OpenHash.MaxKeyValue; public const int MaxKeyBits = OpenHash.MaxKeyBits; OpenHash table; internal DHashtable() { this.table = new OpenHash(); } /// Property Count represents the number of key-value /// pairs stored in the hash table. internal int Count { get { return table.Count; } } /// Property Capacity represents the current capacity /// of the hash table. /// The capacity of the hash table changes dynamically. internal int Capacity { get { return table.Capacity; } } private static int ComposeValue(ushort x, ushort y) { return (((int)x) << 16) | ((int)y); } private static void DecomposeValue(int v, out ushort x, out ushort y) { x = (ushort)(v >> 16); y = (ushort)(v); } /// /// Insert name-key, entry offset, length triplet. /// internal bool Insert(int nameKey, ushort entryOffset, ushort entryLength) requires nameKey <= MaxKeyValue; { return table.Insert(nameKey, ComposeValue(entryOffset, entryLength)); } /// Remove key-value pair. /// true on success, false if key-value pair /// is not present. internal bool Remove(int nameKey, ushort entryOffset, ushort entryLength) requires nameKey <= MaxKeyValue; { return table.Remove(nameKey, ComposeValue(entryOffset, entryLength)); } /// /// Prepare to begin a search. This methods clears out /// the state associated with any previous searches. /// internal void ResetSearch() { table.BeginSearch(); } /// Performs value lookup after GetFirstValue /// has retrieved first value for key. This methods /// allows callers to iterate over hash items with the /// same key. /// The /// hash table was modified or the key does not match /// the key in the last GetFirstValue call. /// internal bool Search(int nameKey, out ushort entryOffset, out ushort entryLength) requires nameKey <= MaxKeyValue; { int value; bool success = table.Search(nameKey, out value); DecomposeValue(value, out entryOffset, out entryLength); return success; } } }