Array2D CombinationWithDuplicatedElementsByNonRecursion(Array1D array, int K) { Array2D result; if (array.empty() || K <= 0) { return result; } if (array.size() < K) { result.push_back(array); /** @warning maybe exclude it */ return result; } std::sort(array.begin(), array.end()); result.push_back(Array1D()); int last = array[0], opt_result_num = 1; for (int i = 0; i < array.size(); i++) { if (array[i] != last) { last = array[i]; opt_result_num = result.size(); } Array2D::size_type result_size = result.size(); for (int j = result_size - 1; j >= result_size - opt_result_num; j--) { result.push_back(result[j]); result.back().push_back(array[i]); } } return result; }
vector<Mat> SegmentDataByIDX(const Mat& data, const Mat& idx) { assert(data.type() == CV_32F || data.type() == CV_64F); assert(data.rows > 0 && data.cols > 0 && idx.rows > 0 && idx.cols > 0); assert(data.cols == idx.cols); Array2D idx_hash; if (idx.type() != CV_32S) { Mat idx_tmp = idx; Mat_<int> tmp; idx_tmp.convertTo(tmp, CV_32S); idx_hash = ComputeIDX(tmp); } else{ idx_hash = ComputeIDX(idx); } if (idx_hash.empty()) { return vector<Mat>(); } vector<Mat> result(idx_hash.size()); for (int i = 0; i < idx_hash.size(); i++) { Array1D cur = idx_hash[i]; if (cur.empty()) { return vector<Mat>(); } Mat temp(data.rows, cur.size(), data.type()); Mat col(data.rows, 1, data.type()); for (int j = 0; j < cur.size(); j++) { col = temp(Range(0, data.rows), Range(j, j + 1)); data(Range(0, data.rows), Range(cur[j] - 1, cur[j])).copyTo(col); } result[i] = temp; } return result; }
/** * @brief find all permutation of array * @brief array has some repeating elements */ Array2D PermutationUnique(Array1D array) { Array2D result; if (array.empty()) { return result; } if (array.size() == 1) { result.push_back(array); return result; } PermutationUniqueHelper(array, 0, result); return result; }
Array2D CombinationVer2(Array1D array, int K) { Array2D result; if (array.empty() || K <= 0) { return result; } if (array.size() < K) { result.push_back(array); /** @warning maybe exclude it */ return result; } Array1D temp; CombinationVer2Helper(array, 0, array.size() - 1, K, temp, result); }
Array2D Combination(Array1D array, int K) { Array2D result; if (array.empty() || K <= 0) { return result; } if (array.size() < K) { result.push_back(array); return result; } Array1D temp; CombinationHelper(array, 0, array.size() - 1, K, temp, result); return result; }
Array2D CombinationByDecrease(Array1D array, int K) { Array2D result; if (array.empty() || K <= 0) { return result; } if (array.size() < K) { result.push_back(array); /** @warning maybe exclude it */ return result; } Array1D temp(K); CombinationByDecreaseHelper(array, array.size(), K, temp, result); }
Array2D CombinationWithDuplicatedElementsVer2(Array1D array, int K) { Array2D result; if (array.empty() || K <= 0) { return result; } if (array.size() < K) { result.push_back(array); /** @warning maybe exclude it */ return result; } Array1D temp; }
// Analysis_Lifetime::Setup() Analysis::RetType Analysis_Lifetime::Setup(Array1D const& dsArray, CpptrajFile* outfile) { if (dsArray.empty()) return Analysis::ERR; standalone_ = outfile; inputDsets_ = dsArray; windowSize_ = -1; averageonly_ = false; cumulative_ = false; deltaAvg_ = false; normalizeCurves_ = true; cut_ = 0.5; fuzzCut_ = -1; Compare_ = Compare_GreaterThan; return Analysis::OK; }
static void CombinationHelper(Array1D &array, int left, int right, int k, Array1D &temp, Array2D &result) { assert(!array.empty()); assert(left >= 0 && right >= 0); if (right - left + 1 < k) { return; } if (k == 0) { result.push_back(temp); return; } temp.push_back(array[left]); CombinationHelper(array, left + 1, right, k - 1, temp, result); temp.pop_back(); CombinationHelper(array, left + 1, right, k, temp, result); }
static void CombinationVer2Helper(Array1D &array, int left, int right, int k, Array1D &temp, Array2D &result) { assert(!array.empty()); assert(left >= 0 && right >= 0); if (k == 0) { result.push_back(temp); return; } for (int i = left; i <= right - k + 1; i++) { temp.push_back(array[i]); CombinationVer2Helper(array, i + 1, right, k - 1, temp, result); temp.pop_back(); } }
Array2D CombinationByNonRecursion(Array1D array, int K) { Array2D result; if (array.empty() || K <= 0) { return result; } if (array.size() < K) { result.push_back(array); /** @warning maybe exclude it */ return result; } Array1D cur(1); for (int i = 1; i <= array.size(); i++) { Array1D::size_type size = cur.size(); Array1D temp; /** @bug has some problems */ for (int j = 0; j < size; j++) { temp.push_back(cur[j]); temp.push_back(array[i]); if (temp.size() == K) { result.push_back(temp); } else { cur.push_back(array[i]); } } } return result; }
Array2D CombinationByBinary(Array1D array, int K) { Array2D result; if (array.empty() || K <= 0) { return result; } if (array.size() < K) { result.push_back(array); /** @warning maybe exclude it */ return result; } /** @warning sort or not */ Array1D temp; int begin = (1 << K) - 1, end = (1 << array.size()) - (1 << (array.size() - K)); for (int bit = begin; bit <= end; bit = NextBit(bit)) { temp.clear(); for (int i = 0; i < array.size(); i++) { if (bit & (1 << i)) { temp.push_back(array[i]); } } result.push_back(temp); } return result; }