📄 vector.h
字号:
// return: logical error status//// this method is to write the start part for writing partial data//template<class TObject>boolean Vector<TObject>::writeStart(Sof& sof_a, const String& pname_a) const { // set sof into partial write mode // sof_a.startPartialWrite(); // store the length position // sof_a.setStartPos(sof_a.tell()); sof_a.setVecSize(0); // we need an empty string for the sub-parameter // String empty_str; // 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 a temporary length // else { Long len((long)0); if (!len.writeData(sof_a)) { return Error::handle(name(), L"writeStart", Error::IO, __FILE__, __LINE__); } } // exit gracefully // return true;}// method: writePartialData//// arguments:// Sof& sof: (input) the Sof file to write// long start_pos: (input) the index of the Vector to start to write// long num_elem: (input) the number of elements to write//// return: a boolean value indicating status//template<class TObject>long Vector<TObject>::writePartialData(Sof& sof_a, long start_pos_a, long num_elem_a) const { // make sure we are in partial write mode // if (!sof_a.getPartialWrite()) { return Error::handle(name(), L"writePartialData", ERR, __FILE__, __LINE__); } // is this the first write? // boolean is_first = (sof_a.getVecSize() == 0); // for the first write, we need to write the initial skip table // if (is_first && sof_a.isBinary() && (num_elem_a > 0)) { sof_a.setLastSkipTablePos(sof_a.tell()); sof_a.setSkipTableIncr(0); sof_a.clearSkipTable(); sof_a.resize(sof_a.getObjectSize() + SKIP_TABLE_GROUP * sizeof(int32)); if (!sof_a.writeSkipTable()) { Error::handle(name(), L"writePartialData", Error::IO, __FILE__, __LINE__, Error::WARNING); } } if ((num_elem_a + start_pos_a) > length_d) { num_elem_a = length_d - start_pos_a; } // loop through the part of vector to write // for (long i = start_pos_a; i < (start_pos_a + num_elem_a); i++) { if (sof_a.isBinary()) { sof_a.resize(sof_a.getObjectSize() + v_d[i].sofSize()); } if (sof_a.isText()) { if (!is_first) { sof_a.decreaseIndention(); sof_a.puts(BLOCK_DELIM_STR); } sof_a.increaseIndention(); is_first = false; } long real_index = sof_a.getVecSize() + i - start_pos_a; if (sof_a.isBinary() && (real_index > 0) && ((real_index % SKIP_TABLE_SKIP) == 0)) { // update the current table with this position // long incr = sof_a.getSkipTableIncr(); sof_a.getSkipTable()[incr] = (int32)sof_a.tell(); sof_a.setSkipTableIncr(incr + 1); if (sof_a.getSkipTableIncr() == SKIP_TABLE_GROUP) { sof_a.setSkipTableIncr(0); // seek back and write the last bin pos table // sof_a.seek(sof_a.getLastSkipTablePos(), File::POS); if (!sof_a.writeSkipTable()) { Error::handle(name(), L"writePartialData", Error::IO, __FILE__, __LINE__); } // seek back to the current one and write out a dummy skip table // sof_a.setLastSkipTablePos(sof_a.getSkipTable()[SKIP_TABLE_GROUP - 1]); sof_a.seek(sof_a.getLastSkipTablePos(), File::POS); sof_a.clearSkipTable(); sof_a.resize(sof_a.getObjectSize() + SKIP_TABLE_GROUP * sizeof(int32)); if (!sof_a.writeSkipTable()) { return Error::handle(name(), L"writePartialData", Error::IO, __FILE__, __LINE__); } } } // write this element // if (!v_d[i].writeData(sof_a, String::EMPTY)) { return Error::handle(name(), L"writePartialData", Error::IO, __FILE__, __LINE__); } } sof_a.setVecSize(sof_a.getVecSize() + num_elem_a); // exit gracefully // return num_elem_a;}// method: writeTerminate//// arguments:// Sof& sof: (input) sof file object to write// long length_pos: (input) sof file position for the length// int32* bin_pos: (input) binary position table// long bin_pos_incr: (input) binary position table increment// long last_bin_pos: (input) where the last table was written// long acc_num: (input) total number of items written// const String& pname: (input) the parameter name//// return: a boolean value indicating status//// this method concludes the partial write and returns the write-in-full mode//template<class TObject>boolean Vector<TObject>::writeTerminate(Sof& sof_a, const String& pname_a) const { // check if we are in write partial mode // if (!sof_a.getPartialWrite()) { return Error::handle(name(), L"writeTerminate", ERR_WMODE, __FILE__, __LINE__); } // write the terminal string // if (sof_a.isText()) { // write the close brace // if (sof_a.getVecSize() > 0) { sof_a.decreaseIndention(); sof_a.puts(BLOCK_END_STR); } // possibly terminate the statement // if (pname_a.length() > 0) { sof_a.puts(BLOCK_TERM_STR); } } // for binary mode, go back and write the actual size // else { sof_a.seek(sof_a.getStartPos(), File::POS); Long size(sof_a.getVecSize()); if (debug_level_d >= Integral::ALL) { String output(L"writing size = "); output.concat(size); output.concat(L" to position "); output.concat(sof_a.tell()); Console::put(output); } size.writeData(sof_a); // now write the last skip table, if necessary // if (sof_a.getSkipTableIncr() > 0) { sof_a.seek(sof_a.getLastSkipTablePos(), File::POS); if (!sof_a.writeSkipTable()) { return Error::handle(name(), L"writeTerminate", Error::IO, __FILE__, __LINE__); } } sof_a.seek(0, File::POS_PLUS_END); } // reset the write mode // sof_a.stopPartialWrite(); // exit gracefully // return true;}//------------------------------------------------------------------------//// class-specific public methods:// get and set methods////------------------------------------------------------------------------// method: setLength// // arguments:// long length: (input) new length// boolean preserve_values: (input) should we save the memory// // return: a boolean value indicating status//// this method sets the length, or number of valid elements//template<class TObject>boolean Vector<TObject>::setLength(long length_a, boolean preserve_values_a) { // check arguments // if (length_a < 0) { return Error::handle(name(), L"setLength", Error::ARG, __FILE__, __LINE__); } // if new length is greater than capacity, call setCapacity // if (length_a > capacity_d) { if (!setCapacity(length_a, preserve_values_a)) { return Error::handle(name(), L"setLength", Error::NOMEM, __FILE__, __LINE__); } } // set new length // length_d = length_a; // exit gracefully // return true;}// method: setCapacity// // arguments:// long capacity: (input) new capacity// boolean preserve_values: (input) should we save the memory// // return: a boolean value indicating status//// this method sets the capacity, which is the maximum number of elements// this vector can hold.//template<class TObject>boolean Vector<TObject>::setCapacity(long capacity_a, boolean preserve_values_a) { // capacity_a < 0: error // if (capacity_a < 0) { return Error::handle(name(), L"setCapacity", Error::ARG, __FILE__, __LINE__); } // capacity_a = capacity_d: done // else if (capacity_a == capacity_d) { return true; } // if capacity_a < length_d: error (capacity can't be less than the length) // else if (capacity_a < (long)length_d) { return Error::handle(name(), L"setCapacity", Error::ARG, __FILE__, __LINE__); } // capacity_a == 0 (and length_d == 0): just delete memory // else if (capacity_a == 0) { // delete the old memory // if (v_d != (TObject*)NULL) { delete [] v_d; v_d = (TObject*)NULL; } } // capacity_a >= length_d: we will need to allocate memory and/or // transfer data. // else { // allocate a new chunk of memory // TObject* new_mem = new TObject[capacity_a]; if (new_mem == (TObject*)NULL) { return Error::handle(name(), L"setCapacity", Error::NOMEM, __FILE__, __LINE__); } // if there are valid elements and we need to preserve them // if (((long)length_d > 0) && preserve_values_a) { for (long i = 0; i < length_d; i++) { new_mem[i].assign(v_d[i]); } } // delete the old memory // if (v_d != (TObject*)NULL) { delete [] v_d; v_d = (TObject*)NULL; } // assign the pointer to the new memory // v_d = new_mem; } // set the new capacity // capacity_d = capacity_a; // exit gracefully // return true;}// method: getVectorSize// // arguments:// long offset: (input) the beginning point for vector// Sof& sof: (input) sof file// // return: the vector size//// this method get the length of vector in a file//template<class TObject>long Vector<TObject>::getVectorSize(long offset_a, Sof& sof_a) { if (sof_a.isText()) { return Error::handle(name(), L"getVectorSize", Error::IO, __FILE__, __LINE__); } // for binary mode, get the actual size // // get current position // long cur_file_pos = sof_a.tell(); // seek the data needed position // sof_a.seek(offset_a, File::POS); // get the length of the vector // Long len; len.readData(sof_a, String::EMPTY); // resume the file position // sof_a.seek(cur_file_pos, File::POS); if (debug_level_d >= Integral::ALL) { String output(L"readinging size = "); output.concat(len); output.concat(L" from position "); output.concat(sof_a.tell()); Console::put(output); } // exit gracefully // return len;}// method: getVectorSize// // arguments:// Sof& sof: (input) sof file object// long tag: (input) object tag// 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: the vector size//// this method get the length of vector in a file. It assumes that the// Sof file is already positioned correctly.//template<class TObject>long Vector<TObject>::getVectorSize(Sof& sof_a, long tag_a, const String& name_a, const String& pname_a, long size_a, boolean param_a, boolean nested_a) { // get the instance of the object from the Sof file // if (!sof_a.find(name_a, tag_a)) { return false; } // 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"getVectorSize", Error::READ, __FILE__, __LINE__, Error::WARNING); } if (!pname.assign(parser.implicitPname())) { return Error::handle(name(), L"getVectorSize", 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(DEF_PARAM); new_size.debug(L"new_size"); } // for binary mode, get the actual size // else { if (!new_size.readData(sof_a, pname)) { return Error::handle(name(), L"getVectorSize", Error::READ, __FILE__, __LINE__, Error::WARNING); } } // exit gracefully // return (long)new_size;}//---------------------------------------------------------------------------//// class-specific public methods:// item manipulation methods////---------------------------------------------------------------------------// method: move//// arguments:// const Vector& v: (input) input Vector// long n: (input) number of elements to be moved// long i_offset: (input) index of first element in input Vector// long o_offset: (input) index of first element in output Vector//// return: logical error status//// note that this method now does appropriate boundary checking/processing.//template<class TObject>boolean Vector<TObject>::move(const Vector& v_a, long n_a, long i_offset_a, long o_offset_a) { // declare local variables // long num_to_move = n_a; // counter for moving long ind_in = i_offset_a; // start index in input long ind_in_end = ind_in + num_to_move; // end index in input long ind_out = o_offset_a; // start index in output long ind_out_end = ind_out + num_to_move; // end index in output long last_index = (long)v_a.length_d; // last index in input // check output vector
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -