ADMB Documentation  11.4.2891
 All Classes Files Functions Variables Typedefs Friends Defines
ivector.h
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  *
00007  * ADModelbuilder and associated libraries and documentations are
00008  * provided under the general terms of the "New BSD" license
00009  *
00010  * License:
00011  *
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions are
00014  * met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright
00017  * notice, this list of conditions and the following disclaimer.
00018  *
00019  * 2.  Redistributions in binary form must reproduce the above copyright
00020  * notice, this list of conditions and the following disclaimer in the
00021  * documentation and/or other materials provided with the distribution.
00022  *
00023  * 3.  Neither the name of the  University of California, Otter Research,
00024  * nor the ADMB Foundation nor the names of its contributors may be used
00025  * to endorse or promote products derived from this software without
00026  * specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00031  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00032  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00033  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00034  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00035  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00036  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  */
00040 
00041 #ifndef __ADMB_IVECTOR_H__
00042 #define __ADMB_IVECTOR_H__
00043 
00044 #include "vector_shapex.h"
00045 
00048 class ivector
00049 {
00050 protected:
00051   int index_min;
00052   int index_max;
00053   int *v;
00054   vector_shapex *shape;
00055 
00056 public:
00057   ivector();
00058   ivector(const lvector&);
00059   ~ivector();
00060 
00061    int allocated() const
00062    {
00063       return (shape != NULL);
00064    }
00065    ivector& operator--()
00066    {
00067       index_min--;
00068       index_max--;
00069       v++;
00070       return *this;
00071    }
00072    void reallocate(double size);
00073 
00074    ivector& operator++()
00075    {
00076       index_min++;
00077       index_max++;
00078       v--;
00079       return *this;
00080    }
00081    int operator!() const
00082    {
00083       return (shape == NULL);
00084    }
00085 
00086    unsigned int get_ncopies() const
00087    {
00088      return shape ? shape->get_ncopies() : 0;
00089    }
00090 
00091    int& elem(int i)
00092    {
00093       return (v[i]);
00094    }
00095    const int& elem(int i) const
00096    {
00097       return v[i];
00098    }
00099    // returns the minimum allowable index
00100    int indexmin() const
00101    {
00102       return index_min;
00103    }
00104    // returns the maximum allowable index
00105    int indexmax() const
00106    {
00107       return index_max;
00108    }
00109    // returns the maximum allowable index
00110   int size() const
00111   {
00112     return v ? index_max - index_min + 1 : 0;
00113   }
00114    int *get_v() const
00115    {
00116       return v;
00117    }
00118    ivector& shift(int min);
00119    //ivector(unsigned int sz); //makes an array [0..sz]
00120    //inline allocated(void) {return ::allocated(this);}
00121 
00122    ivector(const dvector&);
00123 
00124    void fill(const char *s);
00125    void fill_seqadd(int, int);
00126    void fill_multinomial(const int& seed, const dvector& p);
00127    void fill_multinomial(const random_number_generator& rng,
00128      const dvector& p);
00129    ivector(int ncl, int ncu);
00130    ivector(const ad_integer& ncl, const index_type& ncu);
00131    void allocate(const ad_integer& ncl, const index_type& ncu);
00132    // makes an array [ncl..ncu]
00133 
00134    ivector(unsigned int sz, long int *x);
00135 
00136    void safe_allocate(int ncl, int ncu);
00137    void allocate(int ncl, int ncu);
00138    void allocate(const ivector& iv);
00139    void allocate(const dvector& dv);
00140    void allocate(const char *);
00141    void allocate();
00142    void deallocate();
00143    void safe_deallocate();
00144 
00145    //operator int* () {return v;}
00146 
00147    void save_ivector_position() const;
00148 
00149    void save_ivector_value() const;
00150 
00151    void write_on(const ostream& s) const;
00152 
00153    void read_from(const istream& s);
00154 
00155    void write_on(const uostream& s) const;
00156 
00157    void read_from(const uistream& s);
00158 
00159    int& operator[](int i);
00160    int& operator()(int i);
00161    const int& operator[](int i) const;
00162    const int& operator()(int i) const;
00163 
00164    ivector sub(int lb, int ub)
00165    {
00166       return preivector(this, lb, ub);
00167    }
00168    ivector operator()(int lb, int ub)
00169    {
00170       return preivector(this, lb, ub);
00171    }
00172    ivector(const preivector& pd);
00173 
00174    ivector operator()(const ivector& u);
00175 
00176    ivector(const ivector& t);
00177    void shallow_copy(const ivector& t);
00178 
00179    ivector& operator=(const ivector& t);
00180    ivector& operator=(int t);
00181 
00182    ivector& operator=(const lvector& t);
00183 
00184    void initialize(void);
00185    ivector& operator+=(const ivector& t);
00186    ivector& operator+=(int t);
00187 
00188   friend class vector_index;
00189   friend class imatrix;
00190   friend class i3_array;
00191 };
00192 
00193 #ifdef OPT_LIB
00194 inline int& ivector::operator[](int i)
00195 {
00196   return *(v + i);
00197 }
00198 inline int& ivector::operator()(int i)
00199 {
00200   return *(v + i);
00201 }
00202 inline const int& ivector::operator[](int i) const
00203 {
00204   return *(v + i);
00205 }
00206 inline const int& ivector::operator()(int i) const
00207 {
00208   return *(v + i);
00209 }
00210 #endif
00211 
00212 ivector operator+(const ivector& v, const ivector& w);
00213 ivector operator-(const ivector& v, const ivector& w);
00214 ivector operator+(const ivector& v, int w);
00215 ivector operator-(const ivector& v, int w);
00216 ivector operator+(int v, const ivector& w);
00217 ivector operator-(int v, const ivector& w);
00218 
00219 #endif