sparse_vector.h
Go to the documentation of this file.
1 //LIC// ====================================================================
2 //LIC// This file forms part of oomph-lib, the object-oriented,
3 //LIC// multi-physics finite-element library, available
4 //LIC// at http://www.oomph-lib.org.
5 //LIC//
6 //LIC// Version 1.0; svn revision $LastChangedRevision: 1097 $
7 //LIC//
8 //LIC// $LastChangedDate: 2015-12-17 11:53:17 +0000 (Thu, 17 Dec 2015) $
9 //LIC//
10 //LIC// Copyright (C) 2006-2016 Matthias Heil and Andrew Hazel
11 //LIC//
12 //LIC// This library is free software; you can redistribute it and/or
13 //LIC// modify it under the terms of the GNU Lesser General Public
14 //LIC// License as published by the Free Software Foundation; either
15 //LIC// version 2.1 of the License, or (at your option) any later version.
16 //LIC//
17 //LIC// This library is distributed in the hope that it will be useful,
18 //LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 //LIC// Lesser General Public License for more details.
21 //LIC//
22 //LIC// You should have received a copy of the GNU Lesser General Public
23 //LIC// License along with this library; if not, write to the Free Software
24 //LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 //LIC// 02110-1301 USA.
26 //LIC//
27 //LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
28 //LIC//
29 //LIC//====================================================================
30 #ifndef SPARSE_VECTOR_HEADER
31 #define SPARSE_VECTOR_HEADER
32 
33 
34 namespace oomph
35 {
36 
37 
38 //========================================================================
39 /// Sparse vector -- wrapper for map (but avoids filling map during
40 /// reads of zero/empty entries). Square bracket operator is read-only.
41 //========================================================================
42 template<class T>
43 class SparseVector
44 {
45 
46  public:
47  /// Constructor
48  SparseVector()
49  {
50  // Empty instance
51  Empty_pt = new T(0);
52  }
53 
54  /// Destructor
55  ~SparseVector()
56  {
57  // Delete empty instance
58  delete Empty_pt;
59  }
60 
61  // Initialise the bin (only called by the create_bins_of_objects()
62  // method)
63  void initialise(const unsigned& size)
64  {
65  // Create a "large" bool vector indicating all entries are empty
66  Has_entry.resize(size, false);
67  }
68 
69  /// Wipe storage
70  void clear()
71  {
72  Data.clear();
73 
74  // Get current size and reset all entries
75  const unsigned size = Has_entry.size();
76  Has_entry.resize(size, false);
77  }
78 
79  /// Square bracket access (const version)
80  const T& operator[](const unsigned& i) const
81  {
82  typedef typename std::map<unsigned, T>::const_iterator IT;
83  IT it = Data.find(i);
84  if (it == Data.end())
85  {
86  return *Empty_pt;
87  }
88  return (*it).second;
89  }
90 
91  /// Set value of i-th entry
92  void set_value(const unsigned& i, const T& value)
93  {
94  Data[i] = value;
95  // Mark as having entry
96  Has_entry[i] = true;
97  }
98 
99  /// \short Number of nonzero entries stored in here (specifying the
100  /// size of the vector (as a mathematical object)
101  /// doesn't really make sense -- the thing could be infinitely
102  /// big and we wouldn't know or care)
103  unsigned nnz() const
104  {
105  return Data.size();
106  }
107 
108  /// Read-only access to underlying map
109  const std::map<unsigned,T>* map_pt() const
110  {
111  return &Data;
112  }
113 
114  /// Read/write access to underlying map -- dangerous!
115  std::map<unsigned,T>* map_pt()
116  {
117  return &Data;
118  }
119 
120  /// \short Check if the bin has entries
121  bool has_entry(const unsigned &nbin)
122  {
123  return Has_entry[nbin];
124  }
125 
126  /// \short Return vector containing all values
127  /// (without reference to their specific indices)
128  /// for fast direct access
129  void get_all_values(Vector<T>& all_values) const
130  {
131  all_values.clear();
132  all_values.resize(nnz());
133  typedef typename std::map<unsigned, T>::const_iterator IT;
134  unsigned count = 0;
135  for (IT it = Data.begin(); it != Data.end(); it++)
136  {
137  all_values[count++] = (*it).second;
138  }
139  }
140 
141  private:
142 
143  /// Internal storage in map.
144  std::map<unsigned, T> Data;
145 
146  /// Empty instance
147  const T* Empty_pt;
148 
149  /// Keep track of the filled and empty bins
150  std::vector<bool> Has_entry;
151 
152 };
153 
154 
155 
156 
157 #endif
cstr elem_len * i
Definition: cfortran.h:607