📄 vector.h
字号:
// if ((long)length_d > 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 update the last bin_pos table // else { if (bin_pos_incr > 0) { long cur_pos = sof_a.tell(); 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__); } sof_a.seek(cur_pos, File::POS); } } // exit gracefully // return true;}//------------------------------------------------------------------------//// required equality methods////------------------------------------------------------------------------// method: eq//// arguments:// const Vector<TObject>& compare_vector: (input) the vector to compare//// return: a boolean value indicating status//// this method compares two vectors for equivalence. two vectors are equivalent// if all corresponding items are equivalent//template<class TObject>boolean Vector<TObject>::eq(const Vector<TObject>& compare_vector_a) const { // declare the output variable // boolean are_equal = true; // two vectors can not be equivalent if they are of differing lengths // if (length_d != compare_vector_a.length_d) { // set the break flag // are_equal = false; } // loop over each element and see if each is equivalent // for (long i = 0; are_equal && (i < (long)length_d); i++) { // see if the current items are equal // are_equal = v_d[i].eq(compare_vector_a.v_d[i]); } // return a value representing if they are equivalent // return are_equal;}//-------------------------------------------------------------------------//// required memory management methods////-------------------------------------------------------------------------// method: clear//// arguments: // Integral::CMODE cmode_a: (input) clear mode// // return: a boolean value indicating status//// this method clears the contents of the list by the setting of cmode_a//// enum CMODE { RETAIN = 0, RESET, RELEASE, FREE, DEF_CMODE = RESET };//// RETAIN: call clear with RETAIN on each element in the vector//// RESET: clear the structure but don't necessarily delete memory//// RELEASE: clear the structure and release memory//// FREE: clear the structure and release memory//// Programming hint://// use the clear() method to manage the memory of the objects going// into the vector.// particular useful when vector is in USER mode. Caution the object// you place into vector must have a clear method that meets the// requirements of the IFC clear method.////template<class TObject>boolean Vector<TObject>::clear(Integral::CMODE cmode_a) { // if the cmode_a is RETAIN or FREE, call clear(cmode_a) method for // each element // if ((cmode_a == Integral::RETAIN) || (cmode_a == Integral::FREE)) { // loop over all elements // long i_end = length_d; for (long i = 0; i < i_end; i++) { v_d[i].clear(cmode_a); } } // if the cmode_a is RESET, clear the structure but don't // necessarily delete memory // if (cmode_a == Integral::RESET) { setLength(0); } // if the cmode_a is RELEASE or FREE, clear the structure and // release memory. // else if ((cmode_a == Integral::RELEASE) || (cmode_a == Integral::FREE)) { // free memory -- the setCapacity call will actually release it. // length_d = 0; setCapacity(0); } // exit gracefully // return true;}//-------------------------------------------------------------------------//// class-specific public methods:// extensions to required methods////-------------------------------------------------------------------------// method: assign//// arguments:// TObject& value: (input) sets all elements equal to value_a//// return: a boolean value - true on successful completion// template<class TObject>boolean Vector<TObject>::assign(TObject& value_a) { // assign each element // long last_index = (long)length_d; if (last_index < (long)0) { return false; } for (long index = 0; index < last_index; index++) { v_d[index].assign(value_a); } // exit gracefully // return true;}// method: readStart//// 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: logical error status//// this method allocate an SofParser object and load the parse. it is// used for partial read.//template<class TObject>boolean Vector<TObject>::readStart(Sof& sof_a, const String& pname_a, long size_a, boolean param_a, boolean nested_a) { // local variable // Long new_size((long)0); // first cleanup the list // if (!clear(Integral::RELEASE)) { return Error::handle(name(), L"readStart", Error::MEM, __FILE__, __LINE__); } // start the partial read in Sof // sof_a.startPartialRead(); // if text, read in a line, else binary read // if (sof_a.isText()) { String pname; // set the parser debug level // sof_a.getVecParser().setDebug(debug_level_d); // if param is false, this means implicit parameter // if (!param_a) { sof_a.getVecParser().setImplicitParam(); pname.assign(SofParser::implicitPname()); } else { pname.assign(pname_a); } // are we nested? // if (nested_a) { sof_a.getVecParser().setNest(); } // load the parse // sof_a.getVecParser().load(sof_a, size_a); new_size = sof_a.getVecParser().countTokens(pname); } // binary mode // else { sof_a.setStartPos(sof_a.tell()); if (!new_size.readData(sof_a, String::EMPTY)) { return false; } // go ahead and read the first skip table as well // if ((long)new_size > 0) { if (!sof_a.readSkipTable()) { return Error::handle(name(), L"readStart", Error::IO, __FILE__, __LINE__); } } } sof_a.setVecSize((long)new_size); sof_a.setVecCurrentElement((long)0); // exit gracefully // return true;}// method: readPartialData//// arguments:// Sof& sof: (input) sof file object// long start_pos: (input) first entry to read// long num_elem: (input) number of elements to read// const String& pname: (input) parameter name// long size: (input) size in bytes of object (or FULL_SIZE)// boolean param: (input) is the parameter name in the file?// boolean nested: (input) are we nested?//// return: the number of elements read//// this method has the object read itself from an Sof file. it assumes// that the Sof file is already positioned correctly.//template<class TObject>long Vector<TObject>::readPartialData(Sof& sof_a, long start_pos_a, long num_elem_a, const String& pname_a, long size_a, boolean param_a, boolean nested_a) { // check the arguments // if (num_elem_a <= 0) { setLength(0); return (long)0; } if (start_pos_a < 0) { return Error::handle(name(), L"readPartialData", Error::ARG, __FILE__, __LINE__); } // declare local variable // String pname; // if param is false, this means implicit parameter // if (!param_a) { sof_a.getVecParser().setImplicitParam(); pname.assign(SofParser::implicitPname()); } else { pname.assign(pname_a); } if (start_pos_a > sof_a.getVecSize()) { return (long)0; } else if ((start_pos_a + num_elem_a) > sof_a.getVecSize()) { num_elem_a = sof_a.getVecSize() - start_pos_a; } // set length destructively // setLength(num_elem_a, false); if (num_elem_a > 0) { if (sof_a.isText()) { // read a node at a time // for (long i = 0; i < num_elem_a; i++) { // read the node // long count = i + start_pos_a; if (!v_d[i].readData(sof_a, pname, sof_a.getVecParser().getEntry(sof_a, pname, count, 1), false, true)) { return Error::handle(name(), L"readPartialData", SofParser::ERR, __FILE__, __LINE__, Error::WARNING); } } sof_a.setVecCurrentElement(start_pos_a + num_elem_a); } // binary read // else { // do we need to seek backwards? // long cur_elem = sof_a.getVecCurrentElement(); // start back at zero: BUGBUG -- we may be able to seek back // to the begining of the current skip table // boolean restart = false; if (start_pos_a < cur_elem) { restart = true; } // possibly jump ahead // long seek_pos = -1; long skip_table_incr = sof_a.getSkipTableIncr(); // if incrementing will take us past a skip table boundary, take // the current element back to the previous skip table block // start. for instance, if cur = 70 and start = 20. without // reseting this would NOT cause a jump since it is only going // 50 elements (and the skip table size is 100. BUT, it would be // faster to jump to 100 first and then just walk through 20 // elements rather than walking through all 50, so we will set // cur back to 0. // if (((start_pos_a % SKIP_TABLE_SKIP) < (cur_elem % SKIP_TABLE_SKIP)) || ((start_pos_a - cur_elem) > SKIP_TABLE_SKIP)) { cur_elem -= (cur_elem % SKIP_TABLE_SKIP); if (skip_table_incr > 0) { seek_pos = sof_a.getSkipTable()[skip_table_incr - 1]; } else { seek_pos = sof_a.getLastSkipTablePos() + (SKIP_TABLE_GROUP * sizeof(int32)); } if (debug_level_d >= Integral::ALL) { String output(L"going back to block start: cur_elem = "); output.concat(cur_elem); output.concat(L", skip_table_incr = "); output.concat(skip_table_incr); output.concat(L", seek_pos = "); output.concat(seek_pos); Console::put(output); } } if (restart) { sof_a.setVecCurrentElement((long)0); sof_a.seek(sof_a.getStartPos() + length_d.sofSize(), File::POS); sof_a.readSkipTable(); sof_a.setSkipTableIncr(0); return readPartialData(sof_a, start_pos_a, num_elem_a, pname_a); } while ((start_pos_a - cur_elem) >= SKIP_TABLE_SKIP) { cur_elem += SKIP_TABLE_SKIP; seek_pos = sof_a.getSkipTable()[skip_table_incr++]; if (skip_table_incr == SKIP_TABLE_GROUP) { sof_a.seek(seek_pos, File::POS); skip_table_incr = 0; sof_a.setLastSkipTablePos(sof_a.tell()); if (debug_level_d >= Integral::ALL) { String output(L"reading skip table from position "); output.concat(sof_a.tell()); Console::put(output); } if (!sof_a.readSkipTable()) { return Error::handle(name(), L"readPartialData", Error::READ, __FILE__, __LINE__, Error::WARNING); } seek_pos += SKIP_TABLE_GROUP * sizeof(int32); } if (debug_level_d >= Integral::ALL) { String output(L"jumping: cur_elem = "); output.concat(cur_elem); output.concat(L", skip_table_incr = "); output.concat(skip_table_incr); output.concat(L", seek_pos = "); output.concat(seek_pos); Console::put(output); } } if (seek_pos > 0) { sof_a.seek(seek_pos, File::POS); } // loop past the data before the section we are looking for // for (long i = cur_elem; i < start_pos_a; i++) { if ((i > cur_elem) && ((i % SKIP_TABLE_SKIP) == 0)) { return Error::handle(name(), L"readPartialData", ERR, __FILE__, __LINE__); } if (debug_level_d >= Integral::ALL) { String output(L"skipping over object "); output.concat(i); output.concat(L", position = "); output.concat(sof_a.tell()); Console::put(output); } TObject v_tmp; v_tmp.readData(sof_a, pname_a, sof_a.getVecParser().getEntry(sof_a, pname, i , 1), false, true); } cur_elem = start_pos_a; // now read the useful data // for (long i = 0; i < num_elem_a; i++) { if (((i > 0) && (((i + start_pos_a) % SKIP_TABLE_SKIP) == 0))) { skip_table_incr++; // if (((i + start_pos_a) % // (SKIP_TABLE_SKIP * SKIP_TABLE_GROUP)) == 0) { if (skip_table_incr == SKIP_TABLE_GROUP) { sof_a.setLastSkipTablePos(sof_a.tell()); if (!sof_a.readSkipTable()) { return Error::handle(name(), L"readPartialData", Error::READ, __FILE__, __LINE__, Error::WARNING); } skip_table_incr = 0; } } } if (!v_d[i].readData(sof_a, pname_a, sof_a.getVecParser().getEntry(sof_a, pname, i, 1), false, true)) { return (long)0; } } sof_a.setSkipTableIncr(skip_table_incr); sof_a.setVecCurrentElement(cur_elem + num_elem_a); if(((cur_elem+num_elem_a)%(SKIP_TABLE_SKIP * SKIP_TABLE_GROUP)) == 0) { { sof_a.setLastSkipTablePos(sof_a.tell()); if (!sof_a.readSkipTable()) { return Error::handle(name(), L"readPartialData", Error::READ, __FILE__, __LINE__, Error::WARNING); } skip_table_incr = 0; } } } } // exit gracefully // return num_elem_a;}// method: writeStart//// arguments:// Sof& sof: (input) sof file object// const String& pname: (input) parameter name//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -