Data Structures
Arrays
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
.
Getting at specific index
Takes the width of the type
(Bytes) and you multiply by the offset
of the position you want.
This take , since you know the type width
and the offset
, you just multiply them.
Inserting at specific index (Not really inserting)
The content of the index is overwritten, since you cannot just grow the array size.
Also , since accessing at specific position is constant.
Deleting at specific index (Not really deleting)
Overwrite the content of the index to some specific null
value. (Not necessarily null
)
Also , since it is basically the same as inserting.
Linked List
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.
There are also Doubled Linked Lists
, where each object knows the previous
and next
object.
Getting specific value or at specific position
You will have to run through the linked list, since there are no indexes, until you find the desired value.
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.
Inserting at specific position
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)
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.
Deleting at specific 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)
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.
Implementation
Ring Buffer
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.
They are good if you need a DS that maintains order, but have data being flushed periodicaly, and want your memory allocation to be stable and not spiking from time to time.
Like a data structure to maintin logs.
HashMap
Terminology
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.
Hashing
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.
The less collision hashes a hash function generates, the closest to the hashmap can stay for its operations.
There are several ways of dealing with collisions, for instace:
Backof
Avoiding backof
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.
Growing Maps Data Storage
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 bigger the load factor
the less the Map is.
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 }
.
Implementation
LRU
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.
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 .
With this mix, you can access and update any cached values in constant time.
Implementation
Last updated