GitHub: t-h2o/containers
1. Subject
42: subject
2. Google test
In this project, I want inclure unit testing with the Google test framework.
3. Vector
3.1. Resources
-
/usr/include/c++/12.2.0/bits/stl_vector.h
3.2. Check list
-
-
✓ default
-
✓ fill
-
✓ range
-
✓ copy
-
3.3. Reallocation
Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.
Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).
4. Iterator
Iterator | Map | vector |
---|---|---|
|
can change pair.second but not pair.first |
can change the value |
|
cannot change any things |
cannot change the value |
Iterator | set with begin() |
set with rbegin() |
---|---|---|
|
yes |
no |
|
yes |
no |
|
yes |
yes |
|
yes |
yes |
4.1. Resources
5. Pair
5.1. Resources
-
/usr/include/c++/12.2.0/bits/stl_pair.h
5.2. Check-list
-
-
✓ default
-
✓ copy
-
✓ initialization
-
-
❏
swap
6. Map
6.1. Resources
-
-
Nodes can have 2 subtrees
-
Items to the left of a given node are smaller
-
Items to the right of a given node are larger
-
-
-
A node is either red or black
-
The root adn leaves (NULL) are balck
-
If a node is red, then its children are black
-
All path from a node to its NULL descendants contain the same number of black nodes
-
6.2. Check-list
-
❏
operator[]
-
❏
at
-
❏
key_comp
6.3. Red black tree
6.3.1. Deletion
6.3.2. resolve double black
6.3.3. Rules
-
The root is always black
-
Not two consecutives node can be red
For a node:
Here an example of a red black tree
-
Case 0: Init, the first node is black
-
Insert: Add a second node
The key (5) is smaller than the node key (15), so we set it into the left
We recolor the new node (5) to red
-
Add a third node
The key (1) is smaller than the node key (15): go to the left. The key (1) is smaller than (5), so we set it into the left.
The uncle of the new node is NULL
We rotate
We recolor