Changeset 254 for trunk/src/mesh
- Timestamp:
- Mar 2, 2006 9:07:05 AM (18 years ago)
- Location:
- trunk/src/mesh
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/mesh/grid1d.cc
r185 r254 13 13 RpGrid1d::RpGrid1d(int size) 14 14 { 15 m_data.res ize(size);15 m_data.reserve(size); 16 16 } 17 17 … … 41 41 } 42 42 43 // 44 // Add all points to grid1d object at once 45 // 46 RP_ERROR 47 RpGrid1d::addAllPoints(double* val, int nitems) 48 { 49 if (val == NULL) 50 return RP_ERR_NULL_PTR; 51 52 if (nitems != numPoints()) 53 m_data.resize(nitems); 54 55 for (int i=0; i < nitems; i++) 56 m_data[i] = val[i]; 57 58 return RP_SUCCESS; 59 } 60 61 // 62 // add one point to grid1d 63 // 43 64 void RpGrid1d::addPoint(double val) 44 65 { … … 55 76 // 56 77 char* 57 RpGrid1d::serialize(RP_ENCODE_ALG encodeFlag, RP_COMPRESSION compressFlag) 78 RpGrid1d::serialize(int& nb, RP_ENCODE_ALG encodeFlag, 79 RP_COMPRESSION compressFlag) 58 80 { 59 81 int npts = m_data.size(); 82 int nbytes = npts*sizeof(double) + sizeof(int) + 2; 83 char* buf; 60 84 61 85 // total length = tagEncode + tagCompress + num + array data 62 char * buf = (char*) malloc(npts*sizeof(double) + sizeof(int) + 2); 63 if (buf == NULL) { 86 if ( (buf = (char*) malloc(nbytes)) == NULL) { 64 87 RpAppendErr("RpGrid1d::serialize: malloc failed"); 65 88 RpPrintErr(); … … 67 90 } 68 91 92 serialize(buf, nbytes, encodeFlag, compressFlag); 93 nb = nbytes; 94 95 return buf; 96 } 97 98 RP_ERROR 99 RpGrid1d::serialize(char* buf, int nbytes, RP_ENCODE_ALG encodeFlag, 100 RP_COMPRESSION compressFlag) 101 { 102 int npts = m_data.size(); 103 104 if (buf == NULL || (unsigned)nbytes < (npts*sizeof(double)+sizeof(int)+2)) { 105 RpAppendErr("RpGrid1d::serialize: invalid buffer"); 106 RpPrintErr(); 107 return RP_ERR_INVALID_ARRAY; 108 } 109 69 110 char* ptr = buf; 70 71 111 ptr[0] = 'N'; // init to no-encoding 72 112 switch(encodeFlag) { … … 88 128 break; 89 129 } 130 ptr += 2; // advance pointer 90 131 91 132 // TODO encode, compression 92 // 133 // compress() 93 134 94 135 // write to stream buffer 95 ptr += 2;96 136 memcpy((void*)ptr, (void*)&npts, sizeof(int)); 97 137 ptr += sizeof(int); 98 138 memcpy((void*)ptr, (void*)&(m_data[0]), npts*sizeof(double)); 99 139 100 return buf;101 } 102 103 int140 return RP_SUCCESS; 141 } 142 143 RP_ERROR 104 144 RpGrid1d::deserialize(const char* buf) 105 145 { … … 134 174 } 135 175 136 137 //TODO: overload operators [], = 176 void 177 RpGrid1d::xmlString(std::string& textString) 178 { 179 int i; 180 int npts = m_data.size(); 181 char cstr[256]; 182 std::string str; 183 184 // clear input string 185 textString.erase(); 186 187 textString.append("<value>"); 188 189 for (i=0; i < npts; i++) { 190 sprintf(cstr, "\t%.15f\n", m_data[i]); 191 textString.append(cstr); 192 } 193 textString.append("</value>\n"); 194 } 195 196 void 197 RpGrid1d::print() 198 { 199 string str; 200 xmlString(str); 201 printf("%s", str.c_str()); 202 } 203 138 204 139 205 // TODO -
trunk/src/mesh/grid1d.h
r185 r254 17 17 RpGrid1d(const char* buf); // instantiate with byte stream 18 18 19 // add all points to grid 20 RP_ERROR addAllPoints(double* val, int nitems); 21 22 // add one point to grid 19 23 void addPoint(double val); 20 24 … … 22 26 int numPoints() { return m_data.size(); } 23 27 28 // max num points that can be stored 29 int capacity() { return m_data.capacity(); } 30 24 31 // change the size of the grid after grid is constructed 25 32 void resize(int npoints) { m_data.resize(npoints); } 26 33 27 34 // serialize data 28 char * serialize(RP_ENCODE_ALG eflag=RP_NO_ENCODING, 35 // If no input parameters are provided, byte stream will be binary 36 // without compression. 37 // 38 char * serialize(int& nbytes, RP_ENCODE_ALG eflag=RP_NO_ENCODING, 29 39 RP_COMPRESSION cflag=RP_NO_COMPRESSION); 30 int deserialize(const char* buf); 40 41 RP_ERROR serialize(char * buf, int nbytes, 42 RP_ENCODE_ALG eflag=RP_NO_ENCODING, 43 RP_COMPRESSION cflag=RP_NO_COMPRESSION); 44 45 RP_ERROR deserialize(const char* buf); 46 47 void xmlString(std::string& str); 48 void print(); 31 49 32 50 // destructor
Note: See TracChangeset
for help on using the changeset viewer.