6#include "da_algorithm.hpp"
68 typedef std::shared_ptr< SeriesType > SeriesPtr;
72 typedef std::shared_ptr< TableType > TablePtr;
74 static SeriesPtr makeSeries();
75 static SeriesPtr makeSeries(
const QString& n);
76 static SeriesPtr makeSeries(std::initializer_list< T > args);
77 static TablePtr makeTable();
82 void reserve(
int size);
90 void fill(
const T& v);
91 const T& at(
int r,
int c)
const;
94 T
cell(
int r,
int c)
const;
95 SeriesPtr appendRow(
const QString& name);
96 SeriesPtr appendRow(
const QString& name,
int size);
97 void appendRow(SeriesPtr
row);
98 void appendRow(std::initializer_list< T > args,
const QString& n);
100 template<
typename Ite1,
typename Ite2 >
101 void appendColumn(Ite1 b, Ite2 e);
102 void appendColumn(std::initializer_list< T > args);
103 int nameToIndex(
const QString& n)
const;
104 SeriesPtr&
row(
int r);
105 const SeriesPtr&
row(
int r)
const;
106 SeriesPtr&
row(
const QString& n);
107 const SeriesPtr&
row(
const QString& n)
const;
110 SeriesPtr colunm(
int c)
const;
112 SeriesType& operator[](
int r);
113 const SeriesType& operator[](
int r)
const;
114 SeriesType& operator[](
const QString& rowname);
115 const SeriesType& operator[](
const QString& rowname)
const;
151 void setName(
const QString& n);
152 QString getName()
const;
173 TablePtr
takeByValue(
const QString& field, T value)
const;
181 QPair< QList< TablePtr >, QList< T > >
groupBy(
const QString& field)
const;
187 void orderBy(
const QString& sn);
196 QPair< T, int >
lowerBound(
const T& v,
const QString& sortedfield)
const;
197 QPair< T, int >
lowerBound(
const T& v,
int r)
const;
198 QPair< T, int > upperBound(
const T& v,
const QString& sortedfield)
const;
199 QPair< T, int > upperBound(
const T& v,
int r)
const;
213 void remove(
const QString& name);
214 void remove(
int rindex);
220 SeriesPtr m_nullseries;
228template<
typename T >
229typename DARowTable< T >::TablePtr takeByValue(
const DARowTable< T >& table,
const QString& field, T value)
231 const int r = table.nameToIndex(field);
233 typename DARowTable< T >::TablePtr res = DARowTable< T >::makeTable();
235 res->setName(table.getName());
236 res->setRowNames(table.rowNames());
237 int csize = table.columnCount();
239 for (
int i = 0; i < csize; ++i) {
240 if (table.cell(r, i) == value) {
241 typename DARowTable< T >::SeriesPtr col = table.colunm(i);
242 res->appendColumn(col->begin(), col->end());
255template<
typename T >
256typename DARowTable< T >::TablePtr takeByValue(
const DARowTable< T >& table,
int r, T value)
260 res->setName(table.getName());
264 for (
int i = 0; i < csize; ++i) {
265 if (table.
cell(r, i) == value) {
266 typename DARowTable< T >::SeriesPtr col = table.colunm(i);
267 res->appendColumn(col->begin(), col->end());
279template<
typename T >
280QPair< QList< typename DARowTable< T >::TablePtr >, QList< T > >
groupby(
const DARowTable< T >& table,
const QString& field)
282 QList< typename DARowTable< T >::TablePtr > restables;
285 int rindex = table.nameToIndex(field);
287 Q_ASSERT_X(rindex >= 0,
"groupby",
"unknow field");
288 typename DARowTable< T >::SeriesPtr r = table.
row(rindex);
290 return (qMakePair(restables, gr));
292 gr = r->toList().toSet().toList();
293 std::sort(gr.begin(), gr.end());
295 restables.append(takeByValue(table, rindex, v));
297 return (qMakePair(restables, gr));
304template<
typename T >
320template<
typename T >
323 return (a.value < b.value);
331template<
typename T >
332std::shared_ptr< DAVector< ValueWithIndex< T > > >
makeIndexSeries(
const typename DARowTable< T >::SeriesPtr& p)
334 std::shared_ptr< DAVector< ValueWithIndex< T > > > res = std::make_shared< DAVector< ValueWithIndex< T > > >();
336 res->setName(p->getName());
337 const int s = p->size();
340 for (
int i = 0; i < s; ++i) {
346template<
typename T >
347void orderBy(DARowTable< T >& table,
const QString& field)
349 const int r = table.nameToIndex(field);
354template<
typename T >
355void orderBy(DARowTable< T >& table,
int r)
357 typename DARowTable< T >::SeriesPtr row = table.row(r);
358 Q_ASSERT_X(row !=
nullptr,
"orderBy",
"unknow field");
359 auto ordser = makeIndexSeries< T >(row);
361 std::sort(ordser->begin(), ordser->end());
362 int rowcount = table.rowCount();
365 for (
int rc = 0; rc < rowcount; ++rc) {
366 typename DARowTable< T >::SeriesPtr series = table.row(rc);
367 typename DARowTable< T >::SeriesPtr ns = DARowTable< T >::makeSeries(series->getName());
368 ns->reserve(series->size());
369 for (
auto i = ordser->begin(); i != ordser->end(); ++i) {
370 ns->push_back(series->at((*i).index));
372 table.row(rc).swap(ns);
380template<
typename T >
381DARowTable< T >::DARowTable() : m_columns(0), m_mode(FixedMode), m_caseSensitivity(CaseInsensitive)
385template<
typename T >
386DARowTable< T >::DARowTable(
int rows,
int columns) : m_mode(FixedMode), m_caseSensitivity(CaseInsensitive)
390 for (
int i = 0; i < rows; ++i) {
391 m_d.push_back(SeriesType(columns));
401template<
typename T >
405 for (SeriesPtr& row : m_d) {
406 if (row ==
nullptr) {
419template<
typename T >
423 Qt::CaseSensitivity cs = isCaseSensitivity() ? Qt::CaseSensitive : Qt::CaseInsensitive;
425 for (
int i = 0; i < r; ++i) {
426 if (row(i)->name().compare(field, cs) == 0) {
433template<
typename T >
436 for (SeriesPtr r : m_d) {
441template<
typename T >
444 return (m_d.at(r)->at(c));
447template<
typename T >
448T& DARowTable< T >::at(
int r,
int c)
450 return (m_d[ r ]->
operator[](c));
459template<
typename T >
462 if (r < m_d.size()) {
463 const SeriesPtr& rr = row(r);
464 if (c < rr->size()) {
471template<
typename T >
474 return appendRow(name, columnCount());
477template<
typename T >
478typename DARowTable< T >::SeriesPtr DARowTable< T >::appendRow(
const QString& name,
int size)
480 SeriesPtr s = makeSeries(name);
486template<
typename T >
487void DARowTable< T >::appendRow(SeriesPtr row)
489 size_t s = row->size();
491 if ((s == m_columns) || (0 == m_columns)) {
494 }
else if (s < m_columns) {
495 row->resize(m_columns);
498 if (getMode() == ExpandMode) {
503 row->resize(m_columns);
509template<
typename T >
510void DARowTable< T >::appendRow(std::initializer_list< T > args,
const QString& n)
512 SeriesPtr r = makeSeries(args);
518template<
typename T >
519template<
typename Ite1,
typename Ite2 >
520void DARowTable< T >::appendColumn(Ite1 b, Ite2 e)
522 const int rc = rowCount();
525 for (
int i = 0; i < rc; ++i) {
528 row(i)->push_back(*it);
530 row(i)->push_back(T());
536template<
typename T >
537void DARowTable< T >::appendColumn(std::initializer_list< T > args)
539 const int rc = rowCount();
541 for (
int i = 0; i < rc; ++i) {
542 if (i < args.size()) {
543 row(i)->push_back(args[ i ]);
545 row(i)->push_back(T());
551template<
typename T >
552typename DARowTable< T >::SeriesPtr DARowTable< T >::makeSeries(
const QString& n)
554 return (std::make_shared< SeriesType >(n));
557template<
typename T >
558typename DARowTable< T >::SeriesPtr DARowTable< T >::makeSeries(std::initializer_list< T > args)
560 return (std::make_shared< SeriesType >(args));
563template<
typename T >
564typename DARowTable< T >::TablePtr DARowTable< T >::makeTable()
566 return (std::make_shared<
typename DARowTable< T >::TableType >());
569template<
typename T >
570typename DARowTable< T >::SeriesPtr DARowTable< T >::makeSeries()
572 return (std::make_shared< SeriesType >());
575template<
typename T >
576int DARowTable< T >::nameToIndex(
const QString& n)
const
579 Qt::CaseSensitivity cs = isCaseSensitivity() ? Qt::CaseSensitive : Qt::CaseInsensitive;
581 for (
int i = 0; i < r; ++i) {
582 if (row(i)->name().compare(n, cs) == 0) {
594template<
typename T >
605template<
typename T >
611template<
typename T >
614 int r = nameToIndex(n);
616 if ((r < 0) || (r > rowCount())) {
617 return (m_nullseries);
622template<
typename T >
623const typename DARowTable< T >::SeriesPtr& DARowTable< T >::row(
const QString& n)
const
625 int r = nameToIndex(n);
627 if ((r < 0) || (r > rowCount())) {
628 return (m_nullseries);
633template<
typename T >
634typename DARowTable< T >::SeriesPtr DARowTable< T >::colunm(
int c)
const
636 int rsize = rowCount();
637 SeriesPtr col = std::make_shared< SeriesType >(rsize);
639 for (
int r = 0; r < rsize; ++r) {
640 col->operator[](r) = cell(r, c);
645template<
typename T >
646typename DARowTable< T >::SeriesType& DARowTable< T >::operator[](
int r)
651template<
typename T >
652const typename DARowTable< T >::SeriesType& DARowTable< T >::operator[](
int r)
const
657template<
typename T >
658typename DARowTable< T >::SeriesType& DARowTable< T >::operator[](
const QString& rowname)
660 return (*(row(rowname)));
663template<
typename T >
664const typename DARowTable< T >::SeriesType& DARowTable< T >::operator[](
const QString& rowname)
const
666 return (*(row(rowname)));
669template<
typename T >
670void DARowTable< T >::reserve(
int size)
672 for (SeriesPtr p : m_d) {
677template<
typename T >
680 std::vector< int > ss;
682 for (
const SeriesPtr& r : m_d) {
683 ss.push_back(r->size());
685 int maxsize = *(std::max_element(ss.begin(), ss.end()));
687 for (SeriesPtr& r : m_d) {
688 if (r->size() < maxsize) {
695template<
typename T >
701template<
typename T >
707template<
typename T >
713template<
typename T >
719template<
typename T >
726template<
typename T >
732template<
typename T >
733QString DARowTable< T >::getName()
const
735 return (m_d.getName());
738template<
typename T >
743 for (SeriesPtr p : m_d) {
744 r.append(p->getName());
749template<
typename T >
752 const int s = ns.size();
754 for (
int i = 0; i < s; ++i) {
755 if (i < rowCount()) {
756 row(i)->setName(ns[ i ]);
761template<
typename T >
764 return (DA::takeByValue(*
this, field, value));
767template<
typename T >
773template<
typename T >
776 DA::orderBy(*
this, sn);
779template<
typename T >
782 DA::orderBy(*
this, rindex);
785template<
typename T >
788 const int r = nameToIndex(sortedfield);
790 return (lowerBound(v, r));
793template<
typename T >
796 typename DARowTable< T >::SeriesPtr prow = row(r);
797 SeriesIterator ite = std::lower_bound(prow->begin(), prow->end(), v);
799 if (ite == prow->end()) {
800 return (qMakePair< T, int >(prow->back(), prow->size() - 1));
802 size_t dis = std::distance(prow->begin(), ite);
804 return (qMakePair< T, int >(*ite, dis));
807template<
typename T >
808QPair< T, int > DARowTable< T >::upperBound(
const T& v,
const QString& sortedfield)
const
810 const int r = nameToIndex(sortedfield);
812 return (upperBound(v, r));
815template<
typename T >
816QPair< T, int > DARowTable< T >::upperBound(
const T& v,
int r)
const
818 typename DARowTable< T >::SeriesPtr prow = row(r);
819 SeriesIterator ite = std::upper_bound(prow->begin(), prow->end(), v);
821 if (ite == prow->end()) {
822 return (qMakePair< T, int >(prow->back(), prow->size() - 1));
824 size_t dis = std::distance(prow->begin(), ite);
826 return (qMakePair< T, int >(*ite, dis));
829template<
typename T >
832 m_caseSensitivity = cs;
835template<
typename T >
838 return (m_caseSensitivity == CaseSensitive);
841template<
typename T >
844 int r = nameToIndex(name);
851template<
typename T >
852void DARowTable< T >::remove(
int rindex)
855 if (0 == m_d.size()) {
861#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
862template<
typename T >
865 QDebugStateSaver saver(debug);
870 for (
const QString& r : rns) {
871 maxlen = qMax(maxlen, r.size());
874 for (
int i = 0; i < rs; ++i) {
875 typename DA::DARowTable< T >::SeriesPtr r = t.
row(i);
876 QString name = r->getName();
877 if (name.size() < maxlen) {
880 debug.noquote() << name <<
":";
883 for (
int j = 0; j < 5; ++j) {
884 debug.noquote() << r->at(j) <<
",";
886 debug.noquote() <<
" ...... ";
887 for (
int j = cs - 6; j < cs; ++j) {
888 debug.noquote() << r->at(j) <<
",";
891 for (
int j = 0; j < cs; ++j) {
892 debug << r->at(j) <<
",";
901template<
typename T >
904 QDebugStateSaver saver(debug);
909 for (
const QString& r : rns) {
910 maxlen = qMax(maxlen, r.size());
913 for (
int i = 0; i < rs; ++i) {
914 typename DA::DARowTable< T >::SeriesPtr r = t.
row(i);
915 QString name = r->getName();
916 if (name.size() < maxlen) {
919 debug.nospace() << name <<
":";
922 for (
int j = 0; j < 5; ++j) {
923 debug.nospace() << r->at(j) <<
",";
925 debug.nospace() <<
" ...... ";
926 for (
int j = cs - 6; j < cs; ++j) {
927 debug.nospace() << r->at(j) <<
",";
930 for (
int j = 0; j < cs; ++j) {
931 debug << r->at(j) <<
",";
以行为基础的数据表
Definition DARowTable.hpp:54
void clear()
清空
Definition DARowTable.hpp:720
T cell(int r, int c) const
获取单元格
Definition DARowTable.hpp:460
void orderBy(const QString &sn)
orderBy
Definition DARowTable.hpp:774
QStringList rowNames() const
rowNames
Definition DARowTable.hpp:739
int rowCount() const
表的行数
Definition DARowTable.hpp:696
Mode
Definition DARowTable.hpp:57
@ FixedMode
固定模式,表的列不会随着行的变化而变化(默认)
Definition DARowTable.hpp:58
@ ExpandMode
扩展模式,表的列是浮动的,如果插入一行比原来要宽,会自动扩充表的列数
Definition DARowTable.hpp:59
void setCaseSensitivity(CaseSensitivity cs)
设置名字查询时是否对大小写敏感
Definition DARowTable.hpp:830
SeriesPtr & row(int r)
获取行引用
Definition DARowTable.hpp:595
void resize(int r, int c)
改变table 的大小
Definition DARowTable.hpp:402
void setRowNames(const QStringList &ns)
设置行名,如果是个空的表会生成一个默认行
Definition DARowTable.hpp:750
CaseSensitivity
Definition DARowTable.hpp:62
@ CaseInsensitive
大小写不敏感
Definition DARowTable.hpp:63
@ CaseSensitive
大小写敏感
Definition DARowTable.hpp:64
int columnCount() const
表的列数
Definition DARowTable.hpp:702
QPair< T, int > lowerBound(const T &v, const QString &sortedfield) const
查找第一个大于或等于某个元素的位置
Definition DARowTable.hpp:786
QPair< QList< TablePtr >, QList< T > > groupBy(const QString &field) const
groupby
Definition DARowTable.hpp:768
TablePtr takeByValue(const QString &field, T value) const
提取某个值等于value作为新表
Definition DARowTable.hpp:762
void setMode(Mode m)
设置表格的模式
Definition DARowTable.hpp:708
Mode getMode() const
获取模式
Definition DARowTable.hpp:714
void fixSize()
以最大列数进行列数修正,保证所有行同列
Definition DARowTable.hpp:678
void fill(const T &v)
填充元素
Definition DARowTable.hpp:434
bool haveFieldid(const QString &field) const
判断是否存在field
Definition DARowTable.hpp:420
bool isCaseSensitivity() const
判断是否大小写敏感
Definition DARowTable.hpp:836
系列数据,系列允许设置名字
Definition da_data_table.hpp:19
Definition DARowTable.hpp:306
序列化类都是带异常的,使用中需要处理异常
Definition AppMainWindow.cpp:44
QPair< QList< typename DARowTable< T >::TablePtr >, QList< T > > groupby(const DARowTable< T > &table, const QString &field)
groupby 对某个字段执行group by操作
Definition DARowTable.hpp:280
std::shared_ptr< DAVector< _DAValueWithIndex< T > > > makeIndexSeries(const typename DADataTable< T >::SeriesPtr &p)
把序列转换为带序号的序列
Definition da_data_table.hpp:388