void test_empty() { IntVec v; CYBOZU_TEST_EQUAL((int)v.size(), 0); CYBOZU_TEST_ASSERT(v.empty()); for (typename IntVec::const_iterator i = v.begin(); i != v.end(); ++i) { printf("%d\n", i->val()); } v.push_back(3, 1); CYBOZU_TEST_EQUAL((int)v.size(), 1); CYBOZU_TEST_ASSERT(!v.empty()); for (typename IntVec::const_iterator i = v.begin(); i != v.end(); ++i) { CYBOZU_TEST_EQUAL(i->pos(), (size_t)3); CYBOZU_TEST_EQUAL(i->val(), 1); } v.push_back(4, 2); CYBOZU_TEST_EQUAL((int)v.size(), 2); int j = 0; for (typename IntVec::const_iterator i = v.begin(); i != v.end(); ++i) { CYBOZU_TEST_EQUAL(i->pos(), size_t(j + 3)); CYBOZU_TEST_EQUAL(i->val(), j + 1); j++; } v.clear(); CYBOZU_TEST_EQUAL((int)v.size(), 0); CYBOZU_TEST_ASSERT(v.empty()); IntVec v1, v2; for (int i = 0; i < 3; i++) { v1.push_back(i, i); v2.push_back(i, i); } CYBOZU_TEST_ASSERT(v1 == v2); v2.push_back(10, 10); CYBOZU_TEST_ASSERT(v1 != v2); }
string getHappyLetters(string letters) { IntVec chars(26); for (char c : letters) { chars[c - 'a'] += 1; } string ans; for (int i = 0; i < 26; ++i) { IntVec cnt = chars; int p = cnt[i]; cnt[i] = 0; while (true) { sort(cnt.begin(), cnt.end()); if (cnt[24] > 0 && cnt[25] > 0) { cnt[24]--, cnt[25]--; } else { break; } } int q = accumulate(cnt.begin(), cnt.end(), 0); if (p > q) { ans += ('a' + i); } } return ans; }
// // Initialise with domain data // bool DataVar::initFromMeshData(const_DomainChunk_ptr dom, const IntVec& data, int fsCode, Centering c, NodeData_ptr nodes, const IntVec& id) { cleanup(); domain = dom; rank = 0; ptsPerSample = 1; centering = c; sampleID = id; meshName = nodes->getName(); siloMeshName = nodes->getFullSiloName(); numSamples = data.size(); if (numSamples > 0) { float* c = new float[numSamples]; dataArray.push_back(c); IntVec::const_iterator it; for (it=data.begin(); it != data.end(); it++) *c++ = static_cast<float>(*it); } initialized = true; return initialized; }
long long minCost(vector <string> road, vector <int> altitude) { int sz = (int)road.size(); // generate alt table IntVec alt = altitude; sort(alt.begin(), alt.end()); unique(alt.begin(), alt.end()); LL mincost[64][64]; memset(mincost, 0x3f, sizeof(mincost)); LIIQueue q; int i, j; for (i = 0; i < (int)alt.size(); ++i) { LL cost = abs(alt[i] - altitude[0]); mincost[0][i] = cost; q.push(LII(-cost, II(0, alt[i]))); } // dijkstra while (q.size() > 0) { LII current = q.top(); q.pop(); int from = current.second.first; if (from == (sz-1)) { return -current.first; } for (i = 0; i < (int)alt.size(); ++i) { if (alt[i] > current.second.second) { break; } for (j = 0; j < sz; ++j) { if (road[from][j] == 'Y') { LL cost = -current.first + abs(alt[i] - altitude[j]); if (cost < mincost[j][i]) { mincost[j][i] = cost; q.push(LII(-cost, II(j, alt[i]))); } } } } } return -1; }
int main () { typedef vector <int> IntVec; IntVec v (10); for (int i = 0; i < v.size (); i++) v[i] = (i + 1) * (i + 1); IntVec::iterator iter; iter = find_if (v.begin (), v.end (), div_3); if (iter != v.end ()) cout << "Value " << *iter << " at offset " << (iter - v.begin ()) << " is divisible by 3" << endl; return 0; }
void SpeckleyElements::reorderArray(IntVec& v, const IntVec& idx, int elementsPerIndex) { IntVec newArray(v.size()); IntVec::iterator arrIt = newArray.begin(); IntVec::const_iterator idxIt; if (elementsPerIndex == 1) { for (idxIt=idx.begin(); idxIt!=idx.end(); idxIt++) { *arrIt++ = v[*idxIt]; } } else { for (idxIt=idx.begin(); idxIt!=idx.end(); idxIt++) { int i = *idxIt; copy(&v[i*elementsPerIndex], &v[(i+1)*elementsPerIndex], arrIt); arrIt += elementsPerIndex; } } v.swap(newArray); }
int minMoves(vector <int> w) { IntVec v = w; sort(v.begin(), v.end()); int ans = 0; if (v != w) { if (v[0] == w[0] || v.back() == w.back()) { ans = 1; } else { ans = 2 + (v[0] == w.back() && v.back() == w[0]); } } return ans; }
mpz_class PrimeSwing::Swing( int _number, PrimeSieve& _sieve ) { // Small precalculated values if (_number < 33) return smallOddSwing[_number]; // Fetch multiplies IntVec multiplies = GetMultiplies(_number, _sieve); // Return multiplies of primorials return _sieve.Primorial(_number / 2, _number) * UtilityFunctions::SequenceProduct( multiplies.begin(), multiplies.end() ); }
void test(size_t n, bool doPut, F pred) { IntVec iv; PtrVec pv; init(iv, pv, n); if (doPut) put(pv); Xbyak::util::Clock clk; clk.begin(); std::sort(pv.begin(), pv.end(), pred); clk.end(); int sum = std::accumulate(iv.begin(), iv.end(), 0); printf("clk=%.2fclk, sum=%d\n", clk.getClock() / double(n), sum); if (doPut) put(pv); }
long long minimalFatigue(vector <int> magicalGirlStrength, vector <int> enemyStrength, vector<long long> enemyCount) { g = magicalGirlStrength; sort(g.begin(), g.end()); e.clear(); for (int i = 0; i < (int)enemyStrength.size(); ++i) { e[enemyStrength[i]] += enemyCount[i]; } LL low = -1, high = 1LL<<62; while (low < high) { LL mid = (low + high) / 2; if (can(mid)) { high = mid; } else { low = mid + 1; } } return low < (1LL<<62) ? low : -1; }
SpeckleyNodes::SpeckleyNodes(SpeckleyNodes_ptr fullNodes, IntVec& requiredNodes, const string& meshName) : name(meshName) { numDims = fullNodes->numDims; nodeDist = fullNodes->nodeDist; globalNumNodes = fullNodes->globalNumNodes; // first: find the unique set of required nodes and their IDs while // updating the contents of requiredNodes at the same time // requiredNodes contains node indices (not IDs!) IntVec::iterator it; IndexMap indexMap; // maps old index to new index size_t newIndex = 0; for (it = requiredNodes.begin(); it != requiredNodes.end(); it++) { IndexMap::iterator res = indexMap.find(*it); if (res == indexMap.end()) { nodeID.push_back(fullNodes->nodeID[*it]); nodeTag.push_back(fullNodes->nodeTag[*it]); indexMap[*it] = newIndex; *it = newIndex++; } else { *it = res->second; } } // second: now that we know how many nodes we need use the map to fill // the coordinates numNodes = newIndex; for (int dim=0; dim<numDims; dim++) { const float* origC = fullNodes->coords[dim]; float* c = new float[numNodes]; coords.push_back(c); IndexMap::const_iterator mIt; for (mIt = indexMap.begin(); mIt != indexMap.end(); mIt++) { c[mIt->second] = origC[mIt->first]; } } }
bool contains(IntVec& vec, uint32 value) { return std::find(vec.begin(), vec.end(), value) != vec.end(); }
void test_intersect() { DoubleVec dv; IntVec iv; const struct { unsigned int pos; double val; } dTbl[] = { { 5, 3.14 }, { 10, 2 }, { 12, 1.4 }, { 100, 2.3 }, { 1000, 4 }, }; const struct { unsigned int pos; int val; } iTbl[] = { { 2, 4 }, { 5, 2 }, { 100, 4 }, { 101, 3}, { 999, 4 }, { 1000, 1 }, { 2000, 3 }, }; const struct { unsigned int pos; double d; int i; } interTbl[] = { { 5, 3.14, 2 }, { 100, 2.3, 4 }, { 1000, 4, 1 } }; for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(dTbl); i++) { dv.push_back(dTbl[i].pos, dTbl[i].val); } for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(iTbl); i++) { iv.push_back(iTbl[i].pos, iTbl[i].val); } int j = 0; for (typename DoubleVec::const_iterator i = dv.begin(), ie = dv.end(); i != ie; ++i) { CYBOZU_TEST_EQUAL(i->pos(), dTbl[j].pos); CYBOZU_TEST_EQUAL(i->val(), dTbl[j].val); j++; } j = 0; for (typename IntVec::const_iterator i = iv.begin(), ie = iv.end(); i != ie; ++i) { CYBOZU_TEST_EQUAL(i->pos(), iTbl[j].pos); CYBOZU_TEST_EQUAL(i->val(), iTbl[j].val); j++; } int sum = 0; for (typename IntVec::const_iterator i = iv.begin(), ie = iv.end(); i != ie; ++i) { sum += i->val() * i->val(); } CYBOZU_TEST_EQUAL(sum, 71); typedef cybozu::nlp::Intersection<DoubleVec, IntVec> InterSection; InterSection inter(dv, iv); j = 0; for (typename InterSection::const_iterator i = inter.begin(), ie = inter.end(); i != ie; ++i) { CYBOZU_TEST_EQUAL(i->pos(), interTbl[j].pos); CYBOZU_TEST_EQUAL(i->val1(), interTbl[j].d); CYBOZU_TEST_EQUAL(i->val2(), interTbl[j].i); j++; } }
// makes a new paving from a vtk file // expects 3d, structured point data in file SPMinimalnode* SPMinimalnode::vtkPaving(const std::string filename) { SPMinimalnode* newTree = NULL; try { size_t expectedDims = 3; IntVec Xs; IntVec Ys; IntVec Zs; // get the spacing and fill in the coordinates vectors using // getCoordinatesFromVtk. // getCoordinatesFromVtk expects 10 header lines with spacings on 4th IntVec spacing = getCoordinatesFromVtk(Xs, Ys, Zs, filename); bool success = ((spacing.size() == expectedDims) && (spacing[0] > 0) && (spacing[0] == spacing[1]) && (spacing[0] == spacing[2])); if (success) { ivector rootbox(expectedDims); double maxXYZ = spacing[0] * 1.0; ImageList boxes; double totalListVol = 0; IntVecItr xIt = Xs.begin(); IntVecItr yIt = Ys.begin(); IntVecItr zIt = Zs.begin(); for (xIt = Xs.begin(); xIt < Xs.end(); xIt++, yIt++, zIt++) { ivector box(expectedDims); // make the box so that its boundaries are on the inner edges // of the voxel interval xdim(Sup(_interval(*xIt/maxXYZ)), Inf(_interval(*xIt + 1.0)/maxXYZ)); interval ydim(Sup(_interval(*yIt/maxXYZ)), Inf(_interval(*yIt + 1.0)/maxXYZ)); interval zdim(Sup(_interval(*zIt/maxXYZ)), Inf(_interval(*zIt + 1.0)/maxXYZ)); box[1] = xdim; box[2] = ydim; box[3] = zdim; boxes.push_back(box); totalListVol += Volume(box); } rootbox[1] = interval(0.0, 1.0); rootbox[2] = interval(0.0, 1.0); rootbox[3] = interval(0.0, 1.0); newTree = makeTreeFromVoxels(rootbox, boxes, maxXYZ, expectedDims); } if (newTree != NULL) { newTree->setNodeName("X"); newTree->recursiveRename(); } return newTree; } catch(std::exception const& e) { delete newTree; newTree = NULL; throw; } }
bool ASMs3Dmx::evalSolution (Matrix& sField, const IntegrandBase& integrand, const RealArray* gpar, bool regular) const { sField.resize(0,0); if (m_basis.empty()) return false; // Evaluate the basis functions and their derivatives at all points std::vector<std::vector<Go::BasisDerivs>> splinex(m_basis.size()); if (regular) { for (size_t b = 0; b < m_basis.size(); ++b) m_basis[b]->computeBasisGrid(gpar[0],gpar[1],gpar[2],splinex[b]); } else if (gpar[0].size() == gpar[1].size() && gpar[0].size() == gpar[2].size()) { for (size_t b = 0; b < m_basis.size(); ++b) { splinex[b].resize(gpar[0].size()); for (size_t i = 0; i < splinex[b].size(); i++) m_basis[b]->computeBasis(gpar[0][i],gpar[1][i],gpar[2][i],splinex[b][i]); } } std::vector<size_t> elem_sizes; for (auto& it : m_basis) elem_sizes.push_back(it->order(0)*it->order(1)*it->order(2)); // Fetch nodal (control point) coordinates Matrix Xnod, Xtmp; this->getNodalCoordinates(Xnod); MxFiniteElement fe(elem_sizes,firstIp); Vector solPt; std::vector<Matrix> dNxdu(m_basis.size()); Matrix Jac; Vec3 X; // Evaluate the secondary solution field at each point size_t nPoints = splinex[0].size(); for (size_t i = 0; i < nPoints; i++, fe.iGP++) { // Fetch indices of the non-zero basis functions at this point std::vector<IntVec> ip(m_basis.size()); IntVec ipa; size_t ofs = 0; for (size_t b = 0; b < m_basis.size(); ++b) { scatterInd(m_basis[b]->numCoefs(0),m_basis[b]->numCoefs(1),m_basis[b]->numCoefs(2), m_basis[b]->order(0),m_basis[b]->order(1),m_basis[b]->order(2), splinex[b][i].left_idx,ip[b]); // Fetch associated control point coordinates if (b == (size_t)geoBasis-1) utl::gather(ip[geoBasis-1], 3, Xnod, Xtmp); for (auto& it : ip[b]) it += ofs; ipa.insert(ipa.end(), ip[b].begin(), ip[b].end()); ofs += nb[b]; } fe.u = splinex[0][i].param[0]; fe.v = splinex[0][i].param[1]; fe.w = splinex[0][i].param[2]; // Fetch basis function derivatives at current integration point for (size_t b = 0; b < m_basis.size(); ++b) SplineUtils::extractBasis(splinex[b][i],fe.basis(b+1),dNxdu[b]); // Compute Jacobian inverse of the coordinate mapping and // basis function derivatives w.r.t. Cartesian coordinates fe.detJxW = utl::Jacobian(Jac,fe.grad(geoBasis),Xtmp,dNxdu[geoBasis-1]); for (size_t b = 1; b <= m_basis.size(); b++) if (b != (size_t)geoBasis) { if (fe.detJxW == 0.0) fe.grad(b).clear(); else fe.grad(b).multiply(dNxdu[b-1],Jac); } // Cartesian coordinates of current integration point X = Xtmp * fe.basis(geoBasis); // Now evaluate the solution field if (!integrand.evalSol(solPt,fe,X,ipa,elem_sizes,nb)) return false; else if (sField.empty()) sField.resize(solPt.size(),nPoints,true); sField.fillColumn(1+i,solPt); } return true; }