FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fei_FieldMask.cpp
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2005 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 
9 #include "fei_macros.hpp"
10 
11 #include <string>
12 #include <exception>
13 #include <stdexcept>
14 
15 #include "fei_ArrayUtils.hpp"
16 #include "fei_FieldMask.hpp"
17 
18 
20  : maskID_(0),
21  fieldIDs_(),
22  fieldSizes_(),
23  fieldEqnOffsets_(),
24  numFields_(0),
25  numIndices_(1)
26 {
27 }
28 
29 fei::FieldMask::FieldMask(const FieldMask& fm)
30  : maskID_(fm.maskID_),
31  fieldIDs_(fm.fieldIDs_),
32  fieldSizes_(fm.fieldSizes_),
33  fieldEqnOffsets_(fm.fieldEqnOffsets_)
34 {
35  numFields_ = fieldIDs_.size();
36  numIndices_ = fm.numIndices_;
37 }
38 
39 fei::FieldMask::FieldMask(int numFields,
40  const int* fieldIDs,
41  const int* fieldSizes)
42  : maskID_(0),
43  fieldIDs_(0, 4),
44  fieldSizes_(0, 4),
45  fieldEqnOffsets_(0, 4)
46 {
47  for(int i=0; i<numFields; ++i) {
48  addField(fieldIDs[i], fieldSizes[i]);
49  }
50 }
51 
53 {
54 }
55 
56 int fei::FieldMask::getFieldEqnOffset(int fieldID,
57  int& offset) const
58 {
59  int idindex = 0;
60  if (numFields_ < 2) {
61  if (numFields_ < 1) {
62  offset = 0;
63  return 0;
64  }
65 
66  if (fieldIDs_[0] != fieldID) {
67  return -1;
68  }
69  }
70  else {
71  idindex = -1;
72  for(size_t i=0; i<fieldIDs_.size(); ++i) {
73  if (fieldIDs_[i] == fieldID) {
74  idindex = i; break;
75  }
76  }
77  }
78 
79  if (idindex < 0) {
80  return -1;
81  }
82 
83  offset = fieldEqnOffsets_[idindex];
84  return 0;
85 }
86 
87 void fei::FieldMask::addField(int fieldID, int fieldSize)
88 {
89  if (fieldID < 0) {
90  throw std::runtime_error("fei::FieldMask ERROR, fieldID should be >= 0.");
91  }
92 
93  int insertPoint = -1;
94  int idindex = fei::binarySearch(fieldID, fieldIDs_, insertPoint);
95  if (idindex >= 0) {
96  for(unsigned i=idindex+1; i<fieldEqnOffsets_.size(); ++i) {
97  fieldEqnOffsets_[i] += fieldSize;
98  }
99  }
100  else {
101  fieldIDs_.insert(fieldIDs_.begin()+insertPoint, fieldID);
102 
103  fieldSizes_.insert(fieldSizes_.begin()+insertPoint, fieldSize);
104 
105  fieldEqnOffsets_.push_back(1);
106 
107  int eqnOffset = 0;
108  numIndices_ = 0;
109  for(unsigned i=0; i<fieldIDs_.size(); ++i) {
110  fieldEqnOffsets_[i] = eqnOffset;
111  eqnOffset += fieldSizes_[i];
112  numIndices_ += fieldSizes_[i];
113  }
114 
115  numFields_ = fieldIDs_.size();
116  }
117 
118  maskID_ = calculateMaskID();
119 }
120 
122 {
123  return( calculateMaskID(fieldIDs_.size(), &fieldIDs_[0]));
124 }
125 
126 int fei::FieldMask::calculateMaskID(int numFields, const int* fieldIDs)
127 {
128  int maskID = 0;
129  for(int i=0; i<numFields; ++i) {
130  maskID += (fieldIDs[i]+1) +(i+1)*1000;
131  }
132 
133  return(maskID);
134 }
135 
136 int fei::FieldMask::calculateMaskID(const FieldMask& fm, int fieldID)
137 {
138 // if (fm.hasFieldID(fieldID)) return fm.maskID_;
139  return( fm.maskID_ + (fieldID+1) + (fm.numFields_+1)*1000 );
140 }
141 
int binarySearch(const T &item, const T *list, int len)
virtual ~FieldMask()
static int calculateMaskID(int numFields, const int *fieldIDs)
void addField(int fieldID, int fieldSize)
int getFieldEqnOffset(int fieldID, int &offset) const