A collection of std-like single-header C++ libraries

itlib: iboB's Template Libraries

A collection of small single-header C++ libraries similar to or extending the C++ standard library. See below for a list.

Language License

itlib was forked from chobo-shl which is no longer supported. New libraries and updates to the existing ones are added here.

Build Status

Building with GitHub actions on Windows with MSVC, Ubuntu with GCC, and macOS with clang. Debug and Release. With address sanitizer.

Build

Libraries

Every .hpp file in include/itlib is a standalone library and has no dependencies other than the standard lib.

Documentation is provided in comments at the top of each file.

In the list below each library shows its minimum supported C++ standard and has icons for other standards if additional features are available for them.

Library Description
dynamic_bitset.hpp Standard A class similar to std::bitset, but the number of bits is not a part of the type. It's also somewhat similar to std::vector<bool>;, but (so far) it has more limited modification capabilities.
flat_map.hpp Standard A class with the interface of std::map but implemented with an underlying std::vector-type container, thus providing better cache locality of the elements. Similar to boost::flat_map with the notable difference that the underlying container can be changed via a template argument (thus making the class not strictly an std::map drop-in replacement)
flat_set.hpp Standard A class with the interface of std::set but implemented with an underlying std::vector-type container, thus providing better cache locality of the elements. Similar to boost::flat_set with the notable difference that the underlying container can be changed via a template argument (thus making the class not strictly an std::set drop-in replacement)
make_ptr.hpp Standard Small helper functions for creating std::shared_ptr and std::unique_ptr which make the code shorter and more readable.
mem_streambuf.hpp Standard Two helper classes: mem_ostreambuf and mem_istreambuf which allow you to work with std::stream-s with buffers of contiguous memory.
memory_view.hpp Standard A class which provides a std::vector like interface (sans the methods which might change the size or capacity) to a chunk of memory. Similar to C++20's std::span
pod_vector.hpp Standard A container similar to std::vector, which contains PODs. This fact is used to improve performance by skipping constructor and destructor calls and using memcpy and memmove to copy data, and malloc and free, and, most importantly realloc, to manage memory.
qalgorithm.hpp Standard Wrappers of <algorithm> functions which work on entire containers for less typing in the most common use-cases.
rstream.hpp Standard Read stream. Simple std::istream wrappers which don't allow seeks, allowing you to be certain that reads are sequential, and thus allow a redirect, so you can represent several streams as one.
sentry.hpp Standard Standard A sentry class which executes a function object on destruction. Works with C++11, but it's slightly easier to use with C++17.
small_vector.hpp Standard A mix between std::vector and itlib::static_vector. It's a dynamic array, optimized for use when the number of elements is small. Like static_vector is has a static buffer with a given capacity, but can fall back to dynamically allocated memory, should the size exceed it. Similar to boost::small_vector
static_vector.hpp Standard A mix between std::vector and std::array: A dynamically sized container with fixed capacity (supplied as a template parameter). This allows you to have dynamically sized vectors on the stack or as cache-local value members, as long as you know a big enough capacity beforehand. Similar to boost::static_vector.
time_t.hpp Standard A thin wrapper of std::time_t which provides thread safe std::tm getters and type-safe (std::chrono::duration-based) arithmetic
type_traits.hpp Standard Standard Additional type traits to extend the standard library's <type_traits>
ufunction.hpp Standard Standard Unique function. A replacement of std::function which is non-copyable (can capture non-copyable values, and wrap non-copyable objects), and noexcept move-constructible (won't implicitly make owners no-noexcept move-constructible)

Usage

Clone the repo or choose one or more libraries that you like and copy them somewhere in your include paths.

Every library is self-contained so you can copy, move, and modify whichever you like and not wory about interdependencies.

Contributing

Pull requests and issues are welcome.

Please make one pull request and issue per library, tagging them with the library name in the title with brackets. Example:

  • [small_vector] Added insert methods
  • [flat_map] Crash when using with xxxx container

You can use CMake to generate a project and run the tests locally.

Copyright

Copyright © 2016-2019 Chobolabs Inc.

Copyright © 2020-2021 Borislav Stanimirov

These libraries are distributed under the MIT Software License. See LICENSE.txt for further details or copy here.

Owner
Borislav Stanimirov
A (mostly) C++ programmer
Borislav Stanimirov
Comments
  • Support older CMake versions

    Support older CMake versions

    itlib's CMakeList.txt requires CMake 3.10, however I think it does not use any newer commands and thus would be compatible with older versions.

    https://github.com/iboB/itlib/blob/1dc9abf235c64c02c7d0482ef0c81e7c3b759ab8/CMakeLists.txt#L1

  • pod_vector bug in shrink_at

    pod_vector bug in shrink_at

    shrink_at () returns wrong iterator

    T* shrink_at(const T* cp, size_type num) { const auto s = size(); if (s == num) { clear(); return m_end; }

        auto position = const_cast<T*>(cp);
    
        std::memmove(position, position + num, size_t(m_end - position - num) * sizeof(T));
    
        m_end -= num;
    
        return ++position
    }
    

    Should be:

    T* shrink_at(const T* cp, size_type num) { const auto s = size(); if (s == num) { clear(); return m_end; }

        auto position = const_cast<T*>(cp);
    
        std::memmove(position, position + num, size_t(m_end - position - num) * sizeof(T));
    
        m_end -= num;
    
        return position;
    }
    
  • Typo make_shaerd

    Typo make_shaerd

    https://github.com/iboB/itlib/blob/master/include/itlib/make_ptr.hpp#L59 https://github.com/iboB/itlib/blob/master/include/itlib/make_ptr.hpp#L60

    Typo make_shaerd => make_shared

  • bug in small_vector  resize

    bug in small_vector resize

    Corner case in resize if going from dynamic memory to static , causing corrupt memory.

    Reproduce: itlib::small_vector<int, 4,3> v(5); v.resize(2);

    but just checking new_buf in resize() , I think is enough:

    if (m_begin != static_begin_ptr()) { // we've moved from dyn to dyn memory, so deallocate the old one if (new_buf != static_begin_ptr()) atraits::deallocate(get_alloc(), m_begin, m_capacity);

     }
    
  • [pod_vector] Fix alignment handling

    [pod_vector] Fix alignment handling

    Currently pod_vector doesn't handle the alignment of T at all. This is bad (though due to the fact that malloc returns aligned pages, it's mitigated, at least until one tries to create a custom allocator)

    The question here is should pod_vector handle the alignment (by modifying and tweaking the memory returned by malloc and realloc) or should the allocator handle it by providing an additional argument - alignment - to these functions

  • [ufunction] Can't construct ufunction from a free function

    [ufunction] Can't construct ufunction from a free function

    This works with msvc, but with clang and gcc it produces:

    [build] itlib/include/itlib/ufunction.hpp:106:63: error: invalid use of ‘const_cast’ with type ‘int (&)(int, int)’, which is a pointer or reference to a function type
    [build]   106 |         copy_wrapper(const copy_wrapper& other) : func_object(const_cast<FO&&>(other.func_object)) { throw 0; } // should never get to here
    [build]       |                                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

    Apparently the const_cast fails. However we need this const cast for the fake copy constructor.

    Potential solutions would be:

    1. Instead of a const cast add a function which works with const_cast for non-free functions and just copies free functions
    2. Devise some other way of obtaining a FO instance... something like a code-level declval

    The key is that this only needs to compile. It's not important to do something which does sensible stuff when executed.

    Not sure how to approach this yet.

