📄 vector.h
字号:
String numeric; // dump the length // value.assign((long)length_d); output.debugStr(name(), message_a, L"length_d", value); Console::put(output); // dump the capacity // value.assign((long)capacity_d); output.debugStr(name(), message_a, L"capacity_d", value); Console::put(output); // dump the values // for (long i = 0; i < (long)length_d; i++) { param.assign(L"v_d["); value.assign(i); param.concat(value); param.concat(L"]"); output.debugStr(name(), message_a, param); Console::put(output); // increase indention // Console::increaseIndention(); // call the debug method of the element // v_d[i].debug(L""); Console::decreaseIndention(); } // exit gracefully // return true;}//------------------------------------------------------------------------//// required destructor/constructor(s)////-----------------------------------------------------------------------// method: destructor//// arguments: none//// return: none//// this is the default destructor for the Vector class//template<class TObject>Vector<TObject>::~Vector() { // free memory // clear(Integral::RELEASE); // exit gracefully //}// method: default constructor//// arguments:// long length: (input) number of elements//// return: none//template<class TObject>Vector<TObject>::Vector(long length_a) { // initialize data // length_d = (long)0; capacity_d = (long)0; v_d = (TObject*)NULL; // set the length // if (!setLength(length_a)) { Error::handle(name(), L"default constructor", Error::NOMEM, __FILE__, __LINE__); } // exit gracefully //}// method: copy constructor//// arguments:// const Vector<TObject>& copy_vector: (input) the vector to copy//// return: none//template<class TObject>Vector<TObject>::Vector(const Vector<TObject>& copy_vector_a) { // initialize data // length_d = (long)0; capacity_d = (long)0; v_d = (TObject*)NULL; // call the assign method to copy the vector // assign(copy_vector_a); // exit gracefully //}//------------------------------------------------------------------------//// required assign methods////-------------------------------------------------------------------------// method: assign//// arguments:// const Vector<TObject>& copy_vector: (input) the vector to copy//// return: a boolean value indicating status//// this method copies the contents of the input to this vector//template<class TObject>boolean Vector<TObject>::assign(const Vector<TObject>& copy_vector_a) { // resize the vector // if (!setLength(copy_vector_a.length(), false)) { return Error::handle(name(), L"assign", Error::NOMEM, __FILE__, __LINE__); } // copy the data // long last_index = (long)length_d; for (long index = 0; index < last_index; index++) { v_d[index].assign(copy_vector_a.v_d[index]); } // exit gracefully // return true;}//------------------------------------------------------------------------//// required i/o methods////------------------------------------------------------------------------// method: sofSize//// arguments: none//// return: size of object as written to disk via the i/o methods//// this method determines the size of the object on disk//template<class TObject>long Vector<TObject>::sofSize() const { // start with the length // long bytes = length_d.sofSize(); // add each element // for (long i = 0; i < length_d; i++) { if ((i % (SKIP_TABLE_SKIP * SKIP_TABLE_GROUP)) == 0) { bytes += sizeof(int32) * SKIP_TABLE_GROUP; } bytes += v_d[i].sofSize(); } // return the size // return bytes;}// method: read//// arguments:// Sof& sof: (input) sof file object// long tag: (input) sof object instance tag// const String& name: (input) sof object instance name//// return: a boolean value indicating status//// this method has the object read itself from an Sof file//template<class TObject>boolean Vector<TObject>::read(Sof& sof_a, long tag_a, const String& name_a) { // get the instance of the object from the Sof file // if (!sof_a.find(name_a, tag_a)) { return false; } // read the actual data from the sof file // if (!readData(sof_a)) { return false; } // exit gracefully // return true;}// method: write//// arguments:// Sof& sof: (input) sof file object// long tag: (input) sof object instance tag// const String& name: (input) sof object instance name//// return: a boolean value indicating status//// this method has the object write itself to an Sof file//template<class TObject>boolean Vector<TObject>::write(Sof& sof_a, long tag_a, const String& name_a) const { // declare a temporary size variable // long obj_size = 0; // switch on ascii or binary mode // if (sof_a.isText()) { // set the size to be dynamic // obj_size = Sof::ANY_SIZE; } else { // the size of the binary data to write // obj_size = sofSize(); } // write the object into the sof file's index // if (!sof_a.put(name_a, tag_a, obj_size)) { return false; } // exit gracefully // return writeData(sof_a);}// method: readData//// arguments:// Sof& sof: (input) sof file object// const String& pname: (input) parameter name// long size: (input) size of the object// boolean param: (input) is the parameter specified?// boolean nested: (input) is this nested?//// return: a boolean value indicating status//// this method has the object read itself from an Sof file. it assumes// that the Sof file is already positioned correctly.//template<class TObject>boolean Vector<TObject>::readData(Sof& sof_a, const String& pname_a, long size_a, boolean param_a, boolean nested_a) { // first cleanup the list // if (!clear(Integral::FREE)) { return Error::handle(name(), L"readData", Error::READ, __FILE__, __LINE__, Error::WARNING); } // local variables // SofParser parser; String pname; // if param is false, this means implicit parameter // if (!param_a) { if (!parser.setImplicitParam()) { return Error::handle(name(), L"readData", Error::READ, __FILE__, __LINE__, Error::WARNING); } if (!pname.assign(parser.implicitPname())) { return Error::handle(name(), L"readData", Error::READ, __FILE__, __LINE__, Error::WARNING); } } else { pname.assign(pname_a); } // are we nested? // if (nested_a) { parser.setNest(); } // load the parse // parser.load(sof_a, size_a); Long new_size((long)0); // read the length first: this differs for text or binary // if (sof_a.isText()) { new_size = parser.countTokens(pname); } // binary mode // else { if (!new_size.readData(sof_a, pname)) { return Error::handle(name(), L"readData", Error::READ, __FILE__, __LINE__, Error::WARNING); } } if (debug_level_d >= Integral::DETAILED) { new_size.debug(L"new_size"); } // set length destructively // if (!setLength(new_size, false)) { return Error::handle(name(), L"readData", Error::ARG, __FILE__, __LINE__); } // we need to also read in the skip table. we verify the values in // the skip table as a sort of checksum // static int32 bin_pos[SKIP_TABLE_GROUP]; long skip_pos = 0; long skip_incr = 0; // read a node at a time // for (long i = 0; i < new_size; i++) { if (sof_a.isBinary() && ((i % (SKIP_TABLE_SKIP * SKIP_TABLE_GROUP)) == 0)) { if (debug_level_d >= Integral::ALL) { String output(L"reading skip table from position "); output.concat(sof_a.tell()); Console::put(output); } if (i > 0) { skip_pos = bin_pos[SKIP_TABLE_GROUP - 1] + SKIP_TABLE_LENGTH; skip_incr = 0; } if (sof_a.read(bin_pos, sizeof(int32), SKIP_TABLE_GROUP) != SKIP_TABLE_GROUP) { return Error::handle(name(), L"readData", Error::ARG, __FILE__, __LINE__, Error::WARNING); } if (i == 0) { skip_pos = bin_pos[skip_incr++]; } } if (sof_a.isBinary()) { // verify the values in the skip table // if ((i > 0) && ((i % SKIP_TABLE_SKIP) == 0)) { long cur_pos = sof_a.tell(); if (skip_pos != cur_pos) { String output(L"skip_pos = "); output.concat(skip_pos); output.concat(L", cur_pos = "); output.concat(cur_pos); output.concat(L", skip_incr = "); output.concat(skip_incr); output.concat(L", i = "); output.concat(i); Console::put(output); return Error::handle(name(), L"readData", Error::READ, __FILE__, __LINE__, Error::WARNING); } skip_pos = bin_pos[skip_incr++]; } } // read the node // if (!v_d[i].readData(sof_a, pname, parser.getEntry(sof_a, pname, i, 1), false, true)) { return Error::handle(name(), L"readData", Error::WARNING, __FILE__, __LINE__, Error::WARNING); } } // exit gracefully // return true;}// method: writeData//// arguments:// Sof& sof: (input) sof file object// const String& pname: (input) parameter name//// return: a boolean value indicating status//// this method writes the object to the Sof file. it assumes that the// Sof file is already positioned correctly.//template<class TObject>boolean Vector<TObject>::writeData(Sof& sof_a, const String& pname_a) const { // make sure we are in write-in-full mode // if (sof_a.getPartialWrite()) { return Error::handle(name(), L"writeData", ERR_WMODE, __FILE__, __LINE__); } // we need an empty string for the sub-parameter // String empty_str; // for binary writes we will need an array of positions // long bin_pos_incr = 0; int32 bin_pos[SKIP_TABLE_GROUP]; long last_bin_pos = 0; for (long i = 0; i < SKIP_TABLE_GROUP; i++) { bin_pos[i] = -1; } // if text, write a parameter name. this can't be done with // writeLabelPrefix because an empty list gets no brackets // if (sof_a.isText()) { if (pname_a.length() > 0) { String output; output.assign(pname_a); output.concat(SofParser::SPACE_CHAR); output.concat(SofParser::DEF_ASSIGNMENT_CHAR); output.concat(SofParser::SPACE_CHAR); sof_a.puts(output); } if ((long)length_d > 0) { sof_a.puts(BLOCK_START_STR); } } // for binary, write length and bin pos // else { if (!length_d.writeData(sof_a)) { return Error::handle(name(), L"writeData", Error::IO,__FILE__,__LINE__); } last_bin_pos = sof_a.tell(); if ((long)length_d > 0) { if (!sof_a.write(bin_pos, sizeof(int32), SKIP_TABLE_GROUP)) { return Error::handle(name(), L"writeData", Error::IO, __FILE__, __LINE__); } } } boolean is_first = true; // loop through the vector // for (long i = 0; i < length_d; i++) { if (sof_a.isText()) { if (!is_first) { sof_a.decreaseIndention(); sof_a.puts(BLOCK_DELIM_STR); } sof_a.increaseIndention(); is_first = false; } // do we need to update the skip table? // if (sof_a.isBinary() && (i > 0) && ((i % SKIP_TABLE_SKIP) == 0)) { // update the current table with this position // bin_pos[bin_pos_incr++] = (int32)sof_a.tell(); if (bin_pos_incr == SKIP_TABLE_GROUP) { bin_pos_incr = 0; // seek back and write the last bin pos table // sof_a.seek(last_bin_pos, File::POS); if (!sof_a.write(bin_pos, sizeof(int32), SKIP_TABLE_GROUP)) { return Error::handle(name(), L"writeData", Error::IO,__FILE__,__LINE__); } // seek back to the current one // last_bin_pos = bin_pos[SKIP_TABLE_GROUP - 1]; sof_a.seek(last_bin_pos, File::POS); // write out the next dummy table // clearBinPos(bin_pos); if (!sof_a.write(bin_pos, sizeof(int32), SKIP_TABLE_GROUP)) { return Error::handle(name(), L"writeData", Error::IO,__FILE__,__LINE__); } } } // write this element // if (!v_d[i].writeData(sof_a, empty_str)) { return Error::handle(name(), L"writeData",Error::IO, __FILE__, __LINE__); } } if (sof_a.isText()) { // write the close brace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -