Commit 4dd4f663 by Maarten L. Hekkelman

backup

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