| [0b990d] | 1 | //
 | 
|---|
 | 2 | // dim.cc
 | 
|---|
 | 3 | //
 | 
|---|
 | 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
 | 
|---|
 | 5 | //
 | 
|---|
 | 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
 | 
|---|
 | 7 | // Maintainer: LPS
 | 
|---|
 | 8 | //
 | 
|---|
 | 9 | // This file is part of the SC Toolkit.
 | 
|---|
 | 10 | //
 | 
|---|
 | 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
 | 
|---|
 | 12 | // it under the terms of the GNU Library General Public License as published by
 | 
|---|
 | 13 | // the Free Software Foundation; either version 2, or (at your option)
 | 
|---|
 | 14 | // any later version.
 | 
|---|
 | 15 | //
 | 
|---|
 | 16 | // The SC Toolkit is distributed in the hope that it will be useful,
 | 
|---|
 | 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 19 | // GNU Library General Public License for more details.
 | 
|---|
 | 20 | //
 | 
|---|
 | 21 | // You should have received a copy of the GNU Library General Public License
 | 
|---|
 | 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
 | 
|---|
 | 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 | 
|---|
 | 24 | //
 | 
|---|
 | 25 | // The U.S. Government is granted a limited license as per AL 91-7.
 | 
|---|
 | 26 | //
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | #ifdef __GNUC__
 | 
|---|
 | 29 | #pragma implementation
 | 
|---|
 | 30 | #endif
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | #include <math.h>
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | #include <util/keyval/keyval.h>
 | 
|---|
 | 35 | #include <math/scmat/dim.h>
 | 
|---|
 | 36 | #include <util/misc/formio.h>
 | 
|---|
 | 37 | #include <util/state/stateio.h>
 | 
|---|
 | 38 | #include <iomanip>
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | using namespace std;
 | 
|---|
 | 41 | using namespace sc;
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 | static void
 | 
|---|
 | 44 | fail(const char *s)
 | 
|---|
 | 45 | {
 | 
|---|
 | 46 |   ExEnv::errn() << indent << "math/scmat/dim.cc: " << s << endl;
 | 
|---|
 | 47 |   abort();
 | 
|---|
 | 48 | }
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 51 | // SCBlockInfo member functions
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | static ClassDesc SCBlockInfo_cd(
 | 
|---|
 | 54 |   typeid(SCBlockInfo),"SCBlockInfo",1,"public SavableState",
 | 
|---|
 | 55 |   0, create<SCBlockInfo>, create<SCBlockInfo>);
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 | SCBlockInfo::SCBlockInfo(int n, int nblocks, const int *blocksizes):
 | 
|---|
 | 58 |   subdims_(0)
 | 
|---|
 | 59 | {
 | 
|---|
 | 60 |   n_ = n;
 | 
|---|
 | 61 |   nblocks_ = nblocks;
 | 
|---|
 | 62 |   size_ = 0;
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 |   if (n_ == 0) nblocks_ = 0;
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 |   if (n_ != 0 && nblocks_ == 0) {
 | 
|---|
 | 67 |       nblocks_ = 1;
 | 
|---|
 | 68 |       size_ = new int[1];
 | 
|---|
 | 69 |       size_[0] = n;
 | 
|---|
 | 70 |     }
 | 
|---|
 | 71 |   else if (nblocks_ == 0) {
 | 
|---|
 | 72 |       size_ = 0;
 | 
|---|
 | 73 |     }
 | 
|---|
 | 74 |   else {
 | 
|---|
 | 75 |       int i;
 | 
|---|
 | 76 |       size_ = new int[nblocks_];
 | 
|---|
 | 77 |       if (blocksizes) {
 | 
|---|
 | 78 |           for (i=0; i<nblocks_; i++) {
 | 
|---|
 | 79 |               size_[i] = blocksizes[i];
 | 
|---|
 | 80 |             }
 | 
|---|
 | 81 |         }
 | 
|---|
 | 82 |       else {
 | 
|---|
 | 83 |           int nper = n/nblocks;
 | 
|---|
 | 84 |           int nleft = n%nblocks;
 | 
|---|
 | 85 |           for (i=0; i<nblocks_; i++) {
 | 
|---|
 | 86 |               size_[i] = nper;
 | 
|---|
 | 87 |               if (i<nleft) size_[i]++;
 | 
|---|
 | 88 |             }
 | 
|---|
 | 89 |         }
 | 
|---|
 | 90 |     }
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |   init_start();
 | 
|---|
 | 93 | }
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 | SCBlockInfo::SCBlockInfo(const Ref<KeyVal>&keyval):
 | 
|---|
 | 96 |   subdims_(0)
 | 
|---|
 | 97 | {
 | 
|---|
 | 98 |   nblocks_ = keyval->count("sizes");
 | 
|---|
 | 99 |   n_ = 0;
 | 
|---|
 | 100 |   size_ = new int[nblocks_];
 | 
|---|
 | 101 |   for (int i=0; i<nblocks_; i++) {
 | 
|---|
 | 102 |       size_[i] = keyval->intvalue("sizes",i);
 | 
|---|
 | 103 |       n_ += size_[i];
 | 
|---|
 | 104 |     }
 | 
|---|
 | 105 |   int nsubdims = keyval->count("subdims");
 | 
|---|
 | 106 |   if (nsubdims) {
 | 
|---|
 | 107 |       if (nblocks_ != 0 && nsubdims != nblocks_) {
 | 
|---|
 | 108 |           fail("SCBlockInfo(const Ref<KeyVal>&): nsubdims != nblocks");
 | 
|---|
 | 109 |         }
 | 
|---|
 | 110 |       subdims_ = new RefSCDimension[nsubdims];
 | 
|---|
 | 111 |       for (int i=0; i<nsubdims; i++) {
 | 
|---|
 | 112 |           subdims_[i] << keyval->describedclassvalue("subdims",i);
 | 
|---|
 | 113 |         }
 | 
|---|
 | 114 |       if (nblocks_ == 0) {
 | 
|---|
 | 115 |           delete[] size_;
 | 
|---|
 | 116 |           size_ = new int[nsubdims];
 | 
|---|
 | 117 |           for (int i=0; i<nsubdims; i++) {
 | 
|---|
 | 118 |               size_[i] = subdims_[i].n();
 | 
|---|
 | 119 |               n_ += size_[i];
 | 
|---|
 | 120 |             }
 | 
|---|
 | 121 |           nblocks_ = nsubdims;
 | 
|---|
 | 122 |         }
 | 
|---|
 | 123 |     }
 | 
|---|
 | 124 |   init_start();
 | 
|---|
 | 125 | }
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 | SCBlockInfo::SCBlockInfo(StateIn&s):
 | 
|---|
 | 128 |   SavableState(s)
 | 
|---|
 | 129 | {
 | 
|---|
 | 130 |   s.get(n_);
 | 
|---|
 | 131 |   s.get(nblocks_);
 | 
|---|
 | 132 |   s.get(size_);
 | 
|---|
 | 133 |   s.get(start_);
 | 
|---|
 | 134 |   int have_subdims;
 | 
|---|
 | 135 |   s.get(have_subdims);
 | 
|---|
 | 136 |   if (have_subdims) {
 | 
|---|
 | 137 |       subdims_ = new RefSCDimension[nblocks_];
 | 
|---|
 | 138 |       for (int i=0; i<nblocks_; i++) {
 | 
|---|
 | 139 |           subdims_[i] << SavableState::restore_state(s);
 | 
|---|
 | 140 |         }
 | 
|---|
 | 141 |     }
 | 
|---|
 | 142 |   else {
 | 
|---|
 | 143 |       subdims_ = 0;
 | 
|---|
 | 144 |     }
 | 
|---|
 | 145 | }
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 | void
 | 
|---|
 | 148 | SCBlockInfo::save_data_state(StateOut&s)
 | 
|---|
 | 149 | {
 | 
|---|
 | 150 |   s.put(n_);
 | 
|---|
 | 151 |   s.put(nblocks_);
 | 
|---|
 | 152 |   s.put(size_,nblocks_);
 | 
|---|
 | 153 |   s.put(start_,nblocks_);
 | 
|---|
 | 154 |   if (subdims_) {
 | 
|---|
 | 155 |       s.put(1);
 | 
|---|
 | 156 |       for (int i=0; i<nblocks_; i++) {
 | 
|---|
 | 157 |           SavableState::save_state(subdims_[i].pointer(),s);
 | 
|---|
 | 158 |         }
 | 
|---|
 | 159 |     }
 | 
|---|
 | 160 |   else {
 | 
|---|
 | 161 |       s.put(0);
 | 
|---|
 | 162 |     }
 | 
|---|
 | 163 | }
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 | void
 | 
|---|
 | 166 | SCBlockInfo::init_start()
 | 
|---|
 | 167 | {
 | 
|---|
 | 168 |   start_ = 0;
 | 
|---|
 | 169 |   if (nblocks_) {
 | 
|---|
 | 170 |       start_ = new int[nblocks_];
 | 
|---|
 | 171 |       start_[0] = 0;
 | 
|---|
 | 172 |       for (int i=1; i<nblocks_; i++) {
 | 
|---|
 | 173 |           start_[i] = start_[i-1] + size_[i-1];
 | 
|---|
 | 174 |         }
 | 
|---|
 | 175 |     }
 | 
|---|
 | 176 | }
 | 
|---|
 | 177 | 
 | 
|---|
 | 178 | int
 | 
|---|
 | 179 | SCBlockInfo::equiv(SCBlockInfo *bi)
 | 
|---|
 | 180 | {
 | 
|---|
 | 181 |   if (bi == 0) return 0;
 | 
|---|
 | 182 |   if (n_ != bi->n_) return 0;
 | 
|---|
 | 183 |   if (nblocks_ != bi->nblocks_) return 0;
 | 
|---|
 | 184 |   int i;
 | 
|---|
 | 185 |   for (i=0; i<nblocks_; i++) {
 | 
|---|
 | 186 |       if (start_[i] != bi->start_[i]) return 0;
 | 
|---|
 | 187 |     }
 | 
|---|
 | 188 |   if (subdims_) {
 | 
|---|
 | 189 |       if (bi->subdims_) {
 | 
|---|
 | 190 |           for (i=0; i<nblocks_; i++) {
 | 
|---|
 | 191 |               if (subdims_[i].nonnull()) {
 | 
|---|
 | 192 |                   if (bi->subdims_[i].nonnull()) {
 | 
|---|
 | 193 |                       if (!subdims_[i]->equiv(bi->subdims_[i])) return 0;
 | 
|---|
 | 194 |                     }
 | 
|---|
 | 195 |                   else return 0;
 | 
|---|
 | 196 |                 }
 | 
|---|
 | 197 |               else if (bi->subdims_[i].nonnull()) return 0;
 | 
|---|
 | 198 |             }
 | 
|---|
 | 199 |         }
 | 
|---|
 | 200 |       else {
 | 
|---|
 | 201 |           return 0;
 | 
|---|
 | 202 |         }
 | 
|---|
 | 203 |     }
 | 
|---|
 | 204 |   else if (bi->subdims_) {
 | 
|---|
 | 205 |       return 0;
 | 
|---|
 | 206 |     }
 | 
|---|
 | 207 |   return 1;
 | 
|---|
 | 208 | }
 | 
|---|
 | 209 | 
 | 
|---|
 | 210 | void
 | 
|---|
 | 211 | SCBlockInfo::elem_to_block(int elem, int &block, int &offset)
 | 
|---|
 | 212 | {
 | 
|---|
 | 213 |   for (int i=nblocks_-1; i>=0; i--) {
 | 
|---|
 | 214 |       if (start_[i] <= elem) {
 | 
|---|
 | 215 |           block = i;
 | 
|---|
 | 216 |           offset = elem-start_[i];
 | 
|---|
 | 217 |           return;
 | 
|---|
 | 218 |         }
 | 
|---|
 | 219 |     }
 | 
|---|
 | 220 |   fail("SCBlockInfo::elem_to_block: couldn't find block");
 | 
|---|
 | 221 | }
 | 
|---|
 | 222 | 
 | 
|---|
 | 223 | SCBlockInfo::~SCBlockInfo()
 | 
|---|
 | 224 | {
 | 
|---|
 | 225 |   delete[] start_;
 | 
|---|
 | 226 |   delete[] size_;
 | 
|---|
 | 227 |   delete[] subdims_;
 | 
|---|
 | 228 | }
 | 
|---|
 | 229 | 
 | 
|---|
 | 230 | RefSCDimension
 | 
|---|
 | 231 | SCBlockInfo::subdim(int i)
 | 
|---|
 | 232 | {
 | 
|---|
 | 233 |   if (i >= nblocks_ || i < 0) {
 | 
|---|
 | 234 |       fail("SCBlockInfo::subdim: bad block index");
 | 
|---|
 | 235 |     }
 | 
|---|
 | 236 |   if (!subdims_) return 0;
 | 
|---|
 | 237 |   return subdims_[i];
 | 
|---|
 | 238 | }
 | 
|---|
 | 239 | 
 | 
|---|
 | 240 | void
 | 
|---|
 | 241 | SCBlockInfo::set_subdim(int i, const RefSCDimension &dim)
 | 
