Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Heap.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
4 //
5 // Copyright 2009 NTESS and the Ifpack2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef IFPACK2_HEAP_HPP
11 #define IFPACK2_HEAP_HPP
12 
13 #include <algorithm>
14 #include "Teuchos_Array.hpp"
15 #include "Teuchos_ScalarTraits.hpp"
16 
17 namespace Ifpack2 {
18 
19 template <typename Scalar, typename Ordinal>
20 struct greater_indirect {
21  greater_indirect(const Teuchos::Array<Scalar>& vals)
22  : m_vals(vals) {}
23  ~greater_indirect() {}
24 
25  bool operator()(const Ordinal& lhs, const Ordinal& rhs) const { return Teuchos::ScalarTraits<Scalar>::magnitude(m_vals[lhs]) >
27 
28  private:
29  const Teuchos::Array<Scalar>& m_vals;
30 }; // struct greater_indirect
31 
34 template <typename Ordinal, typename SizeType>
35 void add_to_heap(const Ordinal& idx, Teuchos::Array<Ordinal>& heap, SizeType& heap_len) {
36  if (heap.size() == heap_len)
37  heap.push_back(idx);
38  else
39  heap[heap_len] = idx;
40  ++heap_len;
41  std::push_heap(heap.begin(), heap.begin() + heap_len, std::greater<Ordinal>());
42 }
43 
47 template <typename Ordinal, typename SizeType, class Compare>
48 void add_to_heap(const Ordinal& idx, Teuchos::Array<Ordinal>& heap, SizeType& heap_len, Compare comp) {
49  if (heap.size() == heap_len)
50  heap.push_back(idx);
51  else
52  heap[heap_len] = idx;
53  ++heap_len;
54  std::push_heap(heap.begin(), heap.begin() + heap_len, comp);
55 }
56 
58 template <typename Ordinal, typename SizeType>
59 void rm_heap_root(Teuchos::Array<Ordinal>& heap, SizeType& heap_len) {
60  std::pop_heap(heap.begin(), heap.begin() + heap_len, std::greater<Ordinal>());
61  --heap_len;
62 }
63 
67 template <typename Ordinal, typename SizeType, class Compare>
68 void rm_heap_root(Teuchos::Array<Ordinal>& heap, SizeType& heap_len, Compare comp) {
69  std::pop_heap(heap.begin(), heap.begin() + heap_len, comp);
70  --heap_len;
71 }
72 
73 } // namespace Ifpack2
74 
75 #endif
void rm_heap_root(Teuchos::Array< Ordinal > &heap, SizeType &heap_len)
Definition: Ifpack2_Heap.hpp:59
static magnitudeType magnitude(T a)
void push_back(const value_type &x)
size_type size() const
void add_to_heap(const Ordinal &idx, Teuchos::Array< Ordinal > &heap, SizeType &heap_len)
Definition: Ifpack2_Heap.hpp:35
iterator begin()