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