Skip to content

Latest commit

 

History

History
69 lines (54 loc) · 4.16 KB

README.md

File metadata and controls

69 lines (54 loc) · 4.16 KB

ft_containers

42/Codam deep dive into standard C++ containers.

Description

Reimplementing STL containers: vector, stack, map, and set.
This includes all member and non-member functions and overloads, and also iterator systems.
Inner data structures should be appropriate to the container.
I therefore also reimplemented a Red-Black tree class (complete with iterator and nodes) for use with map and set, as is done by the STL.

Red-Black Tree

I followed the implementation of Red-Black Trees as described in "Introduction to Algorithms" (3rd ed.) by Cormen et al.
A RB tree differs from a regular binary search tree by having 1 extra attribute in its nodes: colour, which is either red or black.
This property and the constraints surrounding it ensure that a RB tree is approximately balanced and require less rotations (compared to say AVL trees).

My RB tree also makes use of a single sentinel node instead of a nullptr to simplify handling of last and root nodes (as we can treat the sentinel node like a normal node).

The tree is therefore somewhat circular, as both the root's parent and the leaves child points to the single sentinel.

RB tree diagrams
(a) conventional RB tree (b) RB tree using single sentinel node

Compilation

I've included extensive test files that compare my implementation of the containers against the STL ones.
Each function of the containers is tested against its STL version.
I also apply "stress tests" that utilize large container sizes to test complexity.
The compilation commands below will create 2 binaries - one compiled with my library "ft" and one compiled with the STL.

Build
Run make in the root directory to compile the test files with the container library headers.
This will compile with stress tests included (see above).
Binaries ft_bin and std_bin may then be executed to see behaviour of respective libraries.

For compiling without stress tests:
make quick

Quick Build-n-Run

make run    # runs simple output comparison using diff, ignores performance
make compare    # detailed comparison program that shows output and performance differences
make qcompare    # same as above but without stress test, faster
Output of binary compiled by make Output of make qcompare

Resources

General:

Vector:

SFINAE:

Red-Black Tree:

Sentinel nodes in binary search trees:

Rebind: