Skip to content

aboderinsamuel/cpp-mini-stl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cpp-mini-stl

A header-only C++20 library reimplementing core Standard Library components from first principles, including a dynamic array (Vector<T>), hash table (HashMap<K,V>), smart pointers (UniquePtr<T>, SharedPtr<T>), and a fixed-block memory pool allocator.

Highlights

  • Reimplemented Vector<T> with manual memory management and move-aware reallocation
  • Built a separate-chaining HashMap<K,V> with automatic rehashing
  • Implemented UniquePtr<T> and reference-counted SharedPtr<T>
  • Designed a fixed-block MemoryPool allocator for fast small-object allocation
  • Header-only C++20 library
  • Unit tested and benchmarked
  • AddressSanitizer and UBSan clean

Components

Component Description
Vector<T> Dynamic contiguous array with amortized O(1) insertion
HashMap<K,V> Hash table using separate chaining
UniquePtr<T> Exclusive ownership smart pointer
SharedPtr<T> Reference-counted smart pointer
MemoryPool Fixed-block allocator with free-list management

Project Structure

cpp-mini-stl/
├── CMakeLists.txt
├── README.md
├── include/
│   └── mini/
│       ├── vector.hpp
│       ├── hash_map.hpp
│       ├── unique_ptr.hpp
│       ├── shared_ptr.hpp
│       └── memory_pool.hpp
├── src/
│   └── main.cpp
├── tests/
│   └── test_cases.cpp
├── benchmarks/
│   └── benchmark.cpp
├── docs/
│   └── architecture.md
└── design.md

Build

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j

Run Tests

ctest --test-dir build

Usage

#include <mini/vector.hpp>
#include <mini/hash_map.hpp>
#include <mini/unique_ptr.hpp>
#include <iostream>

int main() {
    mini::Vector<int> numbers;
    numbers.push_back(10);
    numbers.push_back(20);
    numbers.push_back(30);

    mini::HashMap<std::string, int> scores;
    scores["alice"] = 95;
    scores["bob"] = 88;

    mini::UniquePtr<int> value(new int(42));

    std::cout << "Vector size: " << numbers.size() << '\n';
    std::cout << "Alice: " << scores["alice"] << '\n';
    std::cout << "Value: " << *value << '\n';

    return 0;
}

Benchmarks

./build/benchmarks/benchmark

Example results:

Benchmark Result
MemoryPool Allocation (64B) 8.4× faster than malloc
Vector Push Back Measured
HashMap Lookup Measured
SharedPtr Creation Measured

Documentation

  • docs/architecture.md — implementation details and architecture
  • design.md — design decisions and tradeoffs

Requirements

  • C++20
  • CMake 3.20+

License

MIT License

About

From-scratch C++ STL subset: Vector, HashMap, SmartPtr, Pool Allocator. Tested, valgrind-clean, move-aware.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors