FEI Version of the Day
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     fieldEqnOffsets_(),
00024     numFields_(0),
00025     numIndices_(1)
00026 {
00027 }
00028 
00029 fei::FieldMask::FieldMask(const FieldMask& fm)
00030   : maskID_(fm.maskID_),
00031     fieldIDs_(fm.fieldIDs_),
00032     fieldSizes_(fm.fieldSizes_),
00033     fieldEqnOffsets_(fm.fieldEqnOffsets_)
00034 {
00035   numFields_ = fieldIDs_.size();
00036   numIndices_ = fm.numIndices_;
00037 }
00038 
00039 fei::FieldMask::FieldMask(int numFields,
00040             const int* fieldIDs,
00041             const int* fieldSizes)
00042   : maskID_(0),
00043     fieldIDs_(0, 4),
00044     fieldSizes_(0, 4),
00045     fieldEqnOffsets_(0, 4)
00046 {
00047   for(int i=0; i<numFields; ++i) {
00048     addField(fieldIDs[i], fieldSizes[i]);
00049   }
00050 }
00051 
00052 fei::FieldMask::~FieldMask()
00053 {
00054 }
00055 
00056 void fei::FieldMask::getFieldEqnOffset(int fieldID,
00057              int& offset) const
00058 {
00059   int idindex = 0;
00060   if (numFields_ < 2) {
00061     if (numFields_ < 1) {
00062       offset = 0;
00063       return;
00064     }
00065 
00066     if (fieldIDs_[0] != fieldID) {
00067       throw std::runtime_error("fei::FieldMask::getFieldEqnOffset: fieldID not found");
00068     }
00069   }
00070   else {
00071     idindex = -1;
00072     for(size_t i=0; i<fieldIDs_.size(); ++i) {
00073       if (fieldIDs_[i] == fieldID) {
00074         idindex = i; break;
00075       }
00076     }
00077   }
00078 
00079   if (idindex < 0) {
00080     throw std::runtime_error("fei::FieldMask::getFieldEqnOffset: fieldID not found");
00081   }
00082 
00083   offset = fieldEqnOffsets_[idindex];
00084 }
00085 
00086 void fei::FieldMask::addField(int fieldID, int fieldSize)
00087 {
00088   if (fieldID < 0) {
00089     throw std::runtime_error("fei::FieldMask ERROR, fieldID should be >= 0.");
00090   }
00091 
00092   int insertPoint = -1;
00093   int idindex = fei::binarySearch(fieldID, fieldIDs_, insertPoint);
00094   if (idindex >= 0) {
00095     for(unsigned i=idindex+1; i<fieldEqnOffsets_.size(); ++i) {
00096       fieldEqnOffsets_[i] += fieldSize;
00097     }
00098   }
00099   else {
00100     fieldIDs_.insert(fieldIDs_.begin()+insertPoint, fieldID);
00101 
00102     fieldSizes_.insert(fieldSizes_.begin()+insertPoint, fieldSize);
00103 
00104     fieldEqnOffsets_.push_back(1);
00105 
00106     int eqnOffset = 0;
00107     numIndices_ = 0;
00108     for(unsigned i=0; i<fieldIDs_.size(); ++i) {
00109       fieldEqnOffsets_[i] = eqnOffset;
00110       eqnOffset += fieldSizes_[i];
00111       numIndices_ += fieldSizes_[i];
00112     }
00113 
00114     numFields_ = fieldIDs_.size();
00115   }
00116 
00117   maskID_ = calculateMaskID();
00118 }
00119 
00120 int fei::FieldMask::calculateMaskID()
00121 {
00122   return( calculateMaskID(fieldIDs_.size(), &fieldIDs_[0]));
00123 }
00124 
00125 int fei::FieldMask::calculateMaskID(int numFields, const int* fieldIDs)
00126 {
00127   int maskID = 0;
00128   for(int i=0; i<numFields; ++i) {
00129     maskID += (fieldIDs[i]+1) +(i+1)*1000;
00130   }
00131 
00132   return(maskID);
00133 }
00134 
00135 int fei::FieldMask::calculateMaskID(const FieldMask& fm, int fieldID)
00136 {
00137 //  if (fm.hasFieldID(fieldID)) return fm.maskID_;
00138   return( fm.maskID_ + (fieldID+1) + (fm.numFields_+1)*1000 );
00139 }
00140 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends