📄 vector.h
字号:
// if ((ind_in < 0) || (ind_out < 0) || (ind_out > (long)length_d)) { return Error::handle(name(), L"move", Error::ARG, __FILE__, __LINE__); } else if ((ind_out_end > (long)length_d)) { // resize this vector // setLength(ind_out_end); } // subtract amount to be zeroed at the end // if (ind_in_end > last_index) { num_to_move -= ind_in_end - last_index; } // move the data // while (num_to_move > 0) { v_d[ind_out++].assign(v_a.v_d[ind_in++]); num_to_move--; } // reset the values at end if necessary // while (ind_out < ind_out_end) { v_d[ind_out++].clear(); } // exit gracefully // return true;}// method: shift//// arguments:// const Vector& v: (input) source vector// long delta: (input) shift factor//// return: a boolean value indicating status//// this methods shifts the position of the elements of the vector// if delta_a > 0: x[i] = x[i-delta], if delta_a < 0: x[i] = x[i+delta]//template<class TObject>boolean Vector<TObject>::shift(const Vector& v_a, long delta_a) { // create new space // if (!setLength((long)v_a.length_d, false)) { return false; } // clear the space // clear(Integral::RETAIN); // shift the data forward // if (delta_a > 0) { long i = (long)length_d - 1; long j = i - delta_a; long i_end = delta_a - 1; while (i > i_end) { v_d[i--].assign(v_a.v_d[j--]); } } // shift the data backward // else { long i = 0; long j = i - delta_a; long i_end = (long)length_d + delta_a; while (i < i_end) { v_d[i++].assign(v_a.v_d[j++]); } } // exit gracefully // return true;}// method: shift//// arguments:// long delta: (input) shift factor//// return: a boolean value indicating status//// shift the specified amount// delta_a > 0: x[i] = x[i-delta]// delta_a < 0: x[i] = x[i+delta]//template<class TObject>boolean Vector<TObject>::shift(long delta_a) { // shift the data forward // if (delta_a > 0) { long i = (long)length_d - 1; long j = i - delta_a; long i_end = delta_a - 1; while (i > i_end) { v_d[i--].assign(v_d[j--]); } while (i >= 0) { v_d[i--].clear(); } } // shift the data backward // else { long i = 0; long j = i - delta_a; long i_end = (long)length_d + delta_a; while (i < i_end) { v_d[i++].assign(v_d[j++]); } while (i < length_d) { v_d[i++].clear(); } } // exit gracefully // return true;}// method: concat//// arguments:// const Vector& v: (input) Vector to be concatenated //// return: a boolean value indicating status//// this method concatenates v_a to the end of "this".//template<class TObject>boolean Vector<TObject>::concat(const Vector& v_a) { // declare local variables // long output_index = (long)length_d; long last_index = (long)v_a.length_d; long index; // resize (we must preserve elements) // if (!setLength(length_d + v_a.length_d)) { return Error::handle(name(), L"concat", Error::NOMEM, __FILE__, __LINE__); } // copy the data // for (index = 0; index < last_index; index++) { v_d[output_index++].assign(v_a.v_d[index]); } // exit gracefully // return true;}// method: concat//// arguments:// const Vector& v1: (input) operand// const Vector& v2: (input) operand//// return: a boolean value indicating status//// concatenates v2 to v1 and stored the result in "this".//template<class TObject>boolean Vector<TObject>::concat(const Vector& v1_a, const Vector& v2_a) { // make sure the length is great enough for both // if (!setLength(v1_a.length_d + v2_a.length_d, false)) { return Error::handle(name(), L"concat", Error::NOMEM, __FILE__, __LINE__); } setLength(0); // assign the first vector // if (!assign(v1_a)) { return Error::handle(name(), L"concat", ERR, __FILE__, __LINE__); } // call the master function to concatenate // return concat(v2_a);}// method: concat//// arguments:// const TObject& obj: (input) object to be concatenated //// return: a boolean value indicating status//// this method concatenates obj to the end of "this".//template<class TObject>boolean Vector<TObject>::concat(const TObject& obj_a) { // declare local variables // long output_index = (long)length_d; // resize (we must preserve elements) // if (!setLength((long)length_d + 1)) { return Error::handle(name(), L"concat", Error::NOMEM, __FILE__, __LINE__); } // copy the data // v_d[output_index].assign(obj_a); // exit gracefully // return true;}// method: deleteRange//// arguments:// const Vector& arg: (input) input vector// long offset: (input) first element to delete// long num_elem: (input) number of elements to delete//// return: a boolean value indicating status//template<class TObject>boolean Vector<TObject>::deleteRange(const Vector& arg_a, long offset_a, long num_elem_a) { // make sure this isn't the same object // if (&arg_a == this) { return deleteRange(offset_a, num_elem_a); } // local variables // long len_old = arg_a.length(); long len_new = len_old - num_elem_a; // check arguments // if (num_elem_a == 0) { return assign(arg_a); } else if (((offset_a < 0) || (offset_a >= len_old)) || ((num_elem_a < 0) || ((offset_a + num_elem_a) > len_old))) { return Error::handle(name(), L"deleteRange", Error::ARG, __FILE__, __LINE__); } setLength(len_new); // copy the first chunk // for (long i = 0; i < offset_a; i++) { v_d[i].assign(arg_a.v_d[i]); } // copy the second chunk // for (long i = offset_a; i < len_new; i++) { v_d[i].assign(v_d[i + num_elem_a]); } // exit gracefully // return true;}// method: deleteRange//// arguments:// long offset: (input) first element to delete// long num_elem: (input) number of elements to delete//// return: a boolean value indicating status//template<class TObject>boolean Vector<TObject>::deleteRange(long offset_a, long num_elem_a) { // local variables // long len_old = length(); long len_new = len_old - num_elem_a; // check arguments // if (num_elem_a == 0) { return true; } else if (((offset_a < 0) || (offset_a >= len_old)) || ((num_elem_a < 0) || ((offset_a + num_elem_a) > len_old))) { return Error::handle(name(), L"deleteRange", Error::ARG, __FILE__, __LINE__); } // copy the second chunk // for (long i = offset_a; i < len_new; i++) { v_d[i].assign(v_d[i + num_elem_a]); } // resize // return setLength(len_new);}// method: setRange//// arguments:// long offset: (input) first element to delete// long num_elem: (input) number of elements to delete// const TObject& value: (input) value to set//// return: a boolean value indicating status//template<class TObject>boolean Vector<TObject>::setRange(long offset_a, long num_elem_a, const TObject& value_a) { // local variables // long len = length(); // check arguments // if (num_elem_a == 0) { return true; } else if (((offset_a < 0) || (offset_a >= len)) || ((num_elem_a < 0) || ((offset_a + num_elem_a) > len))) { return Error::handle(name(), L"setRange", Error::ARG, __FILE__, __LINE__); } // assign each element // for (long i = offset_a; i < (offset_a + num_elem_a); i++) { if (!v_d[i].assign(value_a)) { return false; } } // exit gracefully // return true;} // method: sort//// arguments:// Integral::ORDER sort_order: (input) the order to sort// SORT_ALGO sort_algo: (input) the algorithm to use//// return: a boolean value indicating status//// this method sorts the elements in the vector according to the given order//template<class TObject>boolean Vector<TObject>::sort(Integral::ORDER sort_order_a, SORT_ALGO sort_algo_a) { // branches on algorithms // if (sort_algo_a == RAND_QUICK) { return randQuickSort(sort_order_a); } else if (sort_algo_a == INSERTION) { return insertionSort(sort_order_a); } else { return Error::handle(name(), L"sort", Error::NOT_IMPLEM, __FILE__, __LINE__, Error::WARNING); }}//---------------------------------------------------------------------------//// class-specific public methods:// item location and containment methods////---------------------------------------------------------------------------// method: first//// arguments:// const TObject& value: (input) value to be found// long start_pos: (input) position to start searching from//// return: a long value of the element index//// this method finds the index of the first element after the start_pos// (count from 0) in "this" equal to value_a, if the value is not found,// NO_POS is returned.//template<class TObject>long Vector<TObject>::first(const TObject& value_a, long start_pos_a) const { long last_index = (long)length_d; // make sure the range of start_pos is between [0, length_d-1] // if ((start_pos_a == Integral::NO_POS) || (start_pos_a >= length_d)) { start_pos_a = 0; } // check all values // for (long index = start_pos_a; index < last_index; index++) { if (v_d[index].eq(value_a)) { return index; } } // exit gracefully - no match found // return Integral::NO_POS;}// method: last//// arguments:// const TObject& value: (input) value to be found// long end_pos: (input) position to start searching back from//// return: a long value of the element index//// this method finds the index of the first element after the start_pos// in "this" equal to value_a, if the value is not found, NO_POS is// returned. //template<class TObject>long Vector<TObject>::last(const TObject& value_a, long end_pos_a) const { // make sure the range of end_pos is between [0, length_d-1] // if (end_pos_a == Integral::NO_POS || end_pos_a >= length_d) { end_pos_a = (long)length_d - 1; } // check all values // for (long index = end_pos_a; index >= 0; index--) { if (v_d[index].eq(value_a)) { return index; } } // exit gracefully - no match found // return Integral::NO_POS;}//------------------------------------------------------------------------//// class-specific public methods:// apply methods////------------------------------------------------------------------------// method: apply//// arguments:// boolean (TObject::*method)(): (input) the method to apply to each element// this method must be a member of the TObject class//// return: a boolean value indicating status//// this method applies the input method to each element in the vector//template<class TObject>boolean Vector<TObject>::apply(boolean (TObject::*method_a)()) { // loop over each element in the vector and apply the method to it // for (long i = 0; i < length_d; i++) { // apply the method to all non-null elements // (v_d[i].*method_a)(); } // exit gracefully // return true;}//---------------------------------------------------------------------------//// private methods////---------------------------------------------------------------------------// method: randQuickSort//// arguments:// Integral::ORDER sort_order: (input) ascending or descending//// return: a boolean value indicating status//// this method sorts the input vector elements in the specified order and// stores the result in this Vector//// a "randomized version of quicksort" using random pivot taken from://// T. Cormen, C. Leiserson, R. Rivest, Introduction to Algorithms,// MIT Press, Boston, Massachusetts, USA, pp. 161-162, 1998.//// this reference method was modified to eliminate recursion.//template<class TObject>boolean Vector<TObject>::randQuickSort(Integral::ORDER sort_order_a) { // declare the temporary variables // long i, j; TObject x; // declare two stacks // Stack<Long> p_stack; Stack<Long> r_stack; // store the partition indices on the stacks // Long p = 0; p_stack.push(&p); Long r = length() - 1; r_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -