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