2

I basically want to keep a track of old filenames and each file's new filename.

I could use a Dictionary I suppose, but I want to keep it really lightweight.

Would a multidimensional string array work just as well?

2
  • 1
    What's so heavyweight about a Dictionary? Commented Nov 25, 2010 at 9:18
  • List<string> would be more appropriate since List<string> has an order, but elements in an Dictionary<string, string> are unordered. With List<string> you will know that the last element is the current name. Commented Nov 25, 2010 at 9:23

4 Answers 4

9

Use the Dictionary<string,string> class:

  1. It manages its own size for you
  2. It uses hashes to speed up searching for elements
  3. You can just go fileNames[oldName] to get the name instead of using a loop or LINQ

Really, the Dictionary is the lightweight solution here.

Sign up to request clarification or add additional context in comments.

1 Comment

I've run into trouble with Dictionary<,> and the large object heap, since Dictionary<,> maintains two arrays for the objects and their keys. If you have a very large number of files, you may prefer the SortedDictionary<,>. This class is implemented with a binary tree instead of a hash table. Or, if the number of files is stable, you should specify the dictionary's capacity when you create it.
3

The problem with using Dictionary<string,string> in your scenario is that all keys need to be unique, so if you are using 'old file name' as a key, then you cannot have 2 scenarios where the 'old file name' was the same.

The best solution (in my opinion) is

List<Tuple<string,string>>

which will also be ordered with the lastest added last etc.

a new entry would look like

List<Tuple<string,string>> list = new List<Tuple<string, string>>();

list.Add(Tuple.Create(oldfilename,newfilename));

and then to find all new files for a particular old file name you could do the following:

var files = list.Where(t => t.Item1 == oldfilename);

Comments

2

You could also consider using StringDictionary, but its kinda old!

It was used in the land before generics, when dinosaurs ruled the earth.

Comments

1

What's your definition of lightweight? Are you worried about memory use or runtime?

With a multidimensional string array, you'd have to search through the array yourself (you could sort the array and do a binary search, but that's still not as nice as a hash table in the general case), so you would lose out on runtime cost on access.

With a multidimensional string array, you also need to know in advance how many entries you're going to need, to allocate memory. If you don't, you lose time and churn memory reallocating ever-larger arrays. A Dictionary doesn't use contiguous regions of memory, so it doesn't reallocate on expansion.

Finally, on the memory front, keep in mind that a string is a reference. The difference in memory use you'll see will only be related to the lookup structure, which is probably small compared to the data you want to keep track of. If the concern is to keep contiguous blocks of memory for cache efficiency, here neither solution is better than the other, as the strings are stored outside of the datastructure (being references), so any string reads lose that contiguity.

So to conclude, there's really no reason to use a multidimensional array over a dictionary.

2 Comments

Actually, a Dictionary does indeed use contiguous regions of memory. It has two arrays (one of ints and one of data nodes). These are maintained at a size at least as great as the item count. On expansion, if the dictionary is at capacity, the dictionary allocates two new arrays, more than twice as large as the old. It copies the data to the new arrays and replaces the old ones with them. The SortedDictionary, however, is a binary tree, so it does not use large contiguous regions of memory or reallocate on expansion. I still agree with your conclusion, however :)
Thanks phoog. I assumed the internal hashtable was implemented as a mostly-fixed array of chained bucket lists given this is the most popular approach, but reading up on the current implementation I'm somewhat disappointed in its naïveness.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.