void JsonStreamingParser::startValue(char c) { if (c == '[') { startArray(); } else if (c == '{') { startObject(); } else if (c == '"') { startString(); } else if (isDigit(c)) { startNumber(c); } else if (c == 't') { state = STATE_IN_TRUE; buffer[bufferPos] = c; bufferPos++; } else if (c == 'f') { state = STATE_IN_FALSE; buffer[bufferPos] = c; bufferPos++; } else if (c == 'n') { state = STATE_IN_NULL; buffer[bufferPos] = c; bufferPos++; } else { // throw new ParsingError($this->_line_number, $this->_char_number, // "Unexpected character for value: ".$c); } }
// Will exit the program if cannot parse ExcelValue parse(char **string) { while(true) { switch(**string) { case '"': return parseString(string); break; case '-': case '+': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': return parseNumber(string); break; case 't': case 'f': return parseBoolean(string); break; case '#': return parseExcelError(string); break; case ' ': case '\t': case '\n': case '\r': case ',': // Important to progress through arrays (*string)++; break; case '[': return startArray(string); break; case ']': return endArray(string); break; case EOF: return parseFailed("Parse error - reached end of file", 0); break; case '\0': return parseFailed("Parse error - reached end of string", 0); break; default: return parseFailed("Parse error unrecognised character", *string); break; } } }
void JsonStreamingParser::parse(char c) { //System.out.print(c); // valid whitespace characters in JSON (from RFC4627 for JSON) include: // space, horizontal tab, line feed or new line, and carriage return. // thanks: // http://stackoverflow.com/questions/16042274/definition-of-whitespace-in-json if ((c == ' ' || c == '\t' || c == '\n' || c == '\r') && !(state == STATE_IN_STRING || state == STATE_UNICODE || state == STATE_START_ESCAPE || state == STATE_IN_NUMBER || state == STATE_DONE)) { return; } switch (state) { case STATE_IN_STRING: if (c == '"') { endString(); } else if (c == '\\') { state = STATE_START_ESCAPE; } else if ((c < 0x1f) || (c == 0x7f)) { //throw new RuntimeException("Unescaped control character encountered: " + c + " at position" + characterCounter); } else { buffer[bufferPos] = c; bufferPos++; } break; case STATE_IN_ARRAY: if (c == ']') { endArray(); } else { startValue(c); } break; case STATE_IN_OBJECT: if (c == '}') { endObject(); } else if (c == '"') { startKey(); } else { //throw new RuntimeException("Start of string expected for object key. Instead got: " + c + " at position" + characterCounter); } break; case STATE_END_KEY: if (c != ':') { //throw new RuntimeException("Expected ':' after key. Instead got " + c + " at position" + characterCounter); } state = STATE_AFTER_KEY; break; case STATE_AFTER_KEY: startValue(c); break; case STATE_START_ESCAPE: processEscapeCharacters(c); break; case STATE_UNICODE: processUnicodeCharacter(c); break; case STATE_UNICODE_SURROGATE: unicodeEscapeBuffer[unicodeEscapeBufferPos] = c; unicodeEscapeBufferPos++; if (unicodeEscapeBufferPos == 2) { endUnicodeSurrogateInterstitial(); } break; case STATE_AFTER_VALUE: { // not safe for size == 0!!! int within = stack[stackPos - 1]; if (within == STACK_OBJECT) { if (c == '}') { endObject(); } else if (c == ',') { state = STATE_IN_OBJECT; } else { //throw new RuntimeException("Expected ',' or '}' while parsing object. Got: " + c + ". " + characterCounter); } } else if (within == STACK_ARRAY) { if (c == ']') { endArray(); } else if (c == ',') { state = STATE_IN_ARRAY; } else { //throw new RuntimeException("Expected ',' or ']' while parsing array. Got: " + c + ". " + characterCounter); } } else { //throw new RuntimeException("Finished a literal, but unclear what state to move to. Last state: " + characterCounter); } }break; case STATE_IN_NUMBER: if (c >= '0' && c <= '9') { buffer[bufferPos] = c; bufferPos++; } else if (c == '.') { if (doesCharArrayContain(buffer, bufferPos, '.')) { //throw new RuntimeException("Cannot have multiple decimal points in a number. " + characterCounter); } else if (doesCharArrayContain(buffer, bufferPos, 'e')) { //throw new RuntimeException("Cannot have a decimal point in an exponent." + characterCounter); } buffer[bufferPos] = c; bufferPos++; } else if (c == 'e' || c == 'E') { if (doesCharArrayContain(buffer, bufferPos, 'e')) { //throw new RuntimeException("Cannot have multiple exponents in a number. " + characterCounter); } buffer[bufferPos] = c; bufferPos++; } else if (c == '+' || c == '-') { char last = buffer[bufferPos - 1]; if (!(last == 'e' || last == 'E')) { //throw new RuntimeException("Can only have '+' or '-' after the 'e' or 'E' in a number." + characterCounter); } buffer[bufferPos] = c; bufferPos++; } else { endNumber(); // we have consumed one beyond the end of the number parse(c); } break; case STATE_IN_TRUE: buffer[bufferPos] = c; bufferPos++; if (bufferPos == 4) { endTrue(); } break; case STATE_IN_FALSE: buffer[bufferPos] = c; bufferPos++; if (bufferPos == 5) { endFalse(); } break; case STATE_IN_NULL: buffer[bufferPos] = c; bufferPos++; if (bufferPos == 4) { endNull(); } break; case STATE_DONE: myListener->startDocument(); if (c == '[') { startArray(); } else if (c == '{') { startObject(); } else { // throw new ParsingError($this->_line_number, // $this->_char_number, // "Document must start with object or array."); } break; //case STATE_DONE: // throw new ParsingError($this->_line_number, $this->_char_number, // "Expected end of document."); //default: // throw new ParsingError($this->_line_number, $this->_char_number, // "Internal error. Reached an unknown state: ".$this->_state); } characterCounter++; }
void ArrowMarker::draw(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRect &) const { const int x0 = xMap.transform(d_rect.left()); const int y0 = yMap.transform(d_rect.top()); const int x1 = xMap.transform(d_rect.right()); const int y1 = yMap.transform(d_rect.bottom()); p->save(); QPen pen = linePen(); p->setPen(pen); QBrush brush = QBrush(pen.color(), Qt::SolidPattern); QwtPainter::drawLine(p,x0,y0,x1,y1); p->restore(); if (d_end_arrow){ p->save(); p->translate(x1,y1); const double t = theta(x0, y0, x1, y1); p->rotate(-t); QPolygon endArray(3); endArray[0] = QPoint(0,0); int d=(int)floor(d_head_length*tan(M_PI*d_head_angle/180.0)+0.5); endArray[1] = QPoint(-d_head_length,d); endArray[2] = QPoint(-d_head_length,-d); p->setPen(QPen(pen.color(), pen.width(), Qt::SolidLine)); if (d_fill_head) p->setBrush(brush); QwtPainter::drawPolygon(p,endArray); p->restore(); } if (d_start_arrow){ p->save(); p->translate(x0,y0); const double t = theta(x0, y0, x1, y1); p->rotate(-t); QPolygon startArray(3); startArray[0] = QPoint(0,0); int d=(int)floor(d_head_length*tan(M_PI*d_head_angle/180.0)+0.5); startArray[1] = QPoint(d_head_length,d); startArray[2] = QPoint(d_head_length,-d); p->setPen(QPen(pen.color(), pen.width(), Qt::SolidLine)); if (d_fill_head) p->setBrush(brush); QwtPainter::drawPolygon(p,startArray); p->restore(); } if (d_editable) { p->save(); p->setPen(QPen(Qt::black,1,Qt::SolidLine)); QRect handler(QPoint(0,0), QSize(10,10)); handler.moveCenter(startPoint()); p->fillRect(handler, QBrush(Qt::black)); handler.moveCenter(endPoint()); p->fillRect(handler, QBrush(Qt::black)); p->restore(); } }
void LocalBootstrap<Curve>::calculate() const { validCurve_ = false; Size nInsts = ts_->instruments_.size(); // ensure rate helpers are sorted std::sort(ts_->instruments_.begin(), ts_->instruments_.end(), detail::BootstrapHelperSorter()); // check that there is no instruments with the same maturity for (Size i=1; i<nInsts; ++i) { Date m1 = ts_->instruments_[i-1]->latestDate(), m2 = ts_->instruments_[i]->latestDate(); QL_REQUIRE(m1 != m2, "two instruments have the same maturity ("<< m1 <<")"); } // check that there is no instruments with invalid quote for (Size i=0; i<nInsts; ++i) QL_REQUIRE(ts_->instruments_[i]->quote()->isValid(), io::ordinal(i+1) << " instrument (maturity: " << ts_->instruments_[i]->latestDate() << ") has an invalid quote"); // setup instruments for (Size i=0; i<nInsts; ++i) { // don't try this at home! // This call creates instruments, and removes "const". // There is a significant interaction with observability. ts_->instruments_[i]->setTermStructure(const_cast<Curve*>(ts_)); } // set initial guess only if the current curve cannot be used as guess if (validCurve_) QL_ENSURE(ts_->data_.size() == nInsts+1, "dimension mismatch: expected " << nInsts+1 << ", actual " << ts_->data_.size()); else { ts_->data_ = std::vector<Rate>(nInsts+1); ts_->data_[0] = Traits::initialValue(ts_); } // calculate dates and times ts_->dates_ = std::vector<Date>(nInsts+1); ts_->times_ = std::vector<Time>(nInsts+1); ts_->dates_[0] = Traits::initialDate(ts_); ts_->times_[0] = ts_->timeFromReference(ts_->dates_[0]); for (Size i=0; i<nInsts; ++i) { ts_->dates_[i+1] = ts_->instruments_[i]->latestDate(); ts_->times_[i+1] = ts_->timeFromReference(ts_->dates_[i+1]); if (!validCurve_) ts_->data_[i+1] = ts_->data_[i]; } LevenbergMarquardt solver(ts_->accuracy_, ts_->accuracy_, ts_->accuracy_); EndCriteria endCriteria(100, 10, 0.00, ts_->accuracy_, 0.00); PositiveConstraint posConstraint; NoConstraint noConstraint; Constraint& solverConstraint = forcePositive_ ? static_cast<Constraint&>(posConstraint) : static_cast<Constraint&>(noConstraint); // now start the bootstrapping. Size iInst = localisation_-1; Size dataAdjust = Curve::interpolator_type::dataSizeAdjustment; do { Size initialDataPt = iInst+1-localisation_+dataAdjust; Array startArray(localisation_+1-dataAdjust); for (Size j = 0; j < startArray.size()-1; ++j) startArray[j] = ts_->data_[initialDataPt+j]; // here we are extending the interpolation a point at a // time... but the local interpolator can make an // approximation for the final localisation period. // e.g. if the localisation is 2, then the first section // of the curve will be solved using the first 2 // instruments... with the local interpolator making // suitable boundary conditions. ts_->interpolation_ = ts_->interpolator_.localInterpolate( ts_->times_.begin(), ts_->times_.begin()+(iInst + 2), ts_->data_.begin(), localisation_, ts_->interpolation_, nInsts+1); if (iInst >= localisation_) { startArray[localisation_-dataAdjust] = Traits::guess(iInst, ts_, false, 0); // ? } else { startArray[localisation_-dataAdjust] = ts_->data_[0]; } PenaltyFunction<Curve> currentCost( ts_, initialDataPt, ts_->instruments_.begin() + (iInst - localisation_+1), ts_->instruments_.begin() + (iInst+1)); Problem toSolve(currentCost, solverConstraint, startArray); EndCriteria::Type endType = solver.minimize(toSolve, endCriteria); // check the end criteria QL_REQUIRE(endType == EndCriteria::StationaryFunctionAccuracy || endType == EndCriteria::StationaryFunctionValue, "Unable to strip yieldcurve to required accuracy " ); ++iInst; } while ( iInst < nInsts ); validCurve_ = true; }