// // $Source: /cvsroot/gambit/gambit/sources/tools/enumpoly/gnarray.imp,v $ // $Date: 2006/01/07 05:41:26 $ // $Revision: 1.4 $ // // DESCRIPTION: // Implementation for N-dimensional arrays // // This file is part of Gambit // Copyright (c) 2002, The Gambit Project // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // #include #include "gnarray.h" template gNArray::gNArray(void) : storage_size(0), storage(0) { } template gNArray::gNArray(const Gambit::Array &d) : dim(d) { if (dim.Length() <= 0) { storage = 0; storage_size = 0; } else { assert(dim.First() == 1); storage_size = 1; int i; for (i = 1; i <= dim.Length(); i++) { assert(dim[i] >= 1); storage_size *= dim[i]; } storage = new T[storage_size]; for (i = 0; i < storage_size; storage[i++] = 0); } } template gNArray::gNArray(const gNArray &a) : storage_size(a.storage_size), dim(a.dim) { storage = (storage_size > 0) ? new T[storage_size] : 0; for (int i = 0; i < storage_size; i++) storage[i] = a.storage[i]; } template gNArray::~gNArray() { if (storage) delete [] storage; } template gNArray &gNArray::operator=(const gNArray &a) { if (this != &a) { if (storage) delete [] storage; dim = a.dim; storage_size = a.storage_size; storage = (storage_size > 0) ? new T[storage_size] : 0; for (int i = 0; i < storage_size; i++) storage[i] = a.storage[i]; } return *this; } /* template T gNArray::operator[](const Gambit::Vector &v) const { assert(dim.Length() > 0 && dim.Length() == v.Length()); int i,location,offset; for (i = 1, location = 0, offset = 1; i <= dim.Length(); i++) { assert(v[i] > 0 && v[i] <= dim[i]); location += (v[i] - 1) * offset; offset *= dim[i]; } return storage[location]; } template T &gNArray::operator[](const Gambit::Vector &v) { assert(dim.Length() > 0 && dim.Length() == v.Length()); int i, location, offset; for (i = 1, location = 0, offset = 1; i <= dim.Length(); i++) { assert(v[i] > 0 && v[i] <= dim[i]); location += (v[i] - 1) * offset; offset *= dim[i]; } return storage[location]; } */ template T gNArray::operator[](const Gambit::Array &v) const { assert(dim.Length() > 0 && dim.Length() == v.Length()); int i, location, offset; for (i = 1, location = 0, offset = 1; i <= dim.Length(); i++) { assert(v[i] > 0 && v[i] <= dim[i]); location += (v[i] - 1) * offset; offset *= dim[i]; } return storage[location]; } template T &gNArray::operator[](const Gambit::Array &v) { assert(dim.Length() > 0 && dim.Length() == v.Length()); int i, location, offset; for (i = 1, location = 0, offset = 1; i <= dim.Length(); i++) { assert(v[i] > 0 && v[i] <= dim[i]); location += (v[i] - 1) * offset; offset *= dim[i]; } return storage[location]; } template const T &gNArray::operator[](long l) const { assert(l >= 0 && l < storage_size); return storage[l]; } template T &gNArray::operator[](long l) { assert(l >= 0 && l < storage_size); return storage[l]; } template const Gambit::Array &gNArray::Dimensionality(void) const { return dim; }