Word Counter Implementation
Is there a better way than the following brute foce implementation of a c# word counting class?
UPDATED CODE: Sorry!
/// <summary>
/// A word counting class.
/// </summary>
public class WordCounter
{
Dictionary<string, int> dictTest = new Dictionary<string, int> ();
/// <summary>
/// Enters a word and returns the current number of times that word was found.
/// </summary>
/// <param name="word">The word or string found.</param>
/// <returns>Count of times Found() was called with provided word.</returns>
public int Found ( string word )
{
int count = 1;
return dictTest.TryGetValue ( wo开发者_如何学Pythonrd, out count ) ? ++dictTest[word] : dictTest[word] = 1;
}
}
In response to matt, Dictionary is basically a HashTable with generics, so the lookup is constant time (well, not exactly but pretty much).
You can build a Tree and then search will take constant time in the length of the string, you are searching for. A Tree is more space efficient in this case, than using a hash.
well, If you had like LOTS of memories, you could store all the letters individually in a tree structure.
so like, you have an array of 26 objects, first letter is the index into that array, the array is an array of pointers to more arrays of 26 objects (but only if that letter has been encountered of course. so on and so forth, the second letter is the index into the second level of arrays...
does Dictionary use a Binary search pattern? also, does it search by string? or does it hash the strings down, if not, hashing the strings down to ints might improve performance. also theoretically, if you did it manually, there wouldn't be any overhead in keeping the list "sorted" because the initial binary search would give up at roughly the position where it should be inserted in the list if it doesn't exist?
精彩评论