Data Structures
Last updated
Last updated
The most fundamental idea of an Array is that it is a contiguous memory space.
This space must be informed in it's allocation, and you cannot grow it, only reallocate it to a new bigger or smaller Array
.
Takes the width of the type
(Bytes) and you multiply by the offset
of the position you want.
The content of the index is overwritten, since you cannot just grow the array size.
Overwrite the content of the index to some specific null
value. (Not necessarily null
)
A linked list is objects that are linked. This means that each object in this list, only knows who is their next
object.
The weakness of a Linked List
in terms of costs, is the traverse cost to get to an element.
You will have to run through the linked list, since there are no indexes, until you find the desired value.
Desconsidering the asymptotic time to find and get to the position. Inserting a new element is just updating the next
property of the existent object, and updating the next
from the new object. (In Doubled Linked List
, there is also the previous
property, and it must update the previous element and the next element, of the inserted position)
Deleting is the same as inserting, you only update next
property from the previous object. (In Doubled Linked List
, you also update the previous
property from the next object)
Can be seen as Arrays
that have a defined head
and tail
property.
You work inserting data in the middle of this Array which we will call Buffer. And you may insert in the head
or in the tail
, meaning these properties will end up creating an abstract Array inside this Buffer.
Once the tail
reach N
size you then tail % N
, and make it ring around to the begining.
When tail = head
then the buffer is full and must grow.
load factor
The amount of data points vs the amount of storage. (data.len
/ storage.capacity
)
key
A value that is hashable and is used to look up data. (The hash has to be consistent)
value
A value that is associated with a key.
collision
When 2 keys map to the same cell.
Because a Map has a limited amount of space, hashing keys will most certainly end up in collisions of hashes.
The collision happens because the produced hash will have to be modularized so that it always points to a valid slot in the Map.
There are several ways of dealing with collisions, for instace:
A way to avoid the linear or exponencial backof when two hashes collide, is to make these colliding hashes to ocupy the same slot in the Map, instead of finding an unocuppied slot.
Meaning that a slot would consist of a Linked List or similar DS.
As the Data Storage become close to full, the number of collisions increase, thus making the Map less efficient, no matter the strategy used for dealing with collisions.
The ideal limit load factor
is about 0.7. Above that value, the Data Storage should grow.
Growing the Data Storage means, re-hashing all the existing { key, value }
.
Least Recently Used is a caching mechanism. It evicts the least recently used item.
In LRU, you mix a Doubled Linked List with a HashMap.
With this mix, you can access and update any cached values in constant time.
Takes , for head
and tail
of the list. (constant time for tail
only IF you have it's reference saved)
Otherwise takes , for middle elements.
Takes , for the head
of the list.
It would also take , for the list tail
IF you have it's reference saved.
Otherwise for middle elements it will take , since you will have to go through the list until you reach the desired position.
Takes , for the head
of the list.
It would also take , for the list tail
IF you have it's reference saved.
Otherwise for middle elements it will take , since you will have to go through the list until you reach the desired position.
The less collision hashes a hash function generates, the closest to the hashmap can stay for its operations.
The bigger the load factor
the less the Map is.
The Doubled Linked List is used to maintain the order for the accessed items. This is crucial to keep the trimCache()
.
The HashMap is used to access Linked List items in .