HermesCommon  2.0
vector.h
Go to the documentation of this file.
1 // This file is part of HermesCommon
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef __HERMES_COMMON_TUPLE_H
23 #define __HERMES_COMMON_TUPLE_H
24 
25 #include <vector>
26 #include <stdexcept> // for exception, runtime_error, out_of_range
27 #include <typeinfo>
28 #include <sstream>
29 #include <stdio.h>
30 #include "exceptions.h"
31 
32 namespace Hermes
33 {
35 
42  template<typename T>
43  class vector : public std::vector<T>
44  {
45  public:
47  vector() { };
49  vector(int size) { this->reserve(size); };
52  //vector(const T& a) { this->reserve(1); this->push_back(a);};
54  vector(const T& a, const T& b) { this->reserve(2); this->push_back(a); this->push_back(b); };
56  vector(const T& a, const T& b, const T& c) { this->reserve(3); this->push_back(a); this->push_back(b); this->push_back(c); };
58  vector(const T& a, const T& b, const T& c, const T& d) { this->reserve(4); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); };
60  vector(const T& a, const T& b, const T& c, const T& d, const T& e) { this->reserve(5); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); this->push_back(e); };
62  vector(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f) { this->reserve(6); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); this->push_back(e); this->push_back(f); };
64  vector(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f, const T& g) { this->reserve(7); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); this->push_back(e); this->push_back(f); this->push_back(g); };
66  vector(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f, const T& g, const T& h) { this->reserve(8); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); this->push_back(e); this->push_back(f); this->push_back(g); this->push_back(h); };
68  vector(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f, const T& g, const T& h, const T& i) { this->reserve(9); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); this->push_back(e); this->push_back(f); this->push_back(g); this->push_back(h); this->push_back(i); };
70  vector(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f, const T& g, const T& h, const T& i, const T& j) { this->reserve(10); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); this->push_back(e); this->push_back(f); this->push_back(g); this->push_back(h); this->push_back(i); this->push_back(j); };
72  vector(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f, const T& g, const T& h, const T& i, const T& j, const T& k) { this->reserve(11); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); this->push_back(e); this->push_back(f); this->push_back(g); this->push_back(h); this->push_back(i); this->push_back(j); this->push_back(k);};
74  vector(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f, const T& g, const T& h, const T& i, const T& j, const T& k, const T& l) { this->reserve(12); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); this->push_back(e); this->push_back(f); this->push_back(g); this->push_back(h); this->push_back(i); this->push_back(j); this->push_back(k); this->push_back(l);};
76  vector(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f, const T& g, const T& h, const T& i, const T& j, const T& k, const T& l, const T& m) { this->reserve(13); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); this->push_back(e); this->push_back(f); this->push_back(g); this->push_back(h); this->push_back(i); this->push_back(j); this->push_back(k); this->push_back(l); this->push_back(m);};
78  vector(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f, const T& g, const T& h, const T& i, const T& j, const T& k, const T& l, const T& m, const T& n) { this->reserve(14); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); this->push_back(e); this->push_back(f); this->push_back(g); this->push_back(h); this->push_back(i); this->push_back(j); this->push_back(k); this->push_back(l); this->push_back(m); this->push_back(n);};
80  vector(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f, const T& g, const T& h, const T& i, const T& j, const T& k, const T& l, const T& m, const T& n, const T& o) { this->reserve(15); this->push_back(a); this->push_back(b); this->push_back(c); this->push_back(d); this->push_back(e); this->push_back(f); this->push_back(g); this->push_back(h); this->push_back(i); this->push_back(j); this->push_back(k); this->push_back(l); this->push_back(m); this->push_back(n); this->push_back(o);};
81 
82  // Look up an integer number in an array.
83  int find_index_slow(const T& x) {
84  for (int i=0; i < this->size(); i++) {
85  if((*this)[i] == x)
86  return i;
87  }
88  throw Hermes::Exceptions::Exception("Index not found");
89  }
90 
91  // Returns maximum of the vector<T> in case of T == int.
92  int max() {
93  if(this->size() == 0)
94  throw Hermes::Exceptions::Exception("Empty vector");
95  int m;
96  if(typeid((*this)[0]) != typeid(m))
97  throw Hermes::Exceptions::Exception("vector<T>::max() called and T != int.");
98  m = (int)(*this)[0];
99  for (unsigned int i=1; i < this->size(); i++)
100  if((int)(*this)[i] > m)
101  m = (int)(*this)[i];
102  return m;
103  }
104 
105  // Returns minimum of the vector<T> in case of T == int.
106  int min() {
107  if(this->size() == 0)
108  throw Hermes::Exceptions::Exception("Empty vector");
109  int m;
110  if(typeid((*this)[0]) != typeid(m))
111  throw Hermes::Exceptions::Exception("vector<T>::max() called and T != int.");
112  m = (int)(*this)[0];
113  for (unsigned int i=1; i < this->size(); i++)
114  if((int)(*this)[i] < m)
115  m = (int)(*this)[i];
116  return m;
117  }
118 
119  // Look up an integer number in an array.
120  // This prepares a permut array, so subsequent calls are very fast
121  int find_index(int x, bool throw_exception=true) {
122  if(this->size() == 0) {
123  if(throw_exception) {
124  throw Hermes::Exceptions::Exception("Empty vector");
125  }
126  else return -1;
127  }
128  int idx;
129  if(typeid((*this)[0]) != typeid(idx))
130  throw Hermes::Exceptions::Exception("vector<T>::find_index() called and T != int.");
131 
132  if(this->_permut.size() == 0) {
133  // Initialize the permut array
134  this->_min = this->min();
135  this->_max = this->max();
136  for (int i=0; i < (int)this->_max+1; i++) this->_permut.push_back(-1);
137  for (unsigned int i=0; i < this->size(); i++) this->_permut[(int)(*this)[i]] = i;
138  }
139  if(((int)this->_min <= x) && (x <= (int)this->_max))
140  idx = this->_permut[x];
141  else
142  idx = -1;
143  if(idx == -1) {
144  if(throw_exception)
145  throw Hermes::Exceptions::Exception("Index in the vector not found");
146  else
147  return -1;
148  }
149  return idx;
150  }
151 
152  void print() {
153  printf("[");
154  for (int i=0; i < this->size(); i++) printf("%d ", (*this)[i]);
155  printf("]\n");
156  }
157 
158  private:
159  std::vector<int> _permut;
160  int _min, _max;
161  };
162 } // namespace Hermes
163 
164 #endif