2020-03-12 15:23:25 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
//# Copyright (c) 2019-2020 Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
2020-04-10 16:46:00 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-03-12 15:23:25 +00:00
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
#pragma once
|
2020-04-09 08:13:12 +00:00
|
|
|
//# {{
|
2020-06-23 10:04:05 +00:00
|
|
|
#include "toml_preprocessor.h"
|
|
|
|
#if !TOML_IMPLEMENTATION
|
2020-04-01 12:53:10 +00:00
|
|
|
#error This is an implementation-only header.
|
|
|
|
#endif
|
2020-04-08 13:33:57 +00:00
|
|
|
//# }}
|
2020-06-23 10:04:05 +00:00
|
|
|
|
|
|
|
#include "toml_array.h"
|
2020-07-05 15:08:28 +00:00
|
|
|
|
2020-07-25 17:37:30 +00:00
|
|
|
TOML_NAMESPACE_START
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-07-27 10:38:01 +00:00
|
|
|
array::array() noexcept = default;
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-07-27 10:38:01 +00:00
|
|
|
array::array(const array& other) noexcept
|
|
|
|
: node{ other }
|
|
|
|
{
|
|
|
|
elements.reserve(other.elements.size());
|
|
|
|
for (const auto& elem : other)
|
|
|
|
elements.emplace_back(impl::make_node(elem));
|
|
|
|
}
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
array::array(array&& other) noexcept
|
|
|
|
: node{ std::move(other) },
|
2020-07-26 12:03:33 +00:00
|
|
|
elements{ std::move(other.elements) }
|
2020-03-01 14:56:40 +00:00
|
|
|
{}
|
|
|
|
|
2020-07-27 10:38:01 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
array& array::operator= (const array& rhs) noexcept
|
|
|
|
{
|
|
|
|
if (&rhs != this)
|
|
|
|
{
|
|
|
|
node::operator=(rhs);
|
|
|
|
elements.clear();
|
|
|
|
elements.reserve(rhs.elements.size());
|
|
|
|
for (const auto& elem : rhs)
|
|
|
|
elements.emplace_back(impl::make_node(elem));
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
array& array::operator= (array&& rhs) noexcept
|
|
|
|
{
|
2020-07-27 10:38:01 +00:00
|
|
|
if (&rhs != this)
|
|
|
|
{
|
|
|
|
node::operator=(std::move(rhs));
|
|
|
|
elements = std::move(rhs.elements);
|
|
|
|
}
|
2020-03-01 14:56:40 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-07-27 10:38:01 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
void array::preinsertion_resize(size_t idx, size_t count) noexcept
|
|
|
|
{
|
|
|
|
TOML_ASSERT(idx <= elements.size());
|
|
|
|
TOML_ASSERT(count >= 1_sz);
|
|
|
|
const auto old_size = elements.size();
|
|
|
|
const auto new_size = old_size + count;
|
|
|
|
const auto inserting_at_end = idx == old_size;
|
|
|
|
elements.resize(new_size);
|
|
|
|
if (!inserting_at_end)
|
|
|
|
{
|
|
|
|
for(size_t left = old_size, right = new_size - 1_sz; left --> idx; right--)
|
|
|
|
elements[right] = std::move(elements[left]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 15:08:28 +00:00
|
|
|
#define TOML_MEMBER_ATTR(attr) TOML_EXTERNAL_LINKAGE TOML_ATTR(attr)
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-07-05 15:08:28 +00:00
|
|
|
TOML_MEMBER_ATTR(const) node_type array::type() const noexcept { return node_type::array; }
|
|
|
|
TOML_MEMBER_ATTR(const) bool array::is_table() const noexcept { return false; }
|
|
|
|
TOML_MEMBER_ATTR(const) bool array::is_array() const noexcept { return true; }
|
|
|
|
TOML_MEMBER_ATTR(const) bool array::is_value() const noexcept { return false; }
|
|
|
|
TOML_MEMBER_ATTR(const) const array* array::as_array() const noexcept { return this; }
|
|
|
|
TOML_MEMBER_ATTR(const) array* array::as_array() noexcept { return this; }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-07-26 12:03:33 +00:00
|
|
|
TOML_MEMBER_ATTR(pure) const node& array::operator[] (size_t index) const noexcept { return *elements[index]; }
|
|
|
|
TOML_MEMBER_ATTR(pure) node& array::operator[] (size_t index) noexcept { return *elements[index]; }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-07-26 12:03:33 +00:00
|
|
|
TOML_MEMBER_ATTR(pure) const node& array::front() const noexcept { return *elements.front(); }
|
|
|
|
TOML_MEMBER_ATTR(pure) const node& array::back() const noexcept { return *elements.back(); }
|
|
|
|
TOML_MEMBER_ATTR(pure) node& array::front() noexcept { return *elements.front(); }
|
|
|
|
TOML_MEMBER_ATTR(pure) node& array::back() noexcept { return *elements.back(); }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-07-26 12:03:33 +00:00
|
|
|
TOML_MEMBER_ATTR(pure) array::const_iterator array::begin() const noexcept { return { elements.begin() }; }
|
|
|
|
TOML_MEMBER_ATTR(pure) array::const_iterator array::end() const noexcept { return { elements.end() }; }
|
|
|
|
TOML_MEMBER_ATTR(pure) array::const_iterator array::cbegin() const noexcept { return { elements.cbegin() }; }
|
|
|
|
TOML_MEMBER_ATTR(pure) array::const_iterator array::cend() const noexcept { return { elements.cend() }; }
|
|
|
|
TOML_MEMBER_ATTR(pure) array::iterator array::begin() noexcept { return { elements.begin() }; }
|
|
|
|
TOML_MEMBER_ATTR(pure) array::iterator array::end() noexcept { return { elements.end() }; }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-07-26 12:03:33 +00:00
|
|
|
TOML_MEMBER_ATTR(pure) size_t array::size() const noexcept { return elements.size(); }
|
|
|
|
TOML_MEMBER_ATTR(pure) size_t array::capacity() const noexcept { return elements.capacity(); }
|
|
|
|
TOML_MEMBER_ATTR(pure) bool array::empty() const noexcept { return elements.empty(); }
|
|
|
|
TOML_MEMBER_ATTR(const) size_t array::max_size() const noexcept { return elements.max_size(); }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-07-26 12:03:33 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE void array::reserve(size_t new_capacity) { elements.reserve(new_capacity); }
|
|
|
|
TOML_EXTERNAL_LINKAGE void array::clear() noexcept { elements.clear(); }
|
|
|
|
TOML_EXTERNAL_LINKAGE void array::shrink_to_fit() { elements.shrink_to_fit(); }
|
2020-07-05 15:08:28 +00:00
|
|
|
|
|
|
|
#undef TOML_MEMBER_ATTR
|
2020-05-23 14:35:47 +00:00
|
|
|
|
2020-08-02 14:02:10 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
bool array::is_homogeneous(node_type ntype) const noexcept
|
|
|
|
{
|
|
|
|
if (elements.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ntype == node_type::none)
|
|
|
|
ntype = elements[0]->type();
|
|
|
|
|
|
|
|
for (const auto& val : elements)
|
|
|
|
if (val->type() != ntype)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace impl
|
|
|
|
{
|
|
|
|
template <typename T, typename U>
|
|
|
|
TOML_INTERNAL_LINKAGE
|
|
|
|
bool array_is_homogeneous(T& elements, node_type ntype, U& first_nonmatch) noexcept
|
|
|
|
{
|
|
|
|
if (elements.empty())
|
|
|
|
{
|
|
|
|
first_nonmatch = {};
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (ntype == node_type::none)
|
|
|
|
ntype = elements[0]->type();
|
|
|
|
for (const auto& val : elements)
|
|
|
|
{
|
|
|
|
if (val->type() != ntype)
|
|
|
|
{
|
|
|
|
first_nonmatch = val.get();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
bool array::is_homogeneous(node_type ntype, toml::node*& first_nonmatch) noexcept
|
|
|
|
{
|
|
|
|
return impl::array_is_homogeneous(elements, ntype, first_nonmatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
bool array::is_homogeneous(node_type ntype, const toml::node*& first_nonmatch) const noexcept
|
|
|
|
{
|
|
|
|
return impl::array_is_homogeneous(elements, ntype, first_nonmatch);
|
|
|
|
}
|
|
|
|
|
2020-05-23 14:35:47 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
void array::truncate(size_t new_size)
|
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
if (new_size < elements.size())
|
|
|
|
elements.resize(new_size);
|
2020-05-23 14:35:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
array::iterator array::erase(const_iterator pos) noexcept
|
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
return { elements.erase(pos.raw_) };
|
2020-03-01 14:56:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
array::iterator array::erase(const_iterator first, const_iterator last) noexcept
|
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
return { elements.erase(first.raw_, last.raw_) };
|
2020-03-01 14:56:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
void array::pop_back() noexcept
|
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
elements.pop_back();
|
2020-03-01 14:56:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-07-05 15:08:28 +00:00
|
|
|
TOML_ATTR(pure)
|
2020-03-01 14:56:40 +00:00
|
|
|
node* array::get(size_t index) noexcept
|
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
return index < elements.size() ? elements[index].get() : nullptr;
|
2020-03-01 14:56:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-07-05 15:08:28 +00:00
|
|
|
TOML_ATTR(pure)
|
2020-03-01 14:56:40 +00:00
|
|
|
const node* array::get(size_t index) const noexcept
|
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
return index < elements.size() ? elements[index].get() : nullptr;
|
2020-03-01 14:56:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:14:54 +00:00
|
|
|
TOML_API
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
bool operator == (const array& lhs, const array& rhs) noexcept
|
|
|
|
{
|
|
|
|
if (&lhs == &rhs)
|
|
|
|
return true;
|
2020-07-26 12:03:33 +00:00
|
|
|
if (lhs.elements.size() != rhs.elements.size())
|
2020-03-01 14:56:40 +00:00
|
|
|
return false;
|
2020-07-26 12:03:33 +00:00
|
|
|
for (size_t i = 0, e = lhs.elements.size(); i < e; i++)
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
const auto lhs_type = lhs.elements[i]->type();
|
|
|
|
const node& rhs_ = *rhs.elements[i];
|
2020-03-01 14:56:40 +00:00
|
|
|
const auto rhs_type = rhs_.type();
|
|
|
|
if (lhs_type != rhs_type)
|
|
|
|
return false;
|
|
|
|
|
2020-07-26 12:03:33 +00:00
|
|
|
const bool equal = lhs.elements[i]->visit([&](const auto& lhs_) noexcept
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
|
|
|
return lhs_ == *reinterpret_cast<std::remove_reference_t<decltype(lhs_)>*>(&rhs_);
|
|
|
|
});
|
|
|
|
if (!equal)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:14:54 +00:00
|
|
|
TOML_API
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
bool operator != (const array& lhs, const array& rhs) noexcept
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
size_t array::total_leaf_count() const noexcept
|
|
|
|
{
|
|
|
|
size_t leaves{};
|
2020-07-26 12:03:33 +00:00
|
|
|
for (size_t i = 0, e = elements.size(); i < e; i++)
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
auto arr = elements[i]->as_array();
|
2020-03-01 14:56:40 +00:00
|
|
|
leaves += arr ? arr->total_leaf_count() : 1_sz;
|
|
|
|
}
|
|
|
|
return leaves;
|
|
|
|
}
|
|
|
|
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
void array::flatten_child(array&& child, size_t& dest_index) noexcept
|
|
|
|
{
|
|
|
|
for (size_t i = 0, e = child.size(); i < e; i++)
|
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
auto type = child.elements[i]->type();
|
2020-03-01 14:56:40 +00:00
|
|
|
if (type == node_type::array)
|
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
array& arr = *reinterpret_cast<array*>(child.elements[i].get());
|
2020-03-01 14:56:40 +00:00
|
|
|
if (!arr.empty())
|
|
|
|
flatten_child(std::move(arr), dest_index);
|
|
|
|
}
|
|
|
|
else
|
2020-07-26 12:03:33 +00:00
|
|
|
elements[dest_index++] = std::move(child.elements[i]);
|
2020-03-01 14:56:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-06-28 12:26:18 +00:00
|
|
|
array& array::flatten() &
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
if (elements.empty())
|
2020-06-28 12:26:18 +00:00
|
|
|
return *this;
|
2020-03-01 14:56:40 +00:00
|
|
|
|
|
|
|
bool requires_flattening = false;
|
2020-07-26 12:03:33 +00:00
|
|
|
size_t size_after_flattening = elements.size();
|
|
|
|
for (size_t i = elements.size(); i --> 0_sz;)
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
auto arr = elements[i]->as_array();
|
2020-03-01 14:56:40 +00:00
|
|
|
if (!arr)
|
|
|
|
continue;
|
|
|
|
size_after_flattening--; //discount the array itself
|
|
|
|
const auto leaf_count = arr->total_leaf_count();
|
|
|
|
if (leaf_count > 0_sz)
|
|
|
|
{
|
|
|
|
requires_flattening = true;
|
|
|
|
size_after_flattening += leaf_count;
|
|
|
|
}
|
|
|
|
else
|
2020-07-26 12:03:33 +00:00
|
|
|
elements.erase(elements.cbegin() + static_cast<ptrdiff_t>(i));
|
2020-03-01 14:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!requires_flattening)
|
2020-06-28 12:26:18 +00:00
|
|
|
return *this;
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-07-26 12:03:33 +00:00
|
|
|
elements.reserve(size_after_flattening);
|
2020-03-01 14:56:40 +00:00
|
|
|
|
|
|
|
size_t i = 0;
|
2020-07-26 12:03:33 +00:00
|
|
|
while (i < elements.size())
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
2020-07-26 12:03:33 +00:00
|
|
|
auto arr = elements[i]->as_array();
|
2020-03-01 14:56:40 +00:00
|
|
|
if (!arr)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-26 12:03:33 +00:00
|
|
|
std::unique_ptr<node> arr_storage = std::move(elements[i]);
|
2020-03-01 14:56:40 +00:00
|
|
|
const auto leaf_count = arr->total_leaf_count();
|
|
|
|
if (leaf_count > 1_sz)
|
|
|
|
preinsertion_resize(i + 1_sz, leaf_count - 1_sz);
|
|
|
|
flatten_child(std::move(*arr), i); //increments i
|
|
|
|
}
|
2020-06-28 12:26:18 +00:00
|
|
|
|
|
|
|
return *this;
|
2020-03-01 14:56:40 +00:00
|
|
|
}
|
2020-07-18 12:10:19 +00:00
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
bool array::is_array_of_tables() const noexcept
|
|
|
|
{
|
|
|
|
return is_homogeneous(node_type::table);
|
|
|
|
}
|
2020-03-01 14:56:40 +00:00
|
|
|
}
|
2020-07-25 17:37:30 +00:00
|
|
|
TOML_NAMESPACE_END
|