fei_FieldMask.cpp

00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2005 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 
00009 #include "fei_macros.hpp"
00010 
00011 #include <string>
00012 #include <exception>
00013 #include <stdexcept>
00014 
00015 #include "fei_ArrayUtils.hpp"
00016 #include "fei_FieldMask.hpp"
00017 
00018 
00019 fei::FieldMask::FieldMask()
00020   : maskID_(0),
00021     fieldIDs_(),
00022     fieldSizes_(),
00023     fieldInstances_(),
00024     fieldEqnOffsets_(),
00025     numFields_(0),
00026     numIndices_(1)
00027 {
00028 }
00029 
00030 fei::FieldMask::FieldMask(const FieldMask& fm)
00031   : maskID_(fm.maskID_),
00032     fieldIDs_(fm.fieldIDs_),
00033     fieldSizes_(fm.fieldSizes_),
00034     fieldInstances_(fm.fieldInstances_),
00035     fieldEqnOffsets_(fm.fieldEqnOffsets_)
00036 {
00037   numFields_ = fieldIDs_.size();
00038   numIndices_ = fm.numIndices_;
00039 }
00040 
00041 fei::FieldMask::FieldMask(int numFields,
00042             const int* fieldIDs,
00043             const int* fieldSizes,
00044             const int* numInstancesOfThisFieldPerID)
00045   : maskID_(0),
00046     fieldIDs_(0, 4),
00047     fieldSizes_(0, 4),
00048     fieldInstances_(0, 4),
00049     fieldEqnOffsets_(0, 4)
00050 {
00051   for(int i=0; i<numFields; ++i) {
00052     addField(fieldIDs[i], fieldSizes[i],
00053        numInstancesOfThisFieldPerID[i]);
00054   }
00055 }
00056 
00057 fei::FieldMask::~FieldMask()
00058 {
00059 }
00060 
00061 bool fei::FieldMask::hasFieldID(int fieldID) const
00062 {
00063   return(
00064     fei::binarySearch(fieldID,
00065       fieldIDs_.size() ? &fieldIDs_[0] : 0,
00066       fieldIDs_.size())
00067     >= 0
00068     );
00069 }
00070 
00071 void fei::FieldMask::getFieldEqnOffset(int fieldID,
00072              int& offset,
00073              int& numInstancesPerID) const
00074 {
00075   int idindex = 0;
00076   if (numFields_ < 2) {
00077     if (numFields_ < 1) {
00078       offset = 0;
00079       numInstancesPerID = 1;
00080       return;
00081     }
00082 
00083     if (fieldIDs_[0] != fieldID) {
00084       throw std::runtime_error("fei::FieldMask::getFieldEqnOffset: fieldID not found");
00085     }
00086   }
00087   else idindex = fei::binarySearch(fieldID, &fieldIDs_[0], fieldIDs_.size());
00088 
00089   if (idindex < 0) {
00090      throw std::runtime_error("fei::FieldMask::getFieldEqnOffset: fieldID not found");
00091   }
00092 
00093   offset = fieldEqnOffsets_[idindex];
00094   numInstancesPerID = fieldInstances_[idindex];
00095 }
00096 
00097 void fei::FieldMask::addField(int fieldID, int fieldSize, int numInstances)
00098 {
00099   if (fieldID < 0) {
00100     throw std::runtime_error("fei::FieldMask ERROR, fieldID should be >= 0.");
00101   }
00102 
00103   int insertPoint = -1;
00104   int idindex = fei::binarySearch(fieldID, fieldIDs_, insertPoint);
00105   if (idindex >= 0) {
00106     fieldInstances_[idindex] += numInstances;
00107     for(unsigned i=idindex+1; i<fieldEqnOffsets_.size(); ++i) {
00108       fieldEqnOffsets_[i] += numInstances*fieldSize;
00109     }
00110   }
00111   else {
00112     fieldIDs_.insert(fieldIDs_.begin()+insertPoint, fieldID);
00113 
00114     fieldSizes_.insert(fieldSizes_.begin()+insertPoint, fieldSize);
00115 
00116     fieldInstances_.insert(fieldInstances_.begin()+insertPoint, numInstances);
00117 
00118     fieldEqnOffsets_.push_back(numInstances);
00119 
00120     int eqnOffset = 0;
00121     numIndices_ = 0;
00122     for(unsigned i=0; i<fieldIDs_.size(); ++i) {
00123       fieldEqnOffsets_[i] = eqnOffset;
00124       eqnOffset += fieldInstances_[i]*fieldSizes_[i];
00125       numIndices_ += fieldInstances_[i]*fieldSizes_[i];
00126     }
00127 
00128     numFields_ = fieldIDs_.size();
00129   }
00130 
00131   maskID_ = calculateMaskID();
00132 }
00133 
00134 int fei::FieldMask::calculateMaskID()
00135 {
00136   return( calculateMaskID(fieldIDs_.size(), &fieldIDs_[0],
00137         &fieldInstances_[0]) );
00138 }
00139 
00140 int fei::FieldMask::calculateMaskID(int numFields, const int* fieldIDs,
00141           const int* numInstancesPerID)
00142 {
00143   int maskID = 0;
00144   for(int i=0; i<numFields; ++i) {
00145     maskID += (fieldIDs[i]+1)*numInstancesPerID[i] +(i+1)*1000;
00146   }
00147 
00148   return(maskID);
00149 }
00150 
00151 int fei::FieldMask::calculateMaskID(const FieldMask& fm,
00152           int fieldID, int numInstances)
00153 {
00154   return( fm.maskID_ + (fieldID+1)*numInstances + (fm.numFields_+1)*1000 );
00155 }
00156 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends
Generated on Wed Apr 13 10:08:23 2011 for FEI by  doxygen 1.6.3