WinCopies.Util: A World of Extensions
Part 1: The ArrayBuilder class
To build an array requires that the values that are stored in memory follow the previous ones. And, once built, an array is immutable, that is, it has to be re-created each time we want to add values in it. This idea also applies for lists and collections, as both wrap an inner array, which has to be re-created when the item references or values are changed.
So, what code can we use to build an array when we don't know how many values we'll have to put in memory when we initialize it?
Let's consider a String concatenation. A String is simply an array of Chars, but that provides String-specific methods. Now, imagine we have to retrieve a set of Strings from a database. We could send a request to the DB in order to get the number of items that match to a given pattern, then send an other request in order to get the items themselves. But this method is a very bad idea, because, in a real DB, each of these requests can take a lot of time. But, as we said, creating a new array each time we get an item from the DB is not very efficient either. And, creating a new array is precisely what the concatenation operator does. So, if we get 1000 items that we concatenate in a while loop, we'll get 999 concatenations, that is, 999 array creation in memory for this loop.
To avoid that, the .Net framework provides the StringBuilder class. Like the String type, the StringBuilder class is specialized in character manipulation, and can be considered as a 'string of Strings'.
The solutionSo, what does the StringBuilder do? Actually, like lists and collections, a StringBuilder is a wrapper, but not a wrapper of an array, it can be considered as a wrapper of LinkedList. A LinkedList is simply a type that provides some methods to handle its content and a reference to its first or last, or both, items. A LinkedList that contains a reference to its first item is called a Queue and to its last item is called a Stack. For the first one, it is said that the LinkedList uses the FIFO (First In, First Out) method, and for the second, the LIFO (Last In, First Out) method. In addition, the linked list items also provide access to the next (FIFO list) or to the previous (LIFO) item. If the list is designed to be browsable in the two directions, then each item will contains a reference to the next and to the previous item.
The linked lists have a big advantage: values don't have to follow each other in memory. But they also have a big disadvantage, due to the way they store the values in memory: they aren't indexable, as an array index is just a pointer that is incremented by the index.
So, like the .Net List and Collection are wrappers for arrays, the WinCopies.Util's ArrayBuilder is a wrapper for a .Net LinkedList, that provides methods to create arrays based on the items in the list. Using it consists to add a value to it and to increment a counter for each value that is added. Then, when the loop has completed, it creates an array directly with the required length.