ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ivector.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  *
7  * ADModelbuilder and associated libraries and documentations are
8  * provided under the general terms of the "New BSD" license
9  *
10  * License:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are
14  * met:
15  *
16  * 1. Redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer.
18  *
19  * 2. Redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in the
21  * documentation and/or other materials provided with the distribution.
22  *
23  * 3. Neither the name of the University of California, Otter Research,
24  * nor the ADMB Foundation nor the names of its contributors may be used
25  * to endorse or promote products derived from this software without
26  * specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 #ifndef __ADMB_IVECTOR_H__
42 #define __ADMB_IVECTOR_H__
43 
44 #include "vector_shapex.h"
45 #include "gradient_structure.h"
46 
50 class ivector
51 {
52 protected:
53  int index_min;
54  int index_max;
55  int *v;
57 
58 public:
59  ivector();
60  ivector(const lvector&);
61  ~ivector();
62 
63  int allocated() const
64  {
65  return (shape != NULL);
66  }
68  {
69  index_min--;
70  index_max--;
71  v++;
72  return *this;
73  }
74  void reallocate(double size);
75 
77  {
78  index_min++;
79  index_max++;
80  v--;
81  return *this;
82  }
83  int operator!() const
84  {
85  return (shape == NULL);
86  }
87 
88  unsigned int get_ncopies() const { return shape ? shape->get_ncopies() : 0; }
89 
90  int& elem(int i)
91  {
92  return (v[i]);
93  }
94  const int& elem(int i) const
95  {
96  return v[i];
97  }
98  // returns the minimum allowable index
99  int indexmin() const
100  {
101  return index_min;
102  }
103  // returns the maximum allowable index
104  int indexmax() const
105  {
106  return index_max;
107  }
108  // returns the maximum allowable index
109  unsigned int size() const
110  {
111  return static_cast<unsigned int>(
112  index_max < index_min ? 0 : index_max - index_min + 1);
113  }
114  int* get_v() const
115  {
116  return v;
117  }
118  ivector& shift(int min);
119  //ivector(unsigned int sz); //makes an array [0..sz]
120  //inline allocated(void) {return ::allocated(this);}
121 
122  ivector(const dvector&);
123 
124  void fill(const char *s);
125  void fill_seqadd(int, int);
126  void fill_multinomial(const int& seed, const dvector& p);
128  const dvector& p);
129  ivector(int ncl, int ncu);
130  ivector(const ad_integer& ncl, const index_type& ncu);
131  void allocate(const ad_integer& ncl, const index_type& ncu);
132  // makes an array [ncl..ncu]
133 
134  ivector(unsigned int sz, long int *x);
135 
136  void safe_allocate(int ncl, int ncu);
137  void allocate(int ncl, int ncu);
138  void allocate(const ivector& iv);
139  void allocate(const dvector& dv);
140  void allocate(const char *);
141  void allocate();
142  void deallocate();
143  void safe_deallocate();
144 
145  //operator int* () {return v;}
146 
147  void save_ivector_position() const;
148 
149  void save_ivector_value() const;
150 
151  void write_on(const ostream& s) const;
152 
153  void read_from(const istream& s);
154 
155  void write_on(const uostream& s) const;
156 
157  void read_from(const uistream& s);
158 
159  int& operator[](int i);
160  int& operator()(int i);
161  const int& operator[](int i) const;
162  const int& operator()(int i) const;
163 
164  ivector sub(int lb, int ub)
165  {
166  return preivector(this, lb, ub);
167  }
168  ivector operator()(int lb, int ub)
169  {
170  return preivector(this, lb, ub);
171  }
172  ivector(const preivector& pd);
173 
174  ivector operator()(const ivector& u);
175 
176  ivector(const ivector& t);
177  void shallow_copy(const ivector& t);
178 
179  ivector& operator=(const ivector& t);
180  ivector& operator=(int t);
181 
182  ivector& operator=(const lvector& t);
183 
184  void initialize(void);
185  ivector& operator+=(const ivector& t);
186  ivector& operator+=(int t);
187 
188  friend class vector_index;
189  friend class imatrix;
190  friend class i3_array;
191 };
192 
193 #ifdef OPT_LIB
194 inline int& ivector::operator[](int i)
195 {
196  return *(v + i);
197 }
198 inline int& ivector::operator()(int i)
199 {
200  return *(v + i);
201 }
202 inline const int& ivector::operator[](int i) const
203 {
204  return *(v + i);
205 }
206 inline const int& ivector::operator()(int i) const
207 {
208  return *(v + i);
209 }
210 #endif
211 
212 ivector operator+(const ivector& v, const ivector& w);
213 ivector operator-(const ivector& v, const ivector& w);
214 ivector operator+(const ivector& v, int w);
215 ivector operator-(const ivector& v, int w);
216 ivector operator+(int v, const ivector& w);
217 ivector operator-(int v, const ivector& w);
218 
219 #endif
int & elem(int i)
Definition: ivector.h:90
void safe_deallocate()
Safely deallocates memory by reporting if shallow copies are still in scope.
Definition: ivector.cpp:119
unsigned int get_ncopies(void)
Definition: vector_shapex.h:89
void save_ivector_position() const
Description not yet available.
Definition: cmpdif3.cpp:194
int allocated() const
Definition: ivector.h:63
Uses polymorphism to get index information from various data types to be used in constructing and all...
Definition: fvar.hpp:7731
ivector & operator--()
Definition: ivector.h:67
ivector()
Default constructor.
Definition: ivector.cpp:37
unsigned int size() const
Definition: ivector.h:109
Description not yet available.
Definition: imatrix.h:69
ivector sub(int lb, int ub)
Definition: ivector.h:164
d3_array operator-(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements addition of a(i, j, k) + b(i, j, k).
Definition: d3arr2a.cpp:152
#define x
Vector of double precision numbers.
Definition: dvector.h:50
~ivector()
Default destructor.
Definition: ivector.cpp:70
Derived class of index types for ivector.
Definition: fvar.hpp:7809
void read_from(const istream &s)
Reads all the elements from istream _s.
Definition: ivect_io.cpp:93
void fill_multinomial(const int &seed, const dvector &p)
Description not yet available.
Definition: dvect11.cpp:50
d3_array operator+(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements addition of a(i, j, k) + b(i, j, k).
Definition: d3arr2a.cpp:132
Description not yet available.
Definition: fvar.hpp:656
int & operator[](int i)
Definition: ivector.h:194
void fill_seqadd(int, int)
Fills ivector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:73
ivector & operator++()
Definition: ivector.h:76
unsigned int get_ncopies() const
Definition: ivector.h:88
ivector & operator+=(const ivector &t)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: ivec9.cpp:13
int * get_v() const
Definition: ivector.h:114
ivector & operator=(const ivector &t)
Description not yet available.
Definition: ivector.cpp:164
Description not yet available.
Definition: fvar.hpp:7951
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
const int & elem(int i) const
Definition: ivector.h:94
int operator!() const
Definition: ivector.h:83
vector_shapex * shape
Definition: ivector.h:56
ivector operator()(int lb, int ub)
Definition: ivector.h:168
int index_max
Definition: ivector.h:54
#define min(a, b)
Definition: cbivnorm.cpp:188
Description not yet available.
Definition: fvar.hpp:3398
int & operator()(int i)
Definition: ivector.h:198
Description not yet available.
Definition: fvar.hpp:622
int indexmin() const
Definition: ivector.h:99
int indexmax() const
Definition: ivector.h:104
void allocate()
Does not allocate, but initializes class members.
Definition: ivector.cpp:316
void initialize(void)
Description not yet available.
Definition: ivec2.cpp:17
Holds &quot;shape&quot; information for vector objects.
Definition: vector_shapex.h:46
void reallocate(double size)
Reallocate size of array.
Definition: ivect11.cpp:14
void deallocate()
Deallocate i3_array memory.
Definition: ivector.cpp:91
ivector & shift(int min)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: ivec.cpp:17
#define w
void write_on(const ostream &s) const
Writes all the elements to the ostream _s.
Definition: ivect_io.cpp:42
Description not yet available.
Definition: fvar.hpp:3516
int * v
Definition: ivector.h:55
void fill(const char *s)
Description not yet available.
Definition: ivec4.cpp:17
void safe_allocate(int ncl, int ncu)
Description not yet available.
Definition: ivector.cpp:78
Stores integer.
Definition: fvar.hpp:7654
Description not yet available.
Definition: fvar.hpp:3944
void save_ivector_value() const
Saves the size, address, and value information for a ivector.
Definition: cmpdif4.cpp:148
void shallow_copy(const ivector &t)
Shallow copy other data structure pointers.
Definition: ivector.cpp:136
int index_min
Definition: ivector.h:53