34 namespace ComparePTLeaf {
37 template <
typename PTType,
bool IsConst = false>
43 static double PT_TOL() {
return 1e-6; }
121 namespace ComparePTLeaf {
124 template <
typename T>
128 template <
typename T>
136 template <
typename T>
150 if (sub1A.sum() > sub1B.sum())
return true;
151 if (sub1A.sum() < sub1B.sum())
return false;
152 if (sub1A.max() > sub1B.max())
return true;
153 if (sub1A.max() < sub1B.max())
return false;
154 if (sub2A.sum() > sub2B.sum())
return true;
155 if (sub2A.sum() < sub2B.sum())
return false;
156 if (sub3A.sum() > sub3B.
sum())
return true;
157 if (sub3A.sum() < sub3B.
sum())
return false;
161 return std::abs(A.
val()) > std::abs(B.
val());
163 template <
typename T>
172 template <
typename T>
259 template <
typename CompareType>
264 template <
typename PTType,
bool IsConst>
281 template <
bool IsConst2>
286 template <
bool IsConst2>
288 return !((*this) == _it);
315 template <
typename PTType,
bool IsConst>
317 : m_curr_leaf_ptr(_it.leaf_ptr()) {}
321 template <
typename T>
323 while (size() <= i) {
326 return at(i) ? at(i) : at(i) =
new PTNode<T>(
this);
331 template <
typename T>
334 if (ind.
size() == 0 && home_trie.
depth() == 0 &&
335 this ==
static_cast<PTNode<T> *
>(&home_trie)) {
341 while (size() <= ind.
back()) {
346 if (!at(ind.
back())) {
351 return at(ind.
back());
356 template <
typename T>
363 bool remove_up(
true), not_removed(
true);
365 for (
Index i = 0; i < up_node->size() && (remove_up || not_removed); i++) {
366 if ((up_node->at(i)) ==
this) {
367 up_node->at(i) =
nullptr;
370 remove_up = remove_up && !(up_node->at(i));
384 template <
typename T>
386 for (
Index i = 0; i < size(); i++) {
387 if (at(i))
delete at(i);
393 template <
typename T>
396 *prev_leaf_addr =
this;
400 template <
typename T>
407 template <
typename T>
412 prev_leaf_addr = &insertion_ptr;
415 next_leaf = insertion_ptr;
423 (*prev_leaf_addr) =
this;
428 template <
typename T>
431 "PTLeaf insertion failed because 'next' leaf address is not in List.");
440 (*prev_leaf_addr) =
this;
447 template <
typename T>
453 if (prev_leaf_addr) *prev_leaf_addr = next_leaf;
459 prev_leaf_addr =
nullptr;
462 template <
typename T>
473 template <
typename T>
475 :
PTNode<T>(nullptr), m_depth(orig.m_depth), m_leaf_list(nullptr) {
479 for (; it != it_end; ++it)
set(it.key(), *it);
485 template <
typename T>
487 if (depth() == 0 && begin_ptr())
delete begin_ptr();
492 template <
typename T>
496 for (; it != it_end; ++it)
count++;
502 template <
typename T>
504 assert(ind.
size() == depth() &&
505 "In PolyTrie<T>::get(), ind.size() must match PolyTrie<T>::depth().");
506 if (ind.
size() == 0) {
507 if (begin_ptr())
return begin_ptr()->val();
513 if (ind[i] >= tnode->
size() || !(tnode->
at(ind[i]))) {
516 tnode = tnode->
at(ind[i]);
523 template <
typename T>
525 assert(ind.
size() == depth() &&
526 "In PolyTrie<T>::set(), ind.size() must match PolyTrie<T>::depth().");
533 for (
Index i = 0; i < ind.
size() - 1; i++) {
544 template <
typename T>
546 assert(ind.
size() == depth() &&
547 "In PolyTrie<T>::at(), ind.size() must match PolyTrie<T>::depth().");
550 for (
Index i = 0; i < ind.
size() - 1; i++) {
560 template <
typename T>
562 assert(ind.
size() == depth() &&
563 "In PolyTrie<T>::operator(), ind.size() must match "
564 "PolyTrie<T>::depth().");
567 for (
Index i = 0; i < ind.
size() - 1; i++) {
577 template <
typename T>
579 assert(ind.
size() == depth() &&
580 "In PolyTrie<T>::get(), ind.size() must match PolyTrie<T>::depth().");
581 if (ind.
size() == 0) {
582 if (begin_ptr())
return begin_ptr()->val();
588 if (ind[i] >= tnode->
size() || !(tnode->
at(ind[i]))) {
591 tnode = tnode->
at(ind[i]);
598 template <
typename T>
605 template <
typename T>
615 template <
typename T>
623 template <
typename T>
625 if (
this == &other)
return;
628 for (
Index i = 0; i < size(); i++) {
630 at(i)->up_node = &other;
635 for (
Index i = 0; i < size(); i++) {
637 at(i)->up_node =
this;
648 template <
typename T>
651 ind.
size() == depth() &&
652 "In PolyTrie<T>::remove(), ind.size() must match PolyTrie<T>::depth().");
655 if (ind[i] >= tnode->
size() || !(tnode->
at(ind[i]))) {
658 tnode = tnode->
at(ind[i]);
666 template <
typename T>
668 bool is_edited(
false);
670 for (; it != it_end; ++it) {
672 it.remove_and_next();
681 template <
typename T>
684 for (; it != it_end; ++it) {
686 out << it.key() <<
": " << *it <<
'\n';
692 template <
typename T>
700 for (; it != it_end; ++it) *it *= scale;
707 template <
typename T>
710 for (; it != it_end; ++it) {
719 template <
typename T>
722 for (; it != it_end; ++it) {
731 template <
typename T>
734 return result += RHS;
738 template <
typename T>
741 return result -= RHS;
746 template <
typename T>
748 return ((*
this) - RHS).almost_zero(tol);
753 template <
typename T>
760 template <
typename T>
764 for (; it != it_end; ++it) {
772 template <
typename T>
773 template <
typename CompareType>
775 if (!m_leaf_list)
return;
790 int merge_size(1), nmerges(2);
793 while (nmerges > 1) {
805 for (
int i = 0; i < merge_size; i++) {
807 if (!(b_ptr = b_ptr->
next()))
break;
815 while (a_size > 0 || (b_size > 0 && b_ptr)) {
820 b_ptr = b_ptr->
next();
822 }
else if (b_size == 0 || !b_ptr) {
825 a_ptr = a_ptr->
next();
827 }
else if (
compare(*a_ptr, *b_ptr)) {
831 a_ptr = a_ptr->
next();
837 b_ptr = b_ptr->
next();
Basic std::vector like container (deprecated)
PTNode< T > * & at(Index ind)
bool operator()(const PTLeaf< T > &A, const PTLeaf< T > &B) const
static bool compare(const PTLeaf< T > &A, const PTLeaf< T > &B)
bool operator()(const PTLeaf< T > &A, const PTLeaf< T > &B) const
static bool compare(const PTLeaf< T > &A, const PTLeaf< T > &B)
CASM_TMP::ConstSwitch< IsConst, typename PTType::leaf_type > leaf_type
CASM_TMP::ConstSwitch< IsConst, typename PTType::value_type > * pointer
PTIterator & remove_and_next()
leaf_type * leaf_ptr() const
bool operator==(const PTIterator< PTType, IsConst2 > &_it)
CASM_TMP::ConstSwitch< IsConst, typename PTType::value_type > & reference
bool operator!=(const PTIterator< PTType, IsConst2 > &_it)
PTIterator(leaf_type *_leaf_ptr=0)
PTIterator & operator++()
const Array< Index > & key()
PTIterator operator++(int)
leaf_type * m_curr_leaf_ptr
const T & val_ref() const
PTLeaf< T > ** prev_leaf_addr
PTLeaf< T > const * next() const
ComparePTLeaf::ByMonomialOrder CompareByMonomialOrder
PTLeaf(PTNode< T > *_up, const Array< Index > &_key, const T &_val)
const Array< Index > & key() const
PTNode(PTNode< T > *_up, const T &_val=0)
PTLeaf< T > * begin_ptr()
const_iterator end() const
void remove()
Virtual from PTNode<T>, you cannot remove a PolyTrie.
const_iterator begin() const
PTIterator< PolyTrie< T >, true > const_iterator
PTLeaf< T > const * begin_ptr() const
PTLeaf< T > * m_leaf_list
PTIterator< PolyTrie< T > > iterator
ReturnArray< T > sub_array(Index ind_begin, Index ind_end) const
void push_back(const PTNode< T > * &toPush)
PolyTrie operator-(const PolyTrie< T > &RHS) const
PolyTrie operator+(const PolyTrie< T > &RHS) const
void swap(PolyTrie< T > &other)
Efficient swap of this PolyTrie with another.
void insert_before(PTLeaf< T > *next)
bool prune_zeros(double tol=PTNode< T >::PT_TOL())
void list_leaf(PTLeaf< T > *new_leaf)
void swap_after_prev(PTLeaf< T > *other)
T operator()(const Array< Index > &ind) const
PTNode< T > * valid_node_at(Index i)
Index num_nonzero() const
bool operator==(const PolyTrie< T > &RHS) const
T get(const Array< Index > &ind) const
get() provides constant access does not change trie structure
void redefine(Index new_depth)
Clears all leaves and resets depth.
void insert_after(PTLeaf< T > *prev)
bool compare(const PolyTrie< T > &RHS, double tol=2 *PTNode< T >::PT_TOL()) const
void print_sparse(std::ostream &out) const
PolyTrie & operator*=(const T &scale)
void set(const Array< Index > &ind, const T &_val)
set() allows assignment that prunes the trie if _val is approximately 0;
bool almost_zero(double tol=2 *PTNode< T >::PT_TOL()) const
PTLeaf< T > * remove_and_next()
void sort_leaves(const CompareType &compare)
T & at(const Array< Index > &ind)
PolyTrie & operator+=(const PolyTrie< T > &RHS)
T & operator()(const Array< Index > &ind)
PolyTrie & operator-=(const PolyTrie< T > &RHS)
PTNode< T > * valid_leaf_at(PolyTrie< T > &home_trie, const Array< Index > &ind)
PolyTrie(const PolyTrie< T > &orig)
void insert_at(PTLeaf< T > *&insertion_ptr)
List insertion methods.
void remove(const Array< Index > &ind)
Remove entry at 'ind'.
typename std::conditional< IsConst, const T, T >::type ConstSwitch
bool almost_equal(ClusterInvariants const &A, ClusterInvariants const &B, double tol)
Check if ClusterInvariants are equal.
bool compare(ClusterInvariants const &A, ClusterInvariants const &B, double tol)
Compare ClusterInvariants.
void swap(ConfigDoF &A, ConfigDoF &B)
bool almost_zero(const T &val, double tol=TOL)
If T is not integral, use std::abs(val) < tol;.
INDEX_TYPE Index
For long integer indexing: