Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional functionality to cvector and exposed public array.h and vector.h. #123

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Header-only library that provides 0 cost initialization for immutable containers
Frozen provides:

- immutable (a.k.a. frozen), ``constexpr``-compatible versions of ``std::set``,
``std::unordered_set``, ``std::map`` and ``std::unordered_map``.

- fixed-capacity, ``constinit``-compatible versions of ``std::map`` and
``std::unordered_set``, ``std::map`` and ``std::unordered_map``,
``std::vector``, and ``std::array``.

- fixed-capacity, ``constinit``-compatible versions of ``std::map`` and
``std::unordered_map`` with immutable, compile-time selected keys mapped
to mutable values.

Expand Down
2 changes: 2 additions & 0 deletions include/frozen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
target_sources(frozen-headers INTERFACE
"${prefix}/frozen/algorithm.h"
"${prefix}/frozen/array.h"
"${prefix}/frozen/map.h"
"${prefix}/frozen/random.h"
"${prefix}/frozen/set.h"
"${prefix}/frozen/string.h"
"${prefix}/frozen/unordered_map.h"
"${prefix}/frozen/unordered_set.h"
"${prefix}/frozen/vector.h"
"${prefix}/frozen/bits/algorithms.h"
"${prefix}/frozen/bits/basic_types.h"
"${prefix}/frozen/bits/elsa.h"
Expand Down
35 changes: 35 additions & 0 deletions include/frozen/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Frozen
* Copyright 2016 QuarksLab
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#ifndef FROZEN_LETITGO_ARRAY_H
#define FROZEN_LETITGO_ARRAY_H

#include "frozen/bits/basic_types.h"

namespace frozen {

template <class T, std::size_t N>
using array = bits::carray<T, N>;

} // namespace frozen

#endif
230 changes: 215 additions & 15 deletions include/frozen/bits/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ struct ignored_arg {};

template <class T, std::size_t N>
class cvector {
T data [N] = {}; // zero-initialization for scalar type T, default-initialized otherwise
std::size_t dsize = 0;
T data_[N] = {}; // zero-initialization for scalar type T, default-initialized otherwise
std::size_t dsize_ = 0;

template <class Iter>
constexpr cvector(Iter iter, size_t size)
: dsize_(size) {
for (std::size_t i = 0; i < size; ++i)
data_[i] = *iter++;
}

public:
// Container typdefs
Expand All @@ -51,36 +58,167 @@ class cvector {
using const_pointer = const value_type *;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;

// Constructors
constexpr cvector(void) = default;
constexpr cvector(size_type count, const T& value) : dsize(count) {
constexpr cvector(size_type count, const T& value) : dsize_(count) {
for (std::size_t i = 0; i < N; ++i)
data[i] = value;
data_[i] = value;
}

template <std::size_t M>
constexpr cvector(T const (&init)[M])
: cvector(init, M)
{
static_assert(M <= N, "Cannot initialize a cvector with a larger array");
}

constexpr cvector(std::initializer_list<T> init)
: cvector(init.begin(), init.size()) {}

// Iterators
constexpr iterator begin() noexcept { return data_; }
constexpr const_iterator begin() const noexcept { return data_; }
constexpr const_iterator cbegin() const noexcept { return data_; }
constexpr iterator end() noexcept { return data_ + dsize_; }
constexpr const_iterator end() const noexcept { return data_ + dsize_; }
constexpr const_iterator cend() const noexcept { return data_ + dsize_; }

constexpr reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
constexpr reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }

// Capacity
constexpr bool empty() const { return dsize_ == 0; }
constexpr size_type size() const { return dsize_; }
constexpr size_type max_size() const { return N; }
constexpr size_type capacity() const { return N; }

// Element access
constexpr reference operator[](std::size_t index) { return data_[index]; }
constexpr const_reference operator[](std::size_t index) const { return data_[index]; }

constexpr reference at(std::size_t index) {
if (index > dsize_)
FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (" + std::to_string(dsize_) + ')'));
return data_[index];
}
constexpr const_reference at(std::size_t index) const {
if (index > dsize_)
FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (" + std::to_string(dsize_) + ')'));
return data_[index];
}

constexpr reference front() { return data_[0]; }
constexpr const_reference front() const { return data_[0]; }

constexpr reference back() { return data_[dsize_ - 1]; }
constexpr const_reference back() const { return data_[dsize_ - 1]; }

constexpr value_type* data() noexcept { return data; }
constexpr const value_type* data() const noexcept { return data; }

// Modifiers
constexpr void push_back(const T & a) { data_[dsize_++] = a; }
constexpr void push_back(T && a) { data_[dsize_++] = std::move(a); }
constexpr void pop_back() { --dsize_; }

constexpr void clear() { dsize_ = 0; }

constexpr void fill(const value_type& val) {
for (std::size_t i = 0; i < N; ++i)
{
data_[i] = val;
}
dsize_ = N;
}
};

// Specialization for a compile-time empty container.
template <class T>
class cvector<T, 0> {
public:
// Container typdefs
using value_type = T;
using reference = value_type &;
using const_reference = const value_type &;
using pointer = value_type *;
using const_pointer = const value_type *;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;

// Constructors
constexpr cvector(void) = default;
constexpr cvector(size_type count, const T& value)
{
// static_assert(count == 0, "Cannot initialize empty cvector");
(void)count;
(void)value;
}

template <std::size_t M>
constexpr cvector(T const (&init)[M])
{
static_assert(M == 0, "Cannot initialize empty cvector");
(void)init;
}

constexpr cvector(std::initializer_list<T> init)
{
static_assert(init.size() == 0, "Cannot initialize empty cvector");
}

// Iterators
constexpr iterator begin() noexcept { return data; }
constexpr iterator end() noexcept { return data + dsize; }
constexpr iterator begin() noexcept { return nullptr; }
constexpr const_iterator begin() const noexcept { return nullptr; }
constexpr const_iterator cbegin() const noexcept { return nullptr; }
constexpr iterator end() noexcept { return nullptr; }
constexpr const_iterator end() const noexcept { return nullptr; }
constexpr const_iterator cend() const noexcept { return nullptr; }

constexpr reverse_iterator rbegin() noexcept { return reverse_iterator(); }
constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(); }
constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(); }
constexpr reverse_iterator rend() noexcept { return reverse_iterator(); }
constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(); }
constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(); }

// Capacity
constexpr size_type size() const { return dsize; }
constexpr bool empty() const { return true; }
constexpr size_type size() const { return 0; }
constexpr size_type max_size() const { return 0; }
constexpr size_type capacity() const { return 0; }

// Element access
constexpr reference operator[](std::size_t index) { return data[index]; }
constexpr const_reference operator[](std::size_t index) const { return data[index]; }
constexpr reference at(std::size_t index) {
FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (0)"));
}
constexpr const_reference at(std::size_t index) const {
FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (0)"));
}

constexpr reference back() { return data[dsize - 1]; }
constexpr const_reference back() const { return data[dsize - 1]; }
constexpr value_type* data() noexcept { return nullptr; }
constexpr const value_type* data() const noexcept { return nullptr; }

// Modifiers
constexpr void push_back(const T & a) { data[dsize++] = a; }
constexpr void push_back(T && a) { data[dsize++] = std::move(a); }
constexpr void pop_back() { --dsize; }
constexpr void push_back(const T & a) { (void)a; }
constexpr void push_back(T && a) { (void)a; }
constexpr void pop_back() {}

constexpr void clear() {}

constexpr void clear() { dsize = 0; }
constexpr void fill(const value_type& val) { (void)val; }
};

