ug4
sparse_vector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef SPARSE_VECTOR_H_
34 #define SPARSE_VECTOR_H_
35 
36 #include <map>
37 namespace ug{
38 
39 template<typename T>
41 {
42  size_t m_size;
43  typedef std::map<size_t, T> container;
45 public:
46  typedef T value_type;
47  class const_iterator : public container::const_iterator
48  {
49  using container::const_iterator::operator *;
50  public:
51  const_iterator(typename container::const_iterator it) : container::const_iterator(it) {}
52  const T &value() const { return (operator *()).second; }
53  size_t index() const { return (operator *()).first; }
54  };
55 
56  class iterator : public container::iterator
57  {
58  using container::iterator::operator *;
59  public:
60  iterator(typename container::iterator it) : container::iterator(it) {}
61  const T &value() const { return (operator *()).second; }
62  T &value() { return (operator *())->second; }
63  size_t index() const { return (operator *()).first; }
64  };
65 
66  SparseVector(size_t s) : m_size(s)
67  {
68  }
70  {
71  const_iterator c(data.begin());
72  return c;
73  }
75  {
76  return const_iterator(data.end());
77  }
78 
79  const T &operator()(size_t c) const
80  {
81  assert(c < m_size);
82  return data[c];
83  }
84  T &operator()(size_t c)
85  {
86  assert(c < m_size);
87  return data[c];
88  }
89  bool has_connection(size_t c) const
90  {
91  return data.find(c) != data.end();
92  }
93 
94  size_t size()
95  {
96  return m_size;
97  }
98  size_t num_connections() const
99  {
100  return data.size();
101  }
102 
103  void print() const
104  {
105  for(const_iterator it=begin(); it != end(); ++it)
106  {
107  //if(BlockNorm(it.value()) == 0.0) continue;
108  UG_LOG("(" << it.index() << " -> " << it.value() << ")");
109  }
110 
111  UG_LOG("\n");
112  }
113 
114 };
115 
116 }
117 #endif /* SPARSE_VECTOR_H_ */
parameterString s
Definition: sparse_vector.h:48
const T & value() const
Definition: sparse_vector.h:52
size_t index() const
Definition: sparse_vector.h:53
const_iterator(typename container::const_iterator it)
Definition: sparse_vector.h:51
Definition: sparse_vector.h:57
size_t index() const
Definition: sparse_vector.h:63
iterator(typename container::iterator it)
Definition: sparse_vector.h:60
T & value()
Definition: sparse_vector.h:62
const T & value() const
Definition: sparse_vector.h:61
Definition: sparse_vector.h:41
const_iterator begin() const
Definition: sparse_vector.h:69
container data
Definition: sparse_vector.h:44
bool has_connection(size_t c) const
Definition: sparse_vector.h:89
void print() const
Definition: sparse_vector.h:103
size_t size()
Definition: sparse_vector.h:94
T & operator()(size_t c)
Definition: sparse_vector.h:84
SparseVector(size_t s)
Definition: sparse_vector.h:66
const T & operator()(size_t c) const
Definition: sparse_vector.h:79
const_iterator end() const
Definition: sparse_vector.h:74
size_t m_size
Definition: sparse_vector.h:42
std::map< size_t, T > container
Definition: sparse_vector.h:43
T value_type
Definition: sparse_vector.h:46
size_t num_connections() const
Definition: sparse_vector.h:98
#define UG_LOG(msg)
Definition: log.h:367
the ug namespace