Xmipp  v3.23.11-Nereus
Classes | Public Member Functions | List of all members
cif::category_index Class Reference

Public Member Functions

 category_index (category *cat)
 
 ~category_index ()
 
row * find (row *k) const
 
row * find_by_value (row_initializer k) const
 
void insert (row *r)
 
void erase (row *r)
 
void reconstruct ()
 
std::tuple< row *, row * > reorder ()
 
size_t size () const
 

Detailed Description

Definition at line 139 of file category.cpp.

Constructor & Destructor Documentation

◆ category_index()

cif::category_index::category_index ( category *  cat)
inline

Definition at line 142 of file category.cpp.

143  : m_category(*cat)
144  , m_row_comparator(m_category)
145  , m_root(nullptr)
146  {
147  reconstruct();
148  }

◆ ~category_index()

cif::category_index::~category_index ( )
inline

Definition at line 150 of file category.cpp.

151  {
152  delete m_root;
153  }

Member Function Documentation

◆ erase()

void cif::category_index::erase ( row *  r)

Definition at line 435 of file category.cpp.

436 {
437  assert(find(k) == k);
438 
439  m_root = erase(m_root, k);
440  if (m_root != nullptr)
441  m_root->m_red = false;
442 }
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
void erase(row *r)
Definition: category.cpp:435
row * find(row *k) const
Definition: category.cpp:345

◆ find()

row * cif::category_index::find ( row *  k) const

Definition at line 345 of file category.cpp.

346 {
347  const entry *r = m_root;
348  while (r != nullptr)
349  {
350  int d = m_row_comparator(k, r->m_row);
351  if (d < 0)
352  r = r->m_left;
353  else if (d > 0)
354  r = r->m_right;
355  else
356  break;
357  }
358 
359  return r ? r->m_row : nullptr;
360 }
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
doublereal * d

◆ find_by_value()

row * cif::category_index::find_by_value ( row_initializer  k) const

Definition at line 362 of file category.cpp.

363 {
364  // sort the values in k first
365 
366  row_initializer k2;
367  for (auto &f : m_category.key_field_indices())
368  {
369  auto fld = m_category.get_column_name(f);
370 
371  auto ki = find_if(k.begin(), k.end(), [&fld](auto &i) { return i.name() == fld; });
372  if (ki == k.end())
373  k2.emplace_back(fld, "");
374  else
375  k2.emplace_back(*ki);
376  }
377 
378  const entry *r = m_root;
379  while (r != nullptr)
380  {
381  int d = m_row_comparator(k2, r->m_row);
382  if (d < 0)
383  r = r->m_left;
384  else if (d > 0)
385  r = r->m_right;
386  else
387  break;
388  }
389 
390  return r ? r->m_row : nullptr;
391 }
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
doublereal * d
double * f

◆ insert()

void cif::category_index::insert ( row *  r)

Definition at line 393 of file category.cpp.

394 {
395  m_root = insert(m_root, k);
396  m_root->m_red = false;
397 }
void insert(row *r)
Definition: category.cpp:393
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac

◆ reconstruct()

void cif::category_index::reconstruct ( )

Definition at line 485 of file category.cpp.

486 {
487  delete m_root;
488  m_root = nullptr;
489 
490  for (auto r : m_category)
491  insert(r.get_row());
492 
493  // maybe reconstruction can be done quicker by using the following commented code.
494  // however, I've not had the time to think of a way to set the red/black flag correctly in that case.
495 
496  // std::vector<row*> rows;
497  // transform(mCat.begin(), mCat.end(), backInserter(rows),
498  // [](Row r) -> row* { assert(r.mData); return r.mData; });
499  //
500  // assert(std::find(rows.begin(), rows.end(), nullptr) == rows.end());
501  //
502  // // don't use sort here, it will run out of the stack of something.
503  // // quicksort is notorious for using excessive recursion.
504  // // Besides, most of the time, the data is ordered already anyway.
505  //
506  // stable_sort(rows.begin(), rows.end(), [this](row* a, row* b) -> bool { return this->mComp(a, b) < 0; });
507  //
508  // for (size_t i = 0; i < rows.size() - 1; ++i)
509  // assert(mComp(rows[i], rows[i + 1]) < 0);
510  //
511  // deque<entry*> e;
512  // transform(rows.begin(), rows.end(), back_inserter(e),
513  // [](row* r) -> entry* { return new entry(r); });
514  //
515  // while (e.size() > 1)
516  // {
517  // deque<entry*> ne;
518  //
519  // while (not e.empty())
520  // {
521  // entry* a = e.front();
522  // e.pop_front();
523  //
524  // if (e.empty())
525  // ne.push_back(a);
526  // else
527  // {
528  // entry* b = e.front();
529  // b->mLeft = a;
530  //
531  // assert(mComp(a->mRow, b->mRow) < 0);
532  //
533  // e.pop_front();
534  //
535  // if (not e.empty())
536  // {
537  // entry* c = e.front();
538  // e.pop_front();
539  //
540  // assert(mComp(b->mRow, c->mRow) < 0);
541  //
542  // b->mRight = c;
543  // }
544  //
545  // ne.push_back(b);
546  //
547  // if (not e.empty())
548  // {
549  // ne.push_back(e.front());
550  // e.pop_front();
551  // }
552  // }
553  // }
554  //
555  // swap (e, ne);
556  // }
557  //
558  // assert(e.size() == 1);
559  // mRoot = e.front();
560 }
void insert(row *r)
Definition: category.cpp:393

◆ reorder()

std::tuple<row *, row *> cif::category_index::reorder ( )
inline

Definition at line 165 of file category.cpp.

166  {
167  std::tuple<row *, row *> result = std::make_tuple(nullptr, nullptr);
168 
169  if (m_root != nullptr)
170  {
171  entry *head = find_min(m_root);
172  entry *tail = reorder(m_root);
173 
174  tail->m_row->m_next = nullptr;
175 
176  result = std::make_tuple(head->m_row, tail->m_row);
177  }
178 
179  return result;
180  }
std::tuple< row *, row * > reorder()
Definition: category.cpp:165

◆ size()

size_t cif::category_index::size ( ) const

Definition at line 562 of file category.cpp.

563 {
564  std::stack<entry *> s;
565  s.push(m_root);
566 
567  size_t result = 0;
568 
569  while (not s.empty())
570  {
571  entry *e = s.top();
572  s.pop();
573 
574  if (e == nullptr)
575  continue;
576 
577  ++result;
578 
579  s.push(e->m_left);
580  s.push(e->m_right);
581  }
582 
583  return result;
584 }

The documentation for this class was generated from the following file: