Commit 4dd4f663 by Maarten L. Hekkelman

backup

parent 04b7828a
......@@ -54,88 +54,25 @@ class category
category() = default;
category(std::string_view name)
: m_name(name)
{
}
category(std::string_view name);
category(const category &rhs)
: m_name(rhs.m_name)
, m_columns(rhs.m_columns)
{
for (auto r = rhs.m_head; r != nullptr; r = r->m_next)
insert_impl(end(), clone_row(*r));
}
category(const category &rhs);
category(category &&rhs)
: m_name(std::move(rhs.m_name))
, m_columns(std::move(rhs.m_columns))
, m_head(rhs.m_head)
, m_tail(rhs.m_tail)
{
rhs.m_head = nullptr;
rhs.m_tail = nullptr;
}
category(category &&rhs);
category &operator=(const category &rhs)
{
if (this != &rhs)
{
if (not empty())
clear();
category &operator=(const category &rhs);
m_name = rhs.m_name;
m_columns = rhs.m_columns;
category &operator=(category &&rhs);
for (auto r = rhs.m_head; r != nullptr; r = r->m_next)
insert_impl(cend(), clone_row(*r));
}
return *this;
}
category &operator=(category &&rhs)
{
if (this != &rhs)
{
if (not empty())
clear();
m_name = std::move(rhs.m_name);
m_columns = std::move(rhs.m_columns);
m_head = rhs.m_head;
m_tail = rhs.m_tail;
rhs.m_head = rhs.m_tail = nullptr;
}
return *this;
}
~category()
{
clear();
}
~category();
// --------------------------------------------------------------------
const std::string &name() const { return m_name; }
iset fields() const
{
if (m_validator == nullptr)
throw std::runtime_error("No Validator specified");
if (m_cat_validator == nullptr)
m_validator->report_error("undefined Category", true);
iset fields() const;
iset result;
for (auto &iv : m_cat_validator->m_item_validators)
result.insert(iv.m_tag);
return result;
}
std::set<uint16_t> key_field_indices() const;
void set_validator(const validator *v, datablock &db);
void update_links(datablock &db);
......@@ -374,6 +311,20 @@ class category
}
// --------------------------------------------------------------------
bool has_children(row_handle r) const;
bool has_parents(row_handle r) const;
std::vector<row_handle> get_children(row_handle r, category &childCat);
// std::vector<row_handle> getChildren(row_handle r, const char *childCat);
std::vector<row_handle> get_parents(row_handle r, category &parentCat);
// std::vector<row_handle> getParents(row_handle r, const char *parentCat);
std::vector<row_handle> get_linked(row_handle r, category &cat);
// std::vector<row_handle> getLinked(row_handle r, const char *cat);
// --------------------------------------------------------------------
// void insert(const_iterator pos, const row &row)
// {
......@@ -500,7 +451,7 @@ class category
{
auto iv = m_cat_validator->get_validator_for_item(column_name);
if (iv == nullptr)
std::cerr << "Invalid name used '" << name << "' is not a known column in " + m_name << std::endl;
std::cerr << "Invalid name used '" << column_name << "' is not a known column in " + m_name << std::endl;
}
return result;
......@@ -690,6 +641,7 @@ class category
const category_validator *m_cat_validator = nullptr;
std::vector<link> m_parent_links, m_child_links;
bool m_cascade = true;
class category_index* m_index = nullptr;
row *m_head = nullptr, *m_tail = nullptr;
};
......
......@@ -604,11 +604,6 @@ inline condition all()
return condition(new detail::all_condition_impl());
}
// inline condition_t<category> Not(condition_t<category> &&cond)
// {
// return condition_t<category>(new detail::not_condition_impl(std::move(cond)));
// }
namespace literals
{
inline key operator""_key(const char *text, size_t length)
......
......@@ -35,16 +35,9 @@ namespace cif::v2
// --------------------------------------------------------------------
class datablock
class datablock : public std::list<category>
{
public:
using category_list = std::list<category>;
using iterator = category_list::iterator;
using const_iterator = category_list::const_iterator;
using reference = typename category_list::reference;
datablock() = default;
datablock(std::string_view name)
......@@ -90,48 +83,31 @@ class datablock
// --------------------------------------------------------------------
bool empty() const { return m_categories.empty(); }
size_t size() const { return m_categories.size(); }
reference front() { return m_categories.front(); }
reference back() { return m_categories.back(); }
iterator begin() { return m_categories.begin(); }
iterator end() { return m_categories.end(); }
const_iterator cbegin() { return m_categories.cbegin(); }
const_iterator cend() { return m_categories.cend(); }
const_iterator begin() const { return m_categories.begin(); }
const_iterator end() const { return m_categories.end(); }
// --------------------------------------------------------------------
category &operator[](std::string_view name)
{
auto i = std::find_if(m_categories.begin(), m_categories.end(), [name](const category &c)
auto i = std::find_if(begin(), end(), [name](const category &c)
{ return iequals(c.name(), name); });
if (i != m_categories.end())
if (i != end())
return *i;
m_categories.emplace_back(name);
return m_categories.back();
emplace_back(name);
return back();
}
const category &operator[](std::string_view name) const
{
static const category s_empty;
auto i = std::find_if(m_categories.begin(), m_categories.end(), [name](const category &c)
auto i = std::find_if(begin(), end(), [name](const category &c)
{ return iequals(c.name(), name); });
return i == m_categories.end() ? s_empty : *i;
return i == end() ? s_empty : *i;
}
category *get(std::string_view name)
{
auto i = std::find_if(m_categories.begin(), m_categories.end(), [name](const category &c)
auto i = std::find_if(begin(), end(), [name](const category &c)
{ return iequals(c.name(), name); });
return i == m_categories.end() ? nullptr : &*i;
return i == end() ? nullptr : &*i;
}
const category *get(std::string_view name) const
......@@ -143,17 +119,17 @@ class datablock
{
bool is_new = true;
auto i = m_categories.begin();
while (i != m_categories.end())
auto i = begin();
while (i != end())
{
if (iequals(name, i->name()))
{
is_new = false;
if (i != m_categories.begin())
if (i != begin())
{
auto n = std::next(i);
m_categories.splice(m_categories.begin(), m_categories, i, n);
splice(begin(), *this, i, n);
}
break;
......@@ -164,14 +140,11 @@ class datablock
if (is_new)
{
m_categories.emplace(m_categories.begin(), name);
// m_categories.emplace(begin(), *this, std::string(name), mValidator);
// for (auto &cat : mCategories)
// cat.updateLinks();
auto &c = emplace_front(name);
c.set_validator(m_validator, *this);
}
return std::make_tuple(m_categories.begin(), is_new);
return std::make_tuple(begin(), is_new);
}
// void write(std::ostream &os) const
......@@ -217,7 +190,6 @@ class datablock
// }
private:
category_list m_categories;
std::string m_name;
const validator *m_validator = nullptr;
};
......
......@@ -36,17 +36,9 @@ namespace cif::v2
// --------------------------------------------------------------------
class file
class file : public std::list<datablock>
{
public:
using datablock_list = std::list<datablock>;
using value_type = datablock_list::value_type;
using reference = datablock_list::reference;
using pointer = datablock_list::pointer;
using iterator = datablock_list::iterator;
using const_iterator = datablock_list::const_iterator;
file() = default;
......@@ -113,39 +105,39 @@ class file
datablock &operator[](std::string_view name)
{
auto i = std::find_if(m_datablocks.begin(), m_datablocks.end(), [name](const datablock &c)
auto i = std::find_if(begin(), end(), [name](const datablock &c)
{ return iequals(c.name(), name); });
if (i != m_datablocks.end())
if (i != end())
return *i;
m_datablocks.emplace_back(name);
return m_datablocks.back();
emplace_back(name);
return back();
}
const datablock &operator[](std::string_view name) const
{
static const datablock s_empty;
auto i = std::find_if(m_datablocks.begin(), m_datablocks.end(), [name](const datablock &c)
auto i = std::find_if(begin(), end(), [name](const datablock &c)
{ return iequals(c.name(), name); });
return i == m_datablocks.end() ? s_empty : *i;
return i == end() ? s_empty : *i;
}
std::tuple<iterator, bool> emplace(std::string_view name)
{
bool is_new = true;
auto i = m_datablocks.begin();
while (i != m_datablocks.end())
auto i = begin();
while (i != end())
{
if (iequals(name, i->name()))
{
is_new = false;
if (i != m_datablocks.begin())
if (i != begin())
{
auto n = std::next(i);
m_datablocks.splice(m_datablocks.begin(), m_datablocks, i, n);
splice(begin(), *this, i, n);
}
break;
......@@ -155,26 +147,14 @@ class file
}
if (is_new)
m_datablocks.emplace(m_datablocks.begin(), name);
{
auto &db = emplace_front(name);
db.set_validator(m_validator);
}
return std::make_tuple(m_datablocks.begin(), is_new);
return std::make_tuple(begin(), is_new);
}
bool empty() const { return m_datablocks.empty(); }
size_t size() const { return m_datablocks.size(); }
iterator begin() { return m_datablocks.begin(); }
iterator end() { return m_datablocks.end(); }
const_iterator cbegin() { return m_datablocks.begin(); }
const_iterator cend() { return m_datablocks.end(); }
const_iterator begin() const { return m_datablocks.begin(); }
const_iterator end() const { return m_datablocks.end(); }
reference front() { return m_datablocks.front(); }
reference back() { return m_datablocks.back(); }
void load(std::istream &is)
{
auto saved = m_validator;
......@@ -191,7 +171,6 @@ class file
}
private:
datablock_list m_datablocks;
const validator* m_validator = nullptr;
};
......
......@@ -89,7 +89,7 @@ class iterator_impl
template <typename IRowType>
iterator_impl(iterator_impl<IRowType, Ts...> &rhs)
: m_category(rhs.m_category)
, m_current(rhs.m_current)
, m_current(const_cast<row_type *>(rhs.m_current))
, m_value(rhs.m_value)
, m_column_ix(rhs.m_column_ix)
{
......@@ -274,7 +274,7 @@ class conditional_iterator_proxy
using value_type = conditional_iterator_proxy::value_type;
using difference_type = std::ptrdiff_t;
using pointer = value_type *;
using reference = row_handle;
using reference = value_type;
conditional_iterator_impl(CategoryType &cat, row_iterator pos, const condition &cond, const std::array<size_t, N> &cix);
conditional_iterator_impl(const conditional_iterator_impl &i) = default;
......
......@@ -110,6 +110,7 @@ class row
private:
friend class item_handle;
friend class category_index;
template <typename, typename...>
friend class iterator_impl;
......@@ -136,6 +137,7 @@ class row_handle
public:
friend class item_handle;
friend class category;
friend class category_index;
row_handle() = default;
......@@ -258,6 +260,9 @@ class row_handle
void assign(size_t column, std::string_view value, bool updateLinked, bool validate = true);
bool operator==(const row_handle &rhs) const { return m_category == rhs.m_category and m_row == rhs.m_row; }
bool operator!=(const row_handle &rhs) const { return m_category != rhs.m_category or m_row != rhs.m_row; }
private:
uint16_t get_column_ix(std::string_view name) const;
......
......@@ -82,7 +82,7 @@ struct type_validator
return icompare(m_name, rhs.m_name) < 0;
}
int compare(const char *a, const char *b) const;
int compare(std::string_view a, std::string_view b) const;
};
struct item_validator
......
......@@ -45,7 +45,7 @@ std::string_view item_handle::text() const
void item_handle::assign_value(const item &v)
{
m_row_handle.assign(m_column, v.value(), false);
m_row_handle.assign(m_column, v.value(), true);
}
}
......@@ -31,6 +31,8 @@
#include <cif++/v2/dictionary_parser.hpp>
#include <cif++/v2/validate.hpp>
#include <cif++/CifUtils.hpp>
namespace cif
{
extern int VERBOSE;
......@@ -69,25 +71,27 @@ DDL_PrimitiveType map_to_primitive_type(std::string_view s)
// --------------------------------------------------------------------
int type_validator::compare(const char *a, const char *b) const
int type_validator::compare(std::string_view a, std::string_view b) const
{
int result = 0;
if (*a == 0)
result = *b == 0 ? 0 : -1;
else if (*b == 0)
result = *a == 0 ? 0 : +1;
if (a.empty())
result = b.empty() ? 0 : -1;
else if (b.empty())
result = a.empty() ? 0 : +1;
else
{
try
switch (m_primitive_type)
{
switch (m_primitive_type)
case DDL_PrimitiveType::Numb:
{
case DDL_PrimitiveType::Numb:
{
double da = strtod(a, nullptr);
double db = strtod(b, nullptr);
double da, db;
auto ra = cif::from_chars(a.begin(), a.end(), da);
auto rb = cif::from_chars(b.begin(), b.end(), db);
if (ra.ec == std::errc() and rb.ec == std::errc())
{
auto d = da - db;
if (std::abs(d) > std::numeric_limits<double>::epsilon())
{
......@@ -96,64 +100,64 @@ int type_validator::compare(const char *a, const char *b) const
else if (d < 0)
result = -1;
}
break;
}
else if (ra.ec == std::errc())
result = 1;
else
result = -1;
break;
}
case DDL_PrimitiveType::UChar:
case DDL_PrimitiveType::Char:
{
// CIF is guaranteed to have ascii only, therefore this primitive code will do
// also, we're collapsing spaces
case DDL_PrimitiveType::UChar:
case DDL_PrimitiveType::Char:
{
// CIF is guaranteed to have ascii only, therefore this primitive code will do
// also, we're collapsing spaces
auto ai = a, bi = b;
for (;;)
auto ai = a.begin(), bi = b.begin();
for (;;)
{
if (ai == a.end())
{
if (*ai == 0)
{
if (*bi != 0)
result = -1;
break;
}
else if (*bi == 0)
{
result = 1;
break;
}
char ca = *ai;
char cb = *bi;
if (bi != b.end())
result = -1;
break;
}
else if (bi == b.end())
{
result = 1;
break;
}
if (m_primitive_type == DDL_PrimitiveType::UChar)
{
ca = tolower(ca);
cb = tolower(cb);
}
char ca = *ai;
char cb = *bi;
result = ca - cb;
if (m_primitive_type == DDL_PrimitiveType::UChar)
{
ca = tolower(ca);
cb = tolower(cb);
}
if (result != 0)
break;
result = ca - cb;
if (ca == ' ')
{
while (ai[1] == ' ')
++ai;
while (bi[1] == ' ')
++bi;
}
if (result != 0)
break;
++ai;
++bi;
if (ca == ' ')
{
while (ai[1] == ' ')
++ai;
while (bi[1] == ' ')
++bi;
}
break;
++ai;
++bi;
}
break;
}
}
catch (const std::invalid_argument &ex)
{
result = 1;
}
}
return result;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment