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
26  { return Teuchos::ScalarTraits<Scalar>::magnitude(m_vals[lhs]) >
28 
29 private:
30  const Teuchos::Array<Scalar>& m_vals;
31 };//struct greater_indirect
32 
33 
36 template<typename Ordinal, typename SizeType>
37 void add_to_heap(const Ordinal& idx, Teuchos::Array<Ordinal>& heap, SizeType& heap_len)
38 {
39  if (heap.size() == heap_len) heap.push_back(idx);
40  else heap[heap_len] = idx;
41  ++heap_len;
42  std::push_heap(heap.begin(), heap.begin()+heap_len, std::greater<Ordinal>());
43 }
44 
48 template<typename Ordinal, typename SizeType, class Compare>
49 void add_to_heap(const Ordinal& idx, Teuchos::Array<Ordinal>& heap, SizeType& heap_len, Compare comp)
50 {
51  if (heap.size() == heap_len) heap.push_back(idx);
52  else 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 {
61  std::pop_heap(heap.begin(), heap.begin()+heap_len, std::greater<Ordinal>());
62  --heap_len;
63 }
64 
68 template<typename Ordinal, typename SizeType, class Compare>
69 void rm_heap_root(Teuchos::Array<Ordinal>& heap, SizeType& heap_len, Compare comp)
70 {
71  std::pop_heap(heap.begin(), heap.begin()+heap_len, comp);
72  --heap_len;
73 }
74 
75 }//namespace Ifpack2
76 
77 #endif
78 
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:37
iterator begin()