GURLS++  2.0.00
C++ Implementation of GURLS Matlab Toolbox
basearray.h
00001 /*
00002  * The GURLS Package in C++
00003  *
00004  * Copyright (C) 2011-1013, IIT@MIT Lab
00005  * All rights reserved.
00006  *
00007  * author:  M. Santoro
00008  * email:   msantoro@mit.edu
00009  * website: http://cbcl.mit.edu/IIT@MIT/IIT@MIT.html
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  *
00015  *     * Redistributions of source code must retain the above
00016  *       copyright notice, this list of conditions and the following
00017  *       disclaimer.
00018  *     * Redistributions in binary form must reproduce the above
00019  *       copyright notice, this list of conditions and the following
00020  *       disclaimer in the documentation and/or other materials
00021  *       provided with the distribution.
00022  *     * Neither the name(s) of the copyright holders nor the names
00023  *       of its contributors or of the Massacusetts Institute of
00024  *       Technology or of the Italian Institute of Technology may be
00025  *       used to endorse or promote products derived from this software
00026  *       without 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
00031  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00032  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00033  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00034  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00036  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00037  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00038  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00039  * POSSIBILITY OF SUCH DAMAGE.
00040  */
00041 
00042 #ifndef _GURLS_BASEARRAY_H_
00043 #define _GURLS_BASEARRAY_H_
00044 
00045 #include <cstdio>
00046 #include <cstring>
00047 #include <iostream>
00048 #include <cmath>
00049 #include <algorithm>
00050 #include <limits>
00051 #include <cassert>
00052 
00053 #include <boost/serialization/base_object.hpp>
00054 #include <boost/serialization/split_member.hpp>
00055 
00056 #include "gurls++/exceptions.h"
00057 
00058 namespace gurls {
00059 
00064 const static int MAX_PRINTABLE_SIZE = 200;
00065 
00071 template <typename T>
00072 class BaseArray {
00073 
00074 protected:
00075 
00076     T* data;                    
00077     unsigned long size;         
00078     bool isowner;               
00079 
00080     void alloc(unsigned long n); 
00081 
00082 public:
00083 
00087     BaseArray() : data(0), size(0), isowner(false) { /* TO BE DISCUSSED*/}
00088 
00092     BaseArray(unsigned long n) {this->alloc(n);}
00093 
00097     BaseArray(const BaseArray<T>& other);
00098 
00102     BaseArray<T>& operator=(const BaseArray<T>& other);
00103 
00107     BaseArray<T>& operator=(const T& val);
00108 
00112     ~BaseArray(){ if (this->isowner && data != NULL && size > 0) { delete[] this->data; } }
00113 
00117     void set(const T * v, unsigned long n, unsigned long start = 0);
00118 
00122     void resize(unsigned long n);
00123 
00127     void asarray(T * v, unsigned long n) const;
00128 
00132     void randomize();
00133 
00137     unsigned long getSize() const { return this->size; }
00138 
00142     const T* getData() const { return this->data; }
00143 
00147     T* getData() { return this->data; }
00148 
00152     const T* begin() const { return this->data(); }
00153 
00157     T* begin() { return this->data(); }
00158 
00162     const T* end() const { return (this->data() + this->size); }
00163 
00167     T* end() { return (this->data() + this->size); }
00168 
00169 
00173     const T& max() const;
00174 
00178     const T& min() const;
00179 
00183     T sum() const;
00184 
00188     BaseArray<T>& operator+=(T);
00189 
00193     BaseArray<T>& operator-=(T);
00194 
00198     BaseArray<T>& operator*=(T);
00199 
00203     BaseArray<T>& operator/=(T);
00204 
00208     BaseArray<T>& add(const BaseArray<T>&);
00209 
00213     BaseArray<T>& subtract(const BaseArray<T>&);
00214 
00218     BaseArray<T>& multiply(const BaseArray<T>&);
00219 
00223     BaseArray<T>& divide(const BaseArray<T>&);
00224 
00228     BaseArray<T>& setReciprocal();
00229 
00233     template <typename U>
00234     friend bool operator== (const BaseArray<U>&, const U&);
00235 
00239     bool closeTo(const BaseArray<T>&, T tolerance) const;
00240 
00244     virtual std::string what() const = 0;
00245 
00246     friend class boost::serialization::access;
00247 
00251     template<class Archive>
00252     void save(Archive & , const unsigned int) const;
00253 
00257     template<class Archive>
00258     void load(Archive & , const unsigned int);
00259 
00260     BOOST_SERIALIZATION_SPLIT_MEMBER()
00261 };
00262 
00263 }
00264 
00265 #include "basearray.hpp"
00266 
00267 #endif // _GURLS_BASEARRAY_H_
00268 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends