Module 14: Dynamic Memory Continued Flashcards
hash tables
data structure that employs an array (which may be on the stack or the heap and a hashing function. it may also include linked lists to allow for collisions
multi-file development
refers to spreading your code across multiple files; the intent is to centralize common code that other developers could use
key
portion of your data used to map to bucket
value
bucket number in hash table array (aka index number)
hash function
maps key to value (common variations follow)
associative array
what a hash table actually is i.e., an array whose index is associated with your custom data type
collision
when more than 1 key maps to the same value, the result is a bucket that contains more than one data item. we use linked lists to allow collisions a “perfect” hash table has no collisions (but then it becomes more of a lookup table)
load factor
number of entries in table or the number of buckets. represents a metric that allows you to determine the tradeoff between time to search and space costs
function pointers
pointers that point to functions. the pointer stores the address in program memory of the first instruction in the function
arrays
- need to allocate a fixed size, cannot grow if you need to add more entries
- adding things to the end of an array is easy, if there is space
- inserting or deleting from the middle of an array is painful, since you need to shift things in an array
- finding things in an array is easy
linked lists
- easy to add new elements to the front or back as needed
- inserting or deleting in the middle of the list is not a problem (but is tricky)
- finding things in the list involves “traversing” the list until we find what we need
- this is not hard but it is more time-consuming than addressing the exact element you desire
- once the linked list gets long, finding things inside them becomes very time consuming
- so, search is an issue with linked lists
review of our implementation of a hash table
combination of an array, linked lists, and a hashing function
- gives us the “fast” addressability of an array with the efficiency of a dynamically allocated linked list
- note: an array of linked lists is not a hash table (it lacks the hashing function)
to insert a node into a hash table:
- apply hashing function to the incoming data to determine the bucket it belongs to
- address the bucket
- insert the node into the linked list addressed by the bucket
to find a node in a hash table
- apply the hash function to the data we wish to find to reveal which bucket it’s in
- address the bucket
- search for the node in the linked list addressed by the bucket #
uses of function pointers
can “call” a function inside a function that has been passed as an argument
- also referred to as a “callback” function
- often used in asynchronous programming, event loops, and GUI programming
- -if an “event” occurs (e.g. a certain button is clicked), caller’s function is executed to “handle” the event
offers tremendous flexibility in writing libraries used by other programmers
- programmer uses a library and gives its flexibility by calling user code at runtime
- -library runs another person’s code
- -double-edged sword: lots of power, lots of problems!