optional lite - A C++17-like optional, a nullable object for C++98, C++11 and later in a single-file header-only library

optional lite: A single-file header-only version of a C++17-like optional, a nullable object for C++98, C++11 and later Contents Example usage In a nu

Aug 10, 2022
span lite - A C++20-like span for C++98, C++11 and later in a single-file header-only library

span lite: A single-file header-only version of a C++20-like span for C++98, C++11 and later Contents Example usage In a nutshell License Dependencies

Jul 30, 2022
string_view lite - A C++17-like string_view for C++98, C++11 and later in a single-file header-only library

string_view lite: A single-file header-only version of a C++17-like string_view for C++98, C++11 and later Contents Example usage In a nutshell Licens

Aug 10, 2022
variant lite - A C++17-like variant, a type-safe union for C++98, C++11 and later in a single-file header-only library

variant lite: A single-file header-only version of a C++17-like variant, a type-safe union for C++98, C++11 and later Contents Example usage In a nuts

Jul 26, 2022
Single-header header-only C++11 / C++14 / C++17 library for easily managing set of auto-generated type-safe flags.
Single-header header-only C++11 / C++14 / C++17 library for easily managing set of auto-generated type-safe flags.

Single-header header-only C++11 / C++14 / C++17 library for easily managing set of auto-generated type-safe flags. Quick start #include <bitflags/bitf