|---|
 | 242 | {
 | 
|---|
 | 243 |   if (!subdims_) {
 | 
|---|
 | 244 |       subdims_ = new RefSCDimension[nblocks_];
 | 
|---|
 | 245 |     }
 | 
|---|
 | 246 |   if (i >= nblocks_ || i < 0) {
 | 
|---|
 | 247 |       fail("SCBlockInfo::set_subdim: bad block index");
 | 
|---|
 | 248 |     }
 | 
|---|
 | 249 |   if (size(i) != dim.n()) {
 | 
|---|
 | 250 |       fail("SCBlockInfo::set_subdim: size mismatch");
 | 
|---|
 | 251 |     }
 | 
|---|
 | 252 |   subdims_[i] = dim;
 | 
|---|
 | 253 | }
 | 
|---|
 | 254 | 
 | 
|---|
 | 255 | void
 | 
|---|
 | 256 | SCBlockInfo::print(ostream&o) const
 | 
|---|
 | 257 | {
 | 
|---|
 | 258 |   indent(o); o << "nblocks = " << nblocks_ << endl;
 | 
|---|
 | 259 |   indent(o); o << "sizes =";
 | 
|---|
 | 260 |   for (int i=0; i<nblocks_; i++) {
 | 
|---|
 | 261 |       o << " " << size_[i];
 | 
|---|
 | 262 |     }
 | 
|---|
 | 263 |   o << endl;
 | 
|---|
 | 264 |   if (subdims_) {
 | 
|---|
 | 265 |       for (int i=0; i<nblocks_; i++) {
 | 
|---|
 | 266 |           indent(o); o << "subdimension " << i << ":" << endl;
 | 
|---|
 | 267 |           incindent(o);
 | 
|---|
 | 268 |           subdims_[i].print(o);
 | 
|---|
 | 269 |           decindent(o);
 | 
|---|
 | 270 |         }
 | 
|---|
 | 271 |     }
 | 
|---|
 | 272 | }
 | 
|---|
 | 273 | 
 | 
|---|
 | 274 | /////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 275 | // SCDimension members
 | 
|---|
 | 276 | 
 | 
|---|
 | 277 | static ClassDesc SCDimension_cd(
 | 
|---|
 | 278 |   typeid(SCDimension),"SCDimension",1,"public SavableState",
 | 
|---|
 | 279 |   0, create<SCDimension>, create<SCDimension>);
 | 
|---|
 | 280 | 
 | 
|---|
 | 281 | SCDimension::SCDimension(const char* name):
 | 
|---|
 | 282 |   n_(0)
 | 
|---|
 | 283 | {
 | 
|---|
 | 284 |   if (name) name_ = strcpy(new char[strlen(name)+1], name);
 | 
|---|
 | 285 |   else name_ = 0;
 | 
|---|
 | 286 | }
 | 
|---|
 | 287 | 
 | 
|---|
 | 288 | SCDimension::SCDimension(int n, const char* name):
 | 
|---|
 | 289 |   n_(n)
 | 
|---|
 | 290 | {
 | 
|---|
 | 291 |   if (name) name_ = strcpy(new char[strlen(name)+1], name);
 | 
|---|
 | 292 |   else name_ = 0;
 | 
|---|
 | 293 |   blocks_ = new SCBlockInfo(n, 1);
 | 
|---|
 | 294 | }
 | 
|---|
 | 295 | 
 | 
|---|
 | 296 | SCDimension::SCDimension(const Ref<SCBlockInfo>& b, const char* name):
 | 
|---|
 | 297 |   n_(b->nelem()), blocks_(b)
 | 
|---|
 | 298 | {
 | 
|---|
 | 299 |   if (name) name_ = strcpy(new char[strlen(name)+1], name);
 | 
|---|
 | 300 |   else name_ = 0;
 | 
|---|
 | 301 | }
 | 
|---|
 | 302 | 
 | 
|---|
 | 303 | SCDimension::SCDimension(int n,
 | 
|---|
 | 304 |                          int nblocks, const int *blocksizes,
 | 
|---|
 | 305 |                          const char* name):
 | 
|---|
 | 306 |   n_(n)
 | 
|---|
 | 307 | {
 | 
|---|
 | 308 |   if (name) name_ = strcpy(new char[strlen(name)+1], name);
 | 
|---|
 | 309 |   else name_ = 0;
 | 
|---|
 | 310 |   blocks_ = new SCBlockInfo(n, nblocks, blocksizes);
 | 
|---|
 | 311 | }
 | 
|---|
 | 312 | 
 | 
|---|
 | 313 | SCDimension::SCDimension(const Ref<KeyVal>& keyval)
 | 
