本文整理汇总了C++中QStack::reserve方法的典型用法代码示例。如果您正苦于以下问题:C++ QStack::reserve方法的具体用法?C++ QStack::reserve怎么用?C++ QStack::reserve使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QStack
的用法示例。
在下文中一共展示了QStack::reserve方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: nodevisited
QList<ListDigraph::Node> ProcessModel::topolSortReachableFrom(const QList<ListDigraph::Node>& s) {
ListDigraph::NodeMap<bool > nodevisited(graph, false);
QList<ListDigraph::Node> res;
QStack<ListDigraph::Node> stack;
ListDigraph::Node curnode;
ListDigraph::Node pnode;
ListDigraph::Node snode;
QList<ListDigraph::Node> reachable = reachableFrom(s);
// Reserve memory
res.reserve(countNodes(graph));
stack.reserve(countNodes(graph));
for (int i = 0; i < s.size(); i++) {
if (s[i] != INVALID) {
stack.push(s[i]);
}
}
bool psched;
while (!stack.empty()) {
curnode = stack.pop();
if (!nodevisited[curnode]) { // This node has not been visited yet
// Check whether all predecessors in reachable are scheduled
psched = true;
for (ListDigraph::InArcIt iait(graph, curnode); iait != INVALID; ++iait) {
pnode = graph.source(iait);
if (reachable.contains(pnode)) { // Consider only nodes which can be reached from s
if (!nodevisited[pnode]) {
psched = false;
break;
}
}
}
if (psched) { // All predecessors have been visited
res.append(curnode);
nodevisited[curnode] = true;
// Push the succeeding nodes
for (ListDigraph::OutArcIt oait(graph, curnode); oait != INVALID; ++oait) {
snode = graph.target(oait);
if (!nodevisited[snode]) {
stack.push(snode);
}
}
} else {
stack.prepend(curnode);
}
} // Else ignore the visited node
}
return res;
}
示例2: read
bool QMakeParser::read(ProFile *pro, const QString &in, int line, SubGrammar grammar)
{
m_proFile = pro;
m_lineNo = line;
// Final precompiled token stream buffer
QString tokBuff;
// Worst-case size calculations:
// - line marker adds 1 (2-nl) to 1st token of each line
// - empty assignment "A=":2 =>
// TokHashLiteral(1) + hash(2) + len(1) + "A"(1) + TokAssign(1) + 0(1) +
// TokValueTerminator(1) == 8 (9)
// - non-empty assignment "A=B C":5 =>
// TokHashLiteral(1) + hash(2) + len(1) + "A"(1) + TokAssign(1) + 2(1) +
// TokLiteral(1) + len(1) + "B"(1) +
// TokLiteral(1) + len(1) + "C"(1) + TokValueTerminator(1) == 14 (15)
// - variable expansion: "$$f":3 =>
// TokVariable(1) + hash(2) + len(1) + "f"(1) = 5
// - function expansion: "$$f()":5 =>
// TokFuncName(1) + hash(2) + len(1) + "f"(1) + TokFuncTerminator(1) = 6
// - scope: "X:":2 =>
// TokHashLiteral(1) + hash(2) + len(1) + "A"(1) + TokCondition(1) +
// TokBranch(1) + len(2) + ... + len(2) + ... == 10
// - test: "X():":4 =>
// TokHashLiteral(1) + hash(2) + len(1) + "A"(1) + TokTestCall(1) + TokFuncTerminator(1) +
// TokBranch(1) + len(2) + ... + len(2) + ... == 11
// - "for(A,B):":9 =>
// TokForLoop(1) + hash(2) + len(1) + "A"(1) +
// len(2) + TokLiteral(1) + len(1) + "B"(1) + TokValueTerminator(1) +
// len(2) + ... + TokTerminator(1) == 14 (15)
tokBuff.reserve((in.size() + 1) * 5);
ushort *tokPtr = (ushort *)tokBuff.constData(); // Current writing position
// Expression precompiler buffer.
QString xprBuff;
xprBuff.reserve(tokBuff.capacity()); // Excessive, but simple
ushort *buf = (ushort *)xprBuff.constData();
// Parser state
m_blockstack.clear();
m_blockstack.resize(1);
QStack<ParseCtx> xprStack;
xprStack.reserve(10);
// We rely on QStrings being null-terminated, so don't maintain a global end pointer.
const ushort *cur = (const ushort *)in.unicode();
m_canElse = false;
freshLine:
m_state = StNew;
m_invert = false;
m_operator = NoOperator;
m_markLine = m_lineNo;
m_inError = false;
int parens = 0; // Braces in value context
int argc = 0;
int wordCount = 0; // Number of words in currently accumulated expression
int lastIndent = 0; // Previous line's indentation, to detect accidental continuation abuse
bool lineMarked = true; // For in-expression markers
ushort needSep = TokNewStr; // Met unquoted whitespace
ushort quote = 0;
ushort term = 0;
Context context;
ushort *ptr;
if (grammar == ValueGrammar) {
context = CtxPureValue;
ptr = tokPtr + 2;
} else {
context = CtxTest;
ptr = buf + 4;
}
ushort *xprPtr = ptr;
#define FLUSH_LHS_LITERAL() \
do { \
if ((tlen = ptr - xprPtr)) { \
finalizeHashStr(xprPtr, tlen); \
if (needSep) { \
wordCount++; \
needSep = 0; \
} \
} else { \
ptr -= 4; \
} \
} while (0)
#define FLUSH_RHS_LITERAL() \
do { \
if ((tlen = ptr - xprPtr)) { \
xprPtr[-2] = TokLiteral | needSep; \
xprPtr[-1] = tlen; \
if (needSep) { \
wordCount++; \
needSep = 0; \
} \
} else { \
ptr -= 2; \
} \
} while (0)
//.........这里部分代码省略.........
示例3: fitCurve
/*!
\param points Series of data points
\return Curve points
*/
QPolygonF QwtWeedingCurveFitter::fitCurve( const QPolygonF &points ) const
{
QStack<Line> stack;
stack.reserve( 500 );
const QPointF *p = points.data();
const int nPoints = points.size();
QVector<bool> usePoint( nPoints, false );
double distToSegment;
stack.push( Line( 0, nPoints - 1 ) );
while ( !stack.isEmpty() )
{
const Line r = stack.pop();
// initialize line segment
const double vecX = p[r.to].x() - p[r.from].x();
const double vecY = p[r.to].y() - p[r.from].y();
const double vecLength = qSqrt( vecX * vecX + vecY * vecY );
const double unitVecX = ( vecLength != 0.0 ) ? vecX / vecLength : 0.0;
const double unitVecY = ( vecLength != 0.0 ) ? vecY / vecLength : 0.0;
double maxDist = 0.0;
int nVertexIndexMaxDistance = r.from + 1;
for ( int i = r.from + 1; i < r.to; i++ )
{
//compare to anchor
const double fromVecX = p[i].x() - p[r.from].x();
const double fromVecY = p[i].y() - p[r.from].y();
const double fromVecLength =
qSqrt( fromVecX * fromVecX + fromVecY * fromVecY );
if ( fromVecX * unitVecX + fromVecY * unitVecY < 0.0 )
{
distToSegment = fromVecLength;
}
if ( fromVecX * unitVecX + fromVecY * unitVecY < 0.0 )
{
distToSegment = fromVecLength;
}
else
{
const double toVecX = p[i].x() - p[r.to].x();
const double toVecY = p[i].y() - p[r.to].y();
const double toVecLength = qSqrt( toVecX * toVecX + toVecY * toVecY );
const double s = toVecX * ( -unitVecX ) + toVecY * ( -unitVecY );
if ( s < 0.0 )
distToSegment = toVecLength;
else
{
distToSegment = qSqrt( qFabs( toVecLength * toVecLength - s * s ) );
}
}
if ( maxDist < distToSegment )
{
maxDist = distToSegment;
nVertexIndexMaxDistance = i;
}
}
if ( maxDist <= d_data->tolerance )
{
usePoint[r.from] = true;
usePoint[r.to] = true;
}
else
{
stack.push( Line( r.from, nVertexIndexMaxDistance ) );
stack.push( Line( nVertexIndexMaxDistance, r.to ) );
}
}
int cnt = 0;
QPolygonF stripped( nPoints );
for ( int i = 0; i < nPoints; i++ )
{
if ( usePoint[i] )
stripped[cnt++] = p[i];
}
stripped.resize( cnt );
return stripped;
}