ADMB Documentation  11.1.2490
 All Classes Files Functions Variables Typedefs Friends Defines
ivector.h
Go to the documentation of this file.
00001 /*
00002  * $Id: ivector.h 1972 2014-05-02 20:15:36Z johnoel $
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 
00046 class ivector
00047 {
00048 protected:
00049   int index_min;
00050   int index_max;
00051   int *v;
00052   vector_shapex *shape;
00053 
00054 public:
00055   ivector();
00056   ivector(const lvector&);
00057   ~ivector();
00058 
00059    int allocated() const
00060    {
00061       return (shape != NULL);
00062    }
00063    ivector& operator--()
00064    {
00065       index_min--;
00066       index_max--;
00067       v++;
00068       return *this;
00069    }
00070    void reallocate(double size);
00071 
00072    ivector& operator++()
00073    {
00074       index_min++;
00075       index_max++;
00076       v--;
00077       return *this;
00078    }
00079    int operator!() const
00080    {
00081       return (shape == NULL);
00082    }
00083 
00084    int& elem(int i)
00085    {
00086       return (v[i]);
00087    }
00088    const int& elem(int i) const
00089    {
00090       return v[i];
00091    }
00092    // returns the minimum allowable index
00093    int indexmin() const
00094    {
00095       return index_min;
00096    }
00097    // returns the maximum allowable index
00098    int indexmax() const
00099    {
00100       return index_max;
00101    }
00102    // returns the maximum allowable index
00103   int size() const
00104   {
00105     return v ? index_max - index_min + 1 : 0;
00106   }
00107    int *get_v() const
00108    {
00109       return v;
00110    }
00111    ivector& shift(int min);
00112    //ivector(unsigned int sz); //makes an array [0..sz]
00113    //inline allocated(void) {return ::allocated(this);}
00114 
00115    ivector(const dvector&);
00116 
00117    void fill(const char *s);
00118    void fill_seqadd(int, int);
00119    void fill_multinomial(const int& seed, const dvector& p);
00120    void fill_multinomial(const random_number_generator& rng,
00121      const dvector& p);
00122    ivector(int ncl, int ncu);
00123    ivector(const ad_integer& ncl, const index_type& ncu);
00124    void allocate(const ad_integer& ncl, const index_type& ncu);
00125    // makes an array [ncl..ncu]
00126 
00127    ivector(unsigned int sz, long int *x);
00128 
00129    void safe_allocate(int ncl, int ncu);
00130    void allocate(int ncl, int ncu);
00131    void allocate(const ivector& iv);
00132    void allocate(const dvector& dv);
00133    void allocate(const char *);
00134    void allocate();
00135    void deallocate();
00136    void safe_deallocate();
00137 
00138    //operator int* () {return v;}
00139 
00140    void save_ivector_position() const;
00141 
00142    void save_ivector_value() const;
00143 
00144    void write_on(const ostream& s) const;
00145 
00146    void read_from(const istream& s);
00147 
00148    void write_on(const uostream& s) const;
00149 
00150    void read_from(const uistream& s);
00151 
00152    int& operator[](int i);
00153    int& operator()(int i);
00154    const int& operator[](int i) const;
00155    const int& operator()(int i) const;
00156 
00157    ivector sub(int lb, int ub)
00158    {
00159       return preivector(this, lb, ub);
00160    }
00161    ivector operator()(int lb, int ub)
00162    {
00163       return preivector(this, lb, ub);
00164    }
00165    ivector(const preivector& pd);
00166 
00167    ivector operator()(const ivector& u);
00168 
00169    ivector(const ivector& t);
00170    void shallow_copy(const ivector& t);
00171 
00172    ivector& operator=(const ivector& t);
00173    ivector& operator=(int t);
00174 
00175    ivector& operator=(const lvector& t);
00176 
00177    void initialize(void);
00178    ivector& operator+=(const ivector& t);
00179    ivector& operator+=(int t);
00180 
00181   friend class vector_index;
00182   friend class imatrix;
00183   friend class i3_array;
00184 };
00185 
00186 #ifdef OPT_LIB
00187 inline int& ivector::operator[](int i)
00188 {
00189   return *(v + i);
00190 }
00191 inline int& ivector::operator()(int i)
00192 {
00193   return *(v + i);
00194 }
00195 inline const int& ivector::operator[](int i) const
00196 {
00197   return *(v + i);
00198 }
00199 inline const int& ivector::operator()(int i) const
00200 {
00201   return *(v + i);
00202 }
00203 #endif
00204 
00205 ivector operator+(const ivector& v, const ivector& w);
00206 ivector operator-(const ivector& v, const ivector& w);
00207 ivector operator+(const ivector& v, int w);
00208 ivector operator-(const ivector& v, int w);
00209 ivector operator+(int v, const ivector& w);
00210 ivector operator-(int v, const ivector& w);
00211 
00212 #endif