Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/t-h2o/containers
The standard C++ containers have all a specific usage. To make sure you understand them, let’s re-implement them!
https://github.com/t-h2o/containers
42 containers cpp cpp98 stl-containers
Last synced: about 1 month ago
JSON representation
The standard C++ containers have all a specific usage. To make sure you understand them, let’s re-implement them!
- Host: GitHub
- URL: https://github.com/t-h2o/containers
- Owner: t-h2o
- Created: 2022-12-14T19:03:56.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-03-03T12:25:49.000Z (over 1 year ago)
- Last Synced: 2024-09-28T23:40:56.107Z (about 2 months ago)
- Topics: 42, containers, cpp, cpp98, stl-containers
- Language: HTML
- Homepage: https://t-h2o.github.io/containers
- Size: 466 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.adoc
Awesome Lists containing this project
README
= Containers
:nofooter:
:toc: left
:sectnums:
:stylesheet: assets/my-stylesheet.css
:stem:GitHub: https://github.com/t-h2o/containers[t-h2o/containers]
== Subject
42: https://cdn.intra.42.fr/pdf/pdf/60315/en.subject.pdf[subject]
== Google test
In this project, I want inclure https://en.wikipedia.org/wiki/Unit_testing[unit testing] with the https://google.github.io/googletest/[Google test] framework.
== Vector
image::assets/vector.svg[example]
=== Resources
.cplusplus.com
* https://cplusplus.com/reference/vector/vector[`std::vector`]
* https://cplusplus.com/reference/memory/allocator/[`std::allocator`].The STL: on GNU/Linux
* `/usr/include/c++/12.2.0/bits/stl_vector.h`=== Check list
.Member functions
* [x] https://cplusplus.com/reference/vector/vector/vector/[`constructor`]
** [x] default
** [x] fill
** [x] range
** [x] copy
* [x] https://cplusplus.com/reference/vector/vector/~vector/[`destructor`]
* [x] https://cplusplus.com/reference/vector/vector/operator=/[`operator=`].Iterators
* [x] https://cplusplus.com/reference/vector/vector/begin/[`begin`]
* [x] https://cplusplus.com/reference/vector/vector/end/[`end`]
* [ ] https://cplusplus.com/reference/vector/vector/rbegin/[`rbegin`]
* [ ] https://cplusplus.com/reference/vector/vector/rend/[`rend`]
* [ ] https://cplusplus.com/reference/vector/vector/cbegin/[`cbegin`]
* [ ] https://cplusplus.com/reference/vector/vector/cend/[`cend`]
* [ ] https://cplusplus.com/reference/vector/vector/crbegin/[`crbegin`]
* [ ] https://cplusplus.com/reference/vector/vector/crend/[`crend`].Capacity
* [x] https://cplusplus.com/reference/vector/vector/size/[`size`]
* [x] https://cplusplus.com/reference/vector/vector/max_size/[`max_size`]
* [x] https://cplusplus.com/reference/vector/vector/resize/[`resize`]
* [x] https://cplusplus.com/reference/vector/vector/capacity/[`capacity`]
* [x] https://cplusplus.com/reference/vector/vector/empty/[`empty`]
* [x] https://cplusplus.com/reference/vector/vector/reserve/[`reserve`]
* [x] https://cplusplus.com/reference/vector/vector/shrink_to_fit/[`shrink_to_fit`].Element access
* [x] `operator[]`
* [x] https://cplusplus.com/reference/vector/vector/at/[`at`]
* [x] https://cplusplus.com/reference/vector/vector/front/[`front`]
* [x] https://cplusplus.com/reference/vector/vector/back/[`back`]
* [x] https://cplusplus.com/reference/vector/vector/data/[`data`].Modifiers
* [x] https://cplusplus.com/reference/vector/vector/assign/[`assign`]
** [x] range
** [x] fill
* [x] https://cplusplus.com/reference/vector/vector/push_back/[`push_back`]
* [x] https://cplusplus.com/reference/vector/vector/pop_back/[`pop_back`]
* [x] https://cplusplus.com/reference/vector/vector/insert/[`insert`]
** [x] single element
** [x] fill
** [x] range
* [x] https://cplusplus.com/reference/vector/vector/erase/[`erase`]
* [x] https://cplusplus.com/reference/vector/vector/swap/[`swap`]
* [x] https://cplusplus.com/reference/vector/vector/clear/[`clear`]
* [ ] https://cplusplus.com/reference/vector/vector/emplace/[`emplace`]
* [ ] https://cplusplus.com/reference/vector/vector/emplace_back/[`emplace_back`].Allocator
* [ ] https://cplusplus.com/reference/vector/vector/get_allocator/[`get_allocator`]=== Reallocation
[blockquote, cplusplus.com]
____
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).
____[stem]
++++
z(x, y) = x * 2 ^ y
++++image::assets/gnuplot.svg[gnuplot]
== Iterator
.map and vector
|===
|Iterator | Map | vector|`iterator`
|can change pair.second but not pair.first
|can change the value|`const_iterator`
|cannot change any things
|cannot change the value|===
.iterator
|===
|Iterator | set with `begin()` | set with `rbegin()`|`iterator`
| yes
| no|`const_iterator`
| yes
| no|`reverse_iterator`
| yes
| yes|`const_reverse_iterator`
| yes
| yes|===
=== Resources
.cplusplus.com
* https://cplusplus.com/reference/iterator/[``]
* https://cplusplus.com/reference/iterator/iterator/[`std::iterator`]
* https://cplusplus.com/reference/iterator/iterator_traits/[`std::iterator_traits`]== Pair
=== Resources
.cplusplus.com
* https://cplusplus.com/reference/utility/pair/[`std::pair`].The STL: on GNU/Linux
* `/usr/include/c++/12.2.0/bits/stl_pair.h`=== Check-list
.Member functions
* [x] https://cplusplus.com/reference/utility/pair/pair/[`constructor`]
** [x] default
** [x] copy
** [x] initialization
* [ ] https://cplusplus.com/reference/utility/pair/operator=/[`operator=`]
* [ ] https://cplusplus.com/reference/utility/pair/swap/[`swap`]== Map
image::assets/map_usecase.svg[map usecase]
=== Resources
.cplusplus.com
* https://cplusplus.com/reference/map/map/[`std::map`].wikipedia
* https://en.wikipedia.org/wiki/Binary_search_tree[Binary search tree]
** 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
* https://en.wikipedia.org/wiki/Red%E2%80%93black_tree[Red–black tree]
** 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
* https://en.wikipedia.org/wiki/Tree_rotation[Tree rotation].YouTube @MisterCode
* https://youtu.be/AN0axYeLue0[Red-Black Trees - Data Structures]
* https://youtu.be/JwgeECkckRo[Insertion for Red-Black Trees ( incl. Examples )]
* https://youtu.be/_c30ot0Kcis[Deletion for Red-Black Trees ( incl. Examples )].YouTube @alenachang8071
* https://youtu.be/eoQpRtMpA9I[Red-black tree deletion: steps + 10 examples].other
* https://www.cs.usfca.edu/~galles/visualization/RedBlack.html[red black tree visualization]=== Check-list
.Member functions
* [ ] https://cplusplus.com/reference/map/map/map/[`constructor`]
* [ ] https://cplusplus.com/reference/map/map/~map/[`destructor`]
* [ ] https://cplusplus.com/reference/map/map/operator=/[`operator=`].Iterators:
* [ ] https://cplusplus.com/reference/map/map/begin[`begin`]
* [ ] https://cplusplus.com/reference/map/map/end[`end`]
* [ ] https://cplusplus.com/reference/map/map/rbegin[`rbegin`]
* [ ] https://cplusplus.com/reference/map/map/rend[`rend`]
* [ ] https://cplusplus.com/reference/map/map/cbegin[`cbegin`]
* [ ] https://cplusplus.com/reference/map/map/cend[`cend`]
* [ ] https://cplusplus.com/reference/map/map/crbegin[`crbegin`]
* [ ] https://cplusplus.com/reference/map/map/crend[`crend`].Capacity:
* [x] https://cplusplus.com/reference/map/map/empty[`empty`]
* [x] https://cplusplus.com/reference/map/map/size[`size`]
* [ ] https://cplusplus.com/reference/map/map/max_size[`max_size`].Element access:
* [ ] `operator[]`
* [ ] https://cplusplus.com/reference/map/map/at[`at`].Modifiers:
* [ ] https://cplusplus.com/reference/map/map/insert[`insert`]
* [ ] https://cplusplus.com/reference/map/map/erase[`erase`]
* [ ] https://cplusplus.com/reference/map/map/swap[`swap`]
* [ ] https://cplusplus.com/reference/map/map/clear[`clear`]
* [ ] https://cplusplus.com/reference/map/map/emplace[`emplace`]
* [ ] https://cplusplus.com/reference/map/map/emplace_hint[`emplace_hint`].Observers:
* [ ] https://cplusplus.com/reference/map/map/key_comp[`key_comp`]
* [ ] https://cplusplus.com/reference/map/map/value_comp[`value_comp`].Operations:
* [ ] https://cplusplus.com/reference/map/map/find[`find`]
* [ ] https://cplusplus.com/reference/map/map/count[`count`]
* [ ] https://cplusplus.com/reference/map/map/lower_bound[`lower_bound`]
* [ ] https://cplusplus.com/reference/map/map/upper_bound[`upper_bound`]
* [ ] https://cplusplus.com/reference/map/map/equal_range[`equal_range`].Allocator:
* [ ] https://cplusplus.com/reference/map/map/get_allocator[`get_allocator`]=== Red black tree
==== Deletion
image::assets/rbt_delete.svg[redblack delete]
==== resolve double black
image::assets/resolve_double_black.svg[redblack delete]
==== Rules
* The root is always black
* Not two consecutives node can be redFor a node:
image::assets/node.svg[redblack]
Here an example of a red black tree
. Case 0: Init, the first node is black
+
image::assets/rbt00.svg[redblack]. Insert: Add a second node
+
The key (5) is smaller than the node key (15), so we set it into the left
+
image::assets/rbt10.svg[redblack]
+
We recolor the new node (5) to red
+
image::assets/rbt11.svg[redblack]. 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.
+
image::assets/rbt20.svg[redblack]
+
The uncle of the new node is NULL
+
image::assets/rbt21.svg[redblack]
+
We rotate
+
image::assets/rbt22.svg[redblack]
+
We recolor
+
image::assets/rbt23.svg[redblack]