|---|
 | 314 | {
 | 
|---|
 | 315 |   blocks_ << keyval->describedclassvalue("blocks");
 | 
|---|
 | 316 |   n_ = keyval->intvalue("n");
 | 
|---|
 | 317 |   if (blocks_.null()) {
 | 
|---|
 | 318 |       if (keyval->error() != KeyVal::OK) {
 | 
|---|
 | 319 |           fail("SCDimension(const Ref<KeyVal>&): missing input");
 | 
|---|
 | 320 |         }
 | 
|---|
 | 321 |       blocks_ = new SCBlockInfo(n_);
 | 
|---|
 | 322 |     }
 | 
|---|
 | 323 |   else {
 | 
|---|
 | 324 |       if (n_ != 0 && n_ != blocks_->nelem()) {
 | 
|---|
 | 325 |           fail("SCDimension(const Ref<KeyVal>&): inconsistent sizes");
 | 
|---|
 | 326 |         }
 | 
|---|
 | 327 |       n_ = blocks_->nelem();
 | 
|---|
 | 328 |     }
 | 
|---|
 | 329 |   name_ = keyval->pcharvalue("name");
 | 
|---|
 | 330 | }
 | 
|---|
 | 331 | 
 | 
|---|
 | 332 | SCDimension::SCDimension(StateIn&s):
 | 
|---|
 | 333 |   SavableState(s)
 | 
|---|
 | 334 | {
 | 
|---|
 | 335 |   s.getstring(name_);
 | 
|---|
 | 336 |   s.get(n_);
 | 
|---|
 | 337 |   blocks_ << SavableState::restore_state(s);
 | 
|---|
 | 338 | }
 | 
|---|
 | 339 | 
 | 
|---|
 | 340 | void
 | 
|---|
 | 341 | SCDimension::save_data_state(StateOut&s)
 | 
|---|
 | 342 | {
 | 
|---|
 | 343 |   s.putstring(name_);
 | 
|---|
 | 344 |   s.put(n_);
 | 
|---|
 | 345 |   SavableState::save_state(blocks_.pointer(), s);
 | 
|---|
 | 346 | }
 | 
|---|
 | 347 | 
 | 
|---|
 | 348 | SCDimension::~SCDimension()
 | 
|---|
 | 349 | {
 | 
|---|
 | 350 |   if (name_) delete[] name_;
 | 
|---|
 | 351 | }
 | 
|---|
 | 352 | 
 | 
|---|
 | 353 | int
 | 
|---|
 | 354 | SCDimension::equiv(const SCDimension *a) const
 | 
|---|
 | 355 | {
 | 
|---|
 | 356 |   if (n_ != a->n_) return 0;
 | 
|---|
 | 357 | 
 | 
|---|
 | 358 |   if (!blocks_->equiv(a->blocks_)) return 0;
 | 
|---|
 | 359 | 
 | 
|---|
 | 360 |   return 1;
 | 
|---|
 | 361 | }
 | 
|---|
 | 362 | 
 | 
|---|
 | 363 | void
 | 
|---|
 | 364 | SCDimension::print(ostream&o) const
 | 
|---|
 | 365 | {
 | 
|---|
 | 366 |   indent(o); o << "n = " << n_;
 | 
|---|
 | 367 |   if (name_) {
 | 
|---|
 | 368 |       o << ", name = " << name_;
 | 
|---|
 | 369 |     }
 | 
|---|
 | 370 |   o << endl;
 | 
|---|
 | 371 |   incindent(o);
 | 
|---|
 | 372 |   if (blocks_.nonnull()) blocks_->print(o);
 | 
|---|
 | 373 |   decindent(o);
 | 
|---|
 | 374 | }
 | 
|---|
 | 375 | 
 | 
|---|
 | 376 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 377 | // RefSCDimension member functions
 | 
|---|
 | 378 | 
 | 
|---|
 | 379 | void
 | 
|---|
 | 380 | RefSCDimension::print(ostream&o) const
 | 
|---|
 | 381 | {
 | 
|---|
 | 382 |   if (null()) { indent(o); o << "n = 0" << endl; }
 | 
|---|
 | 383 |   else pointer()->print(o);
 | 
|---|
 | 384 | }
 | 
|---|
 | 385 | 
 | 
|---|
 | 386 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 387 | 
 | 
|---|
 | 388 | // Local Variables:
 | 
|---|
 | 389 | // mode: c++
 | 
|---|
 | 390 | // c-file-style: "CLJ"
 | 
|---|
 | 391 | // End:
 | 
|---|