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-03-01 14:56:40 +00:00
|
|
|
#pragma once
|
|
|
|
#include "toml_array.h"
|
2020-04-01 12:53:10 +00:00
|
|
|
#if !defined(TOML_IMPLEMENTATION) || !TOML_IMPLEMENTATION
|
|
|
|
#error This is an implementation-only header.
|
|
|
|
#endif
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
namespace toml
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
void array::preinsertion_resize(size_t idx, size_t count) noexcept
|
|
|
|
{
|
|
|
|
const auto new_size = values.size() + count;
|
|
|
|
const auto inserting_at_end = idx == values.size();
|
|
|
|
values.resize(new_size);
|
|
|
|
if (!inserting_at_end)
|
|
|
|
{
|
|
|
|
for (size_t r = new_size, e = idx + count, l = e; r-- > e; l--)
|
|
|
|
values[r] = std::move(values[l]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
array::array() noexcept = default;
|
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
array::array(array&& other) noexcept
|
|
|
|
: node{ std::move(other) },
|
|
|
|
values{ std::move(other.values) }
|
|
|
|
{}
|
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
array& array::operator= (array&& rhs) noexcept
|
|
|
|
{
|
|
|
|
node::operator=(std::move(rhs));
|
|
|
|
values = std::move(rhs.values);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE node_type array::type() const noexcept { return node_type::array; }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE bool array::is_table() const noexcept { return false; }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE bool array::is_array() const noexcept { return true; }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE bool array::is_value() const noexcept { return false; }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE array* array::as_array() noexcept { return this; }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE const array* array::as_array() const noexcept { return this; }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE node& array::operator[] (size_t index) noexcept { return *values[index]; }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE const node& array::operator[] (size_t index) const noexcept { return *values[index]; }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE node& array::front() noexcept { return *values.front(); }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE const node& array::front() const noexcept { return *values.front(); }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE node& array::back() noexcept { return *values.back(); }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE const node& array::back() const noexcept { return *values.back(); }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE array::iterator array::begin() noexcept { return { values.begin() }; }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE array::const_iterator array::begin() const noexcept { return { values.begin() }; }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE array::const_iterator array::cbegin() const noexcept { return { values.cbegin() }; }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE array::iterator array::end() noexcept { return { values.end() }; }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE array::const_iterator array::end() const noexcept { return { values.end() }; }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE array::const_iterator array::cend() const noexcept { return { values.cend() }; }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE bool array::empty() const noexcept { return values.empty(); }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE size_t array::size() const noexcept { return values.size(); }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE void array::reserve(size_t new_capacity) { values.reserve(new_capacity); }
|
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE void array::clear() noexcept { values.clear(); }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
array::iterator array::erase(const_iterator pos) noexcept
|
|
|
|
{
|
|
|
|
return { values.erase(pos.raw_) };
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
array::iterator array::erase(const_iterator first, const_iterator last) noexcept
|
|
|
|
{
|
|
|
|
return { values.erase(first.raw_, last.raw_) };
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
void array::pop_back() noexcept
|
|
|
|
{
|
|
|
|
values.pop_back();
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
node* array::get(size_t index) noexcept
|
|
|
|
{
|
|
|
|
return index < values.size() ? values[index].get() : nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
const node* array::get(size_t index) const noexcept
|
|
|
|
{
|
|
|
|
return index < values.size() ? values[index].get() : nullptr;
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:14:54 +00:00
|
|
|
TOML_API
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
bool operator == (const array& lhs, const array& rhs) noexcept
|
|
|
|
{
|
|
|
|
if (&lhs == &rhs)
|
|
|
|
return true;
|
|
|
|
if (lhs.values.size() != rhs.values.size())
|
|
|
|
return false;
|
|
|
|
for (size_t i = 0, e = lhs.values.size(); i < e; i++)
|
|
|
|
{
|
|
|
|
const auto lhs_type = lhs.values[i]->type();
|
|
|
|
const node& rhs_ = *rhs.values[i];
|
|
|
|
const auto rhs_type = rhs_.type();
|
|
|
|
if (lhs_type != rhs_type)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const bool equal = lhs.values[i]->visit([&](const auto& lhs_) noexcept
|
|
|
|
{
|
|
|
|
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-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
bool operator != (const array& lhs, const array& rhs) noexcept
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-01 14:56:40 +00:00
|
|
|
size_t array::total_leaf_count() const noexcept
|
|
|
|
{
|
|
|
|
size_t leaves{};
|
|
|
|
for (size_t i = 0, e = values.size(); i < e; i++)
|
|
|
|
{
|
|
|
|
auto arr = values[i]->as_array();
|
|
|
|
leaves += arr ? arr->total_leaf_count() : 1_sz;
|
|
|
|
}
|
|
|
|
return leaves;
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_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++)
|
|
|
|
{
|
|
|
|
auto type = child.values[i]->type();
|
|
|
|
if (type == node_type::array)
|
|
|
|
{
|
|
|
|
array& arr = *reinterpret_cast<array*>(child.values[i].get());
|
|
|
|
if (!arr.empty())
|
|
|
|
flatten_child(std::move(arr), dest_index);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
values[dest_index++] = std::move(child.values[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:53:10 +00:00
|
|
|
TOML_FUNC_EXTERNAL_LINKAGE
|
2020-03-28 16:56:59 +00:00
|
|
|
void array::flatten()
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
|
|
|
if (values.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool requires_flattening = false;
|
|
|
|
size_t size_after_flattening = values.size();
|
|
|
|
for (size_t i = values.size(); i --> 0_sz;)
|
|
|
|
{
|
|
|
|
auto arr = values[i]->as_array();
|
|
|
|
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
|
|
|
|
values.erase(values.cbegin() + static_cast<ptrdiff_t>(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!requires_flattening)
|
|
|
|
return;
|
|
|
|
|
|
|
|
values.reserve(size_after_flattening);
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
while (i < values.size())
|
|
|
|
{
|
|
|
|
auto arr = values[i]->as_array();
|
|
|
|
if (!arr)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<node> arr_storage = std::move(values[i]);
|
|
|
|
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-03-28 16:56:59 +00:00
|
|
|
|