Tonight I want to cover Vala’s collections library “libgee”. Like just about any non-trivial code I make good use of this to parse Odysseus’s templates, and sometimes to underly it’s data model.
It’s pretty much CompSci 102, so excuse me if this sounds a bit basic. It isn’t to others.
To start, I’ll discuss Gee.HashMap (implements Gee.Map) which I use to lookup filters/tags to parse and to cache parsed templates. It allows you to lookup a value files under a given key.
It does this by computing an index into an array for the given key and checking if the expected is stored there.
There’s multiple ways to handle things if it’s not, but Gee follows a “next” pointer and tries until it reaches of the “linked list”.
One thing that is trickier about implementing collections over other generic-supporting languages is that it can’t expect the built in types to have the methods it needs.
So Gee has to hardcode “hashing”, “equality”, etc methods for those types itself.
For Gee.HashMap, the remainder operator is applied to the result of the “hash” functions in order to bring it into range of the hashmap’s underlying array.
Another collection I use significantly is Gee.ArrayList (implements Gee.List, like linked lists), but mostly I just use them to build the arrays I use at runtime.
An array is a contiguous chunk of (virtual) memory in which we can store a sequence of items. Gee.ArrayList which tracks the number of items separately and resizes the array when needed.
When I’m done I have it copy those items to an array of exactly the right size, now that I know exactly what that is.