Jul 30, 2022
A standard conforming C++20 implementation of std::optional.

A standard conforming C++20 implementation of std::optional.

Feb 11, 2022
C++11/14/17 std::expected with functional-style extensions

expected Single header implementation of std::expected with functional-style extensions. Clang + GCC: MSVC: Available on Vcpkg and Conan. std::expecte

Aug 13, 2022
C++11/14/17 std::optional with functional-style extensions and reference support

optional Single header implementation of std::optional with functional-style extensions and support for references. Clang + GCC: MSVC: std::optional i

Aug 10, 2022
Range library for C++14/17/20, basis for C++20's std::ranges

range-v3 Range library for C++14/17/20. This code was the basis of a formal proposal to add range support to the C++ standard library. That proposal e

Aug 10, 2022
Monadic interface for std::optional

optional Simple monadic interface for std::optional Installation Just copy and include optional.h header in your project Usage All operations are in b

Apr 15, 2022
Improved and configurable drop-in replacement to std::function that supports move only types, multiple overloads and more

fu2::function an improved drop-in replacement to std::function Provides improved implementations of std::function: copyable fu2::function move-only fu

Aug 3, 2022
expected lite - Expected objects in C++11 and later in a single-file header-only library

expected lite: expected objects for C++11 and later expected lite is a single-file header-only library for objects that either represent a valid value

Aug 16, 2022
gsl-lite – A single-file header-only version of ISO C++ Guidelines Support Library (GSL) for C++98, C++11, and later

gsl-lite: Guidelines Support Library for C++98, C++11 up metadata build packages try online gsl-lite is an implementation of the C++ Core Guidelines S

Aug 10, 2022
gsl-lite – A single-file header-only version of ISO C++ Guidelines Support Library (GSL) for C++98, C++11, and later

gsl-lite: Guidelines Support Library for C++98, C++11 up metadata build packages try online gsl-lite is an implementation of the C++ Core Guidelines S

Aug 10, 2022
A header-only, unobtrusive, almighty alternative to the C++ switch statement that looks just like the original.

uberswitch A header-only, unobtrusive, almighty alternative to the C++ switch statement that looks just like the original. Sample usage (incomplete -

Jul 26, 2022
Abseil Common Libraries (C++)

The repository contains the Abseil C++ library code. Abseil is an open-source collection of C++ code (compliant to C++11) designed to augment the C++ standard library.

Aug 16, 2022
Concise CMake templates for creating C++ libraries or executables.

cmake_templates Concise cmake templates for creating C++ libraries and executables. Creating a normal cmake project Copy the chosen project template s

Aug 16, 2022
A collection of commonly used code for Algorithms Context written in C++

Algo Template CPP A collection of commonly used code for Algorithms Contest written in C++. Number int from_bin(string bin) string to_bin(int num) int

Feb 7, 2022
Lightweight single-file utilities for C99. Portable & zero dependency

plainlibs Lightweight single-file utilities for C99. Key Features Portable across Unix & Windows (including MSVC) Zero dependencies (besides C stdlib)

Jun 19, 2022