template <class T, std::size_t N>
Expand Down Expand Up @@ -145,6 +283,7 @@ class carray {
constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }

// Capacity
constexpr bool empty() const { return N == 0; }
constexpr size_type size() const { return N; }
constexpr size_type max_size() const { return N; }

Expand Down Expand Up @@ -178,6 +317,8 @@ class carray {
data_[i] = val;
}
};

// Specialization for a compile-time empty container.
template <class T>
class carray<T, 0> {

Expand All @@ -197,6 +338,65 @@ class carray<T, 0> {

// Constructors
constexpr carray(void) = default;
constexpr carray(size_type count, const T& value)
{
// static_assert(count == 0, "Cannot initialize empty carray");
(void)count;
(void)value;
}

template <std::size_t M>
constexpr carray(T const (&init)[M])
{
static_assert(M == 0, "Cannot initialize empty carray");
(void)init;
}

template <std::size_t M>
constexpr carray(std::array<T, M> const &init)
{
static_assert(M == 0, "Cannot initialize empty carray");
(void)init;
}

constexpr carray(std::initializer_list<T> init)
{
static_assert(init.size() == 0, "Cannot initialize empty carray");
}

// Iterators
constexpr iterator begin() noexcept { return nullptr; }
constexpr const_iterator begin() const noexcept { return nullptr; }
constexpr const_iterator cbegin() const noexcept { return nullptr; }
constexpr iterator end() noexcept { return nullptr; }
constexpr const_iterator end() const noexcept { return nullptr; }
constexpr const_iterator cend() const noexcept { return nullptr; }

constexpr reverse_iterator rbegin() noexcept { return reverse_iterator(); }
constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(); }
constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(); }
constexpr reverse_iterator rend() noexcept { return reverse_iterator(); }
constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(); }
constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(); }

