int CXMLprefs::SetPreference(const stringT &sPath, const stringT &sValue) { // Find the node specified by the path, creating it if it does not already exist // and add the requested value. // Notes: // This routine only adds plain character data to the node, see comments at the end. // If the node already has plain character data, it is replaced. // If the node exists multiple times with the same path, only the first is altered - // see description of function "first_element_by_path" in the pugixml manual. int iRetVal = XML_SUCCESS; // First see if the node already exists pugi::xml_node node = m_pXMLDoc->first_element_by_path(sPath.c_str(), _T('\\')); if (node == NULL) { // Not there - let's build it // Split up path and then add all nodes in the path (if they don't exist) // Start at the top node = m_pXMLDoc->root(); stringT::size_type pos, lastPos(0); // Find first "non-delimiter". pos = sPath.find_first_of(_T('\\'), lastPos); // Get all nodes in the path, if they exist fine, if not, create them while (pos != stringT::npos || lastPos != stringT::npos) { // Retrieve next node name from path stringT snode = sPath.substr(lastPos, pos - lastPos); // Try to get it pugi::xml_node child = node.child(snode.c_str()); // If not there, add it otherwise use it for the next iteration node = (child == NULL) ? node.append_child(snode.c_str()) : child; // Skip delimiter and find next "non-delimiter" lastPos = sPath.find_first_not_of(_T('\\'), pos); pos = sPath.find_first_of(_T('\\'), lastPos); } } // ***** VERY IMPORTANT ***** // Note, as documented in the pugi manual under "Document object model/Tree Structure", // nodes can be of various types. Element nodes found above, do not have a value. // To add a value to an element node, one has to add a child node of type // 'node_pcdata' (plain character data node) or 'node_cdata' (character data node), // the latter using <![CDATA[[...]]> to encapsulate the data. // If the node has data in its first pcdata child use it, otherwise add a pcdata child pugi::xml_node prefnode = (node.first_child().type() == pugi::node_pcdata) ? node.first_child() : node.append_child(pugi::node_pcdata); if (!prefnode.set_value(sValue.c_str())) iRetVal = XML_PUT_TEXT_FAILED; return iRetVal; }
// It is used to get: // - version // - record type // - value name // - '=' character std::string getCMLCommentToken( const std::string & comment, ssize_t & pos ) { skipSpaces( comment, pos ); ssize_t lastPos( comment.size() - 1 ); std::string token; while ( pos <= lastPos ) { char symbol( comment[ pos ] ); if ( symbol == '=' ) { if ( token.empty() ) { ++pos; return "="; // This is a key-value separator } break; // A key has ended } if ( isspace( symbol ) != 0 ) { break; // A token has ended } token += symbol; ++pos; } return token; }
/** * @param numbers: Give an array numbersbers of n integer * @param target: you need to find four elements that's sum of target * @return: Find all unique quadruplets in the array which gives the sum of * zero. */ vector<vector<int> > fourSum(vector<int> nums, int target) { ans.clear(); vector<int> &a = nums; vector<int> v(4); int n = nums.size(); int i1, i2, i3, i4; sort(a.begin(), a.end()); i1 = 0; while(i1 < n) { i4 = n - 1; while (i4 > i1) { i2 = i1 + 1; i3 = i4 - 1; while (i2 < i3) { if (a[i1] + a[i2] + a[i3] + a[i4] < target) { ++i2; } else if (a[i1] + a[i2] + a[i3] + a[i4] > target) { --i3; } else { v[0] = a[i1]; v[1] = a[i2]; v[2] = a[i3]; v[3] = a[i4]; ans.push_back(v); i2 = nextPos(a, n, i2); } } i4 = lastPos(a, i1, i4); } i1 = nextPos(a, n, i1); } return ans; }
void skipSpaces( const std::string & comment, ssize_t & pos ) { ssize_t lastPos( comment.size() - 1 ); while ( pos <= lastPos ) { if ( isspace( comment[ pos ] ) == 0 ) return; ++pos; } return; }
void ParticleField::Render() { glEnable(GL_TEXTURE_2D); m_ParticleTexture->bind(); static ci::Color c = ci::Color(0.4f, 0.7f, 0.9f); static ci::Color c2 = ci::Color(0.9f, 0.6f, 0.1f); glPushMatrix(); for (std::list<Particle>::iterator it = m_Particles.begin(); it != m_Particles.end(); it++) { const ci::Vec4f back = it->history.back(); float temp = static_cast<float>((it->radius-MIN_RADIUS)/(MAX_RADIUS-MIN_RADIUS)); ci::Color curColor = c*temp + c2*(1-temp); float radius = it->radius * back[3]; ci::Vec3f pos(back[0], back[1], back[2]); scaleVec3(pos); glEnable(GL_TEXTURE_2D); renderImage(pos, radius, curColor, back[3]); glDisable(GL_TEXTURE_2D); glBegin(GL_QUAD_STRIP); float total = static_cast<float>(it->history.size()); for (int i = static_cast<int>(it->history.size())-1; i>0; i--) { float per = static_cast<float>(i) / total; const ci::Vec4f& cur = it->history[i]; const ci::Vec4f& last = it->history[i-1]; ci::Vec3f curPos(cur[0], cur[1], cur[2]); ci::Vec3f lastPos(last[0], last[1], last[2]); scaleVec3(curPos); scaleVec3(lastPos); ci::Vec3f perp0 = curPos - lastPos; ci::Vec3f perp1 = perp0.cross(ci::Vec3f::zAxis()); ci::Vec3f perp2 = perp0.cross(perp1); perp1 = perp0.cross(perp2).normalized(); float offWidth = (it->radius * cur[3] * per * 0.07f); float opacityScale = 0.95f*back[3]*per; if (per > 0.8f) { float temp = (1.0f - per) / 0.2f; float tempScale = sqrt(temp); offWidth *= tempScale; opacityScale *= tempScale; } ci::Vec3f off = perp1 * offWidth; glColor4f(curColor.r, curColor.g, curColor.b, opacityScale); ci::gl::vertex(curPos - off); ci::gl::vertex(curPos + off); } glEnd(); } glPopMatrix(); }
void Ia::simpleDirection() { bool isOk = false; if (lastPos()) { isOk = movLast(); } if (!isOk) { if (_map->getCell(_posY, _posX + 1) == Tile::EMPTY) { _direction = Direction::RIGHT; } else if (_map->getCell(_posY - 1, _posX) == Tile::EMPTY) { _direction = Direction::DOWN; } else if (_map->getCell(_posY, _posX - 1) == Tile::EMPTY) { _direction = Direction::LEFT; } else if (_map->getCell(_posY + 1, _posX) == Tile::EMPTY) { _direction = Direction::UP; } } }
// It is used to get a value. '"' characters are stripped and if there are many // parts then they are merged. std::string getCMLCommentValue( const std::string & comment, ssize_t & pos, std::string & warning ) { skipSpaces( comment, pos ); ssize_t lastPos( comment.size() - 1 ); if ( pos > lastPos ) { warning = "Could not find a property value"; return ""; } if ( comment[ pos ] != '"' ) { std::string token = getCMLCommentToken( comment, pos ); if ( token.empty() ) { warning = "Could not find a property value"; return ""; } return token; } // Here: the value is in double quotes std::string value; ++pos; while ( pos <= lastPos ) { char symbol( comment[ pos ] ); if ( symbol == '\\' ) { if ( pos < lastPos ) { if ( comment[ pos + 1 ] == '"' ) { pos += 2; value += std::string( "\"" ); continue; } } } else if ( symbol == '"' ) { ++pos; // That's the end of the value or of a part. // It might be that the value continues in the next part so we need // to look ahead. ssize_t tempPos( pos ); skipSpaces( comment, tempPos ); if ( tempPos <= lastPos ) { if ( comment[ tempPos ] == '"' ) { // This is a value continue pos = tempPos + 1; continue; } } return value; } value += symbol; ++pos; } // Unfinished double quote warning = "Unfinished double quote for a property value"; return ""; }
// fill matrix values; return true on success. Matrix must have only default values when passed in. int CalculateLocalMatrixGeneric(Matrix& matrix, const DP_BlockInfo *blocks, DP_BlockScoreFunction BlockScore, DP_LoopPenaltyFunction LoopScore, unsigned int queryFrom, unsigned int queryTo) { unsigned int block, residue, prevResidue, loopPenalty, lastBlock = blocks->nBlocks - 1, tracebackResidue = 0; int score, sum, bestPrevScore; // find last possible block positions, based purely on block lengths vector < unsigned int > lastPos(blocks->nBlocks); for (block=0; block<=lastBlock; ++block) { if (blocks->blockSizes[block] > queryTo - queryFrom + 1) { ERROR_MESSAGE("Block " << (block+1) << " too large for this query range"); return STRUCT_DP_PARAMETER_ERROR; } lastPos[block] = queryTo - blocks->blockSizes[block] + 1; } // first row: positive scores of first block at all possible positions for (residue=queryFrom; residue<=lastPos[0]; ++residue) { score = BlockScore(0, residue); matrix[0][residue - queryFrom].score = (score > 0) ? score : 0; } // first column: positive scores of all blocks at first positions for (block=1; block<=lastBlock; ++block) { score = BlockScore(block, queryFrom); matrix[block][0].score = (score > 0) ? score : 0; } // for each successive block, find the best positive scoring with a previous block, if any for (block=1; block<=lastBlock; ++block) { for (residue=queryFrom+1; residue<=lastPos[block]; ++residue) { // get score at this position score = BlockScore(block, residue); if (score == DP_NEGATIVE_INFINITY) continue; // find max score of any allowed previous block bestPrevScore = DP_NEGATIVE_INFINITY; for (prevResidue=queryFrom; prevResidue<=lastPos[block - 1]; ++prevResidue) { // current block must come after the previous block if (residue < prevResidue + blocks->blockSizes[block - 1]) break; // make sure previous block is at an allowed position if (matrix[block - 1][prevResidue - queryFrom].score == DP_NEGATIVE_INFINITY) continue; // get loop score loopPenalty = LoopScore(block - 1, residue - prevResidue - blocks->blockSizes[block - 1]); if (loopPenalty == DP_POSITIVE_INFINITY) continue; // keep maximum score sum = matrix[block - 1][prevResidue - queryFrom].score - loopPenalty; if (sum > bestPrevScore) { bestPrevScore = sum; tracebackResidue = prevResidue; } } // extend alignment if the sum of this block's + previous block's score is positive if (bestPrevScore > 0 && (sum=bestPrevScore+score) > 0) { matrix[block][residue - queryFrom].score = sum; matrix[block][residue - queryFrom].tracebackResidue = tracebackResidue; } // otherwise, start new alignment if score is positive else if (score > 0) matrix[block][residue - queryFrom].score = score; } } return STRUCT_DP_OKAY; }
// fill matrix values; return true on success. Matrix must have only default values when passed in. int CalculateGlobalMatrixGeneric(Matrix& matrix, const DP_BlockInfo *blocks, DP_BlockScoreFunction BlockScore, DP_LoopPenaltyFunction LoopScore, unsigned int queryFrom, unsigned int queryTo) { unsigned int block, residue, prevResidue, lastBlock = blocks->nBlocks - 1; int blockScore = 0, sum; unsigned int loopPenalty; // find possible block positions, based purely on block lengths vector < unsigned int > firstPos(blocks->nBlocks), lastPos(blocks->nBlocks); for (block=0; block<=lastBlock; ++block) { if (block == 0) { firstPos[0] = queryFrom; lastPos[lastBlock] = queryTo - blocks->blockSizes[lastBlock] + 1; } else { firstPos[block] = firstPos[block - 1] + blocks->blockSizes[block - 1]; lastPos[lastBlock - block] = lastPos[lastBlock - block + 1] - blocks->blockSizes[lastBlock - block]; } } // further restrict the search if blocks are frozen for (block=0; block<=lastBlock; ++block) { if (blocks->freezeBlocks[block] != DP_UNFROZEN_BLOCK) { if (blocks->freezeBlocks[block] < firstPos[block] || blocks->freezeBlocks[block] > lastPos[block]) { ERROR_MESSAGE("CalculateGlobalMatrix() - frozen block " << (block+1) << " does not leave room for unfrozen blocks"); return STRUCT_DP_PARAMETER_ERROR; } firstPos[block] = lastPos[block] = blocks->freezeBlocks[block]; } } // fill in first row with scores of first block at all possible positions for (residue=firstPos[0]; residue<=lastPos[0]; ++residue) matrix[0][residue - queryFrom].score = BlockScore(0, residue); // for each successive block, find the best allowed pairing of the block with the previous block bool blockScoreCalculated; for (block=1; block<=lastBlock; ++block) { for (residue=firstPos[block]; residue<=lastPos[block]; ++residue) { blockScoreCalculated = false; for (prevResidue=firstPos[block - 1]; prevResidue<=lastPos[block - 1]; ++prevResidue) { // current block must come after the previous block if (residue < prevResidue + blocks->blockSizes[block - 1]) break; // make sure previous block is at an allowed position if (matrix[block - 1][prevResidue - queryFrom].score == DP_NEGATIVE_INFINITY) continue; // get loop score at this position; assume loop score zero if both frozen if (blocks->freezeBlocks[block] != DP_UNFROZEN_BLOCK && blocks->freezeBlocks[block - 1] != DP_UNFROZEN_BLOCK) { loopPenalty = 0; } else { loopPenalty = LoopScore(block - 1, residue - prevResidue - blocks->blockSizes[block - 1]); if (loopPenalty == DP_POSITIVE_INFINITY) continue; } // get score at this position if (!blockScoreCalculated) { blockScore = BlockScore(block, residue); if (blockScore == DP_NEGATIVE_INFINITY) break; blockScoreCalculated = true; } // find highest sum of scores + loop score for allowed pairing of this block with previous sum = blockScore + matrix[block - 1][prevResidue - queryFrom].score - loopPenalty; if (sum > matrix[block][residue - queryFrom].score) { matrix[block][residue - queryFrom].score = sum; matrix[block][residue - queryFrom].tracebackResidue = prevResidue; } } } } return STRUCT_DP_OKAY; }