// Capacity
constexpr bool empty() const { return true; }
constexpr size_type size() const { return 0; }
constexpr size_type max_size() const { return 0; }

// Element access
constexpr reference at(std::size_t index) {
FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (0)"));
}
constexpr const_reference at(std::size_t index) const {
FROZEN_THROW_OR_ABORT(std::out_of_range("Index (" + std::to_string(index) + ") out of bound (0)"));
}

constexpr value_type* data() noexcept { return nullptr; }
constexpr const value_type* data() const noexcept { return nullptr; }

// Modifiers
constexpr void fill(const value_type& val) { (void)val; }

};

Expand Down
35 changes: 35 additions & 0 deletions include/frozen/vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Frozen
* Copyright 2016 QuarksLab
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#ifndef FROZEN_LETITGO_VECTOR_H
#define FROZEN_LETITGO_VECTOR_H

#include "frozen/bits/basic_types.h"

namespace frozen {

template <class T, std::size_t N>
using vector = bits::cvector<T, N>;

} // namespace frozen

#endif
4 changes: 3 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target_sources(frozen.tests PRIVATE
${CMAKE_CURRENT_LIST_DIR}/bench.hpp
${CMAKE_CURRENT_LIST_DIR}/catch.hpp
${CMAKE_CURRENT_LIST_DIR}/test_algorithms.cpp
${CMAKE_CURRENT_LIST_DIR}/test_array.cpp
${CMAKE_CURRENT_LIST_DIR}/test_main.cpp
${CMAKE_CURRENT_LIST_DIR}/test_map.cpp
${CMAKE_CURRENT_LIST_DIR}/test_rand.cpp
Expand All @@ -19,7 +20,8 @@ target_sources(frozen.tests PRIVATE
${CMAKE_CURRENT_LIST_DIR}/test_unordered_map.cpp
${CMAKE_CURRENT_LIST_DIR}/test_unordered_map_str.cpp
${CMAKE_CURRENT_LIST_DIR}/test_unordered_set.cpp
${CMAKE_CURRENT_LIST_DIR}/test_unordered_str_set.cpp)
${CMAKE_CURRENT_LIST_DIR}/test_unordered_str_set.cpp
${CMAKE_CURRENT_LIST_DIR}/test_vector.cpp)

string(CONCAT generator
# msvc gives invalid integral overflow warning for unsigned type
Expand Down
Loading