LongInt Algorithm::Modular_exponentiation(LongInt a,LongInt m, LongInt r)
{
    //qDebug()<<"algorithm.cpp: Modular_exponentiation"<<a<<m<<r;
    if(r==0)
    {
        //qDebug()<<"algorithm.cpp: Modular_exponentiation :( 1 )";
        return LongInt(1);
    }
    QVector<LongInt> part;
    part<<1;
    LongInt a_k(1),k(1);

    while(1)
    {
        //qDebug()<<a_k<<a<<m;
        a_k=((a_k*a)%m);
        part<<a_k;
        if(part.last()==1)
        {
            part.removeLast();
            //qDebug()<<"algorithm.cpp: Modular_exponentiation :"<<part.value( ((r % LongInt(part.length()))).toInt());
            return part.value( ((r % LongInt(part.length()))).toInt());
        }
        k+=1;
        if(k>r)
        {
            //qDebug()<<"algorithm.cpp: Modular_exponentiation :"<<part.value( ((r % LongInt(part.length()))).toInt());
            return part.value( ((r % LongInt(part.length()))).toInt());
            return a_k;
        }
    }
}
Ejemplo n.º 2
0
void CompletionModel::updateCompletion(const QString &text, FindFlags f)
{
    if (text.isEmpty())
         return;
     beginResetModel();
     Utils::erase(m_entries, Utils::equal(&CompletionEntry::text, text));
     m_entries.prepend({text, f});
     while (m_entries.size() > MAX_COMPLETIONS)
         m_entries.removeLast();
     endResetModel();
}
Ejemplo n.º 3
0
QVector<QPoint>
CurveGroup::subsample(QVector<QPoint> cp, float delta, bool closed)
{
  // find total path length  
  int xcount = cp.count(); 
  double plen = 0;
  for (int i=1; i<xcount; i++)
    {
      QPoint v = cp[i]-cp[i-1];
      plen += qSqrt(QPoint::dotProduct(v,v));
    }

  if (closed) // for closed curve
    {
      QPoint v = cp[0]-cp[xcount-1];
      plen += qSqrt(QPoint::dotProduct(v,v));
    }

  int npcount = plen/delta;
  delta = plen/npcount;

  QVector<QPoint> c;
  c << cp[0];

  double clen = 0;
  double pclen = 0;
  int j = c.count();
  int iend = xcount;
  if (closed) iend = xcount+1;
  for (int i=1; i<iend; i++)
    {
      QPoint a, b;
      if (i < xcount)
	{
	  b = cp[i];
	  a = cp[i-1];
	}
      else
	{
	  b = cp[0];
	  a = cp[xcount-1];
	}

      QPoint dv = b-a;
      clen += qSqrt(QPoint::dotProduct(dv, dv));

      while (j*delta <= clen)
	{
	  double frc = (j*delta - pclen)/(clen-pclen);
	  c << (a + frc*dv);

	  j = c.count();
	}
      
      pclen = clen;
    }

  if (!closed)
    {
      if ((c[c.count()-1]-cp[xcount-1]).manhattanLength() < delta)
	c.removeLast();
      c << cp[xcount-1];
    }

  return c;
}
Ejemplo n.º 4
0
// LATER make read and write timeout parameters
bool PfParser::parse(QIODevice *source, PfOptions options) {
  bool lazyBinaryFragments = options.shouldLazyLoadBinaryFragments();
  if (!_handler) {
    qWarning() << "PfParser::parse called before setting a handler";
    return false;
  }
  _handler->setErrorString(tr("unknown handler error"));
  int line = 1, column = 0, arrayColumn = 0;
  quint8 c, quote = 0, escapeshift = 0;
  quint16 escaped = 0;
  qint8 digit;
  State state = TopLevel; // current state
  State quotedState = TopLevel; // saved state for quotes and comments
  State escapedState = TopLevel; // saved state for escapes
  QByteArray content, comment, surface;
  QVector<Node> nodes;
  bool firstNode = true;
  PfArray array;
  if (!source->isOpen() && !source->open(QIODevice::ReadOnly)) {
    _handler->setErrorString(tr("cannot open document : %1")
                             .arg(source->errorString()));
    goto error;
  }
  if (!_handler->startDocument(options)) {
    _handler->setErrorString(tr("cannot handle start of document"));
    goto error;
  }
  while (source->bytesAvailable()
         || source->waitForReadyRead(options.readTimeout()),
         source->getChar((char*)&c)) {
    ++column;
    switch(state) {
    case TopLevel:
      if (c == '(') {
        state = Name;
      } else if (c == '\n') {
        ++line;
        column = 0;
      } else if (pfisspace(c)) {
      } else if (c == '#') {
        state = Comment;
        quotedState = TopLevel;
      } else {
        _handler->setErrorString(tr("unexpected character '%1' "
                                    "(in TopLevel state)")
                                 .arg(pfquotechar(c)));
        goto error;
      }
      break;
    case Name:
      if (pfisendofname(c) && content.isEmpty()) {
        _handler->setErrorString(tr("anonymous node"));
        goto error;
      } else if (c == '(') {
        nodes.append(QString::fromUtf8(content));
        content.clear();
        if (!_handler->startNode(names(nodes))) {
          _handler->setErrorString(tr("cannot handle start of node"));
          goto error;
        }
      } else if (c == ')') {
        nodes.append(QString::fromUtf8(content));
        content.clear();
        QVector<QString> names = ::names(nodes);
        if (!_handler->startNode(names) || !_handler->endNode(names)) {
          _handler->setErrorString(tr("cannot handle end of node"));
          goto error;
        }
        nodes.removeLast();
        state = nodes.size() ? Content : TopLevel;
        if (nodes.isEmpty()) {
          switch (options.rootNodesParsingPolicy()) {
          case StopAfterFirstRootNode:
            if (firstNode)
              goto stop_parsing;
            break;
          case FailAtSecondRootNode:
            if (!firstNode) {
              _handler->setErrorString(tr("only one root node is allowed "
                                          "(by option)"));
              goto error;
            }
            break;
          case ParseEveryRootNode:
            ;
          }
        }
      } else if (pfisspace(c)) {
        if (c == '\n') {
          ++line;
          column = 0;
        }
        nodes.append(QString::fromUtf8(content));
        content.clear();
        if (!_handler->startNode(names(nodes))) {
          _handler->setErrorString(tr("cannot handle start of node"));
          goto error;
        }
        state = Content;
      } else if (c == '#') {
        nodes.append(QString::fromUtf8(content));
        content.clear();
        if (!_handler->startNode(names(nodes))) {
          _handler->setErrorString(tr("cannot handle start of node"));
          goto error;
        }
        state = Comment;
        quotedState = Content;
      } else if (c == '|') {
        nodes.append(QString::fromUtf8(content));
        content.clear();
        if (!_handler->startNode(names(nodes))) {
          _handler->setErrorString(tr("cannot handle start of node"));
          goto error;
        }
        state = BinarySurfaceOrLength;
      } else if (c == ';') {
        nodes.append(QString::fromUtf8(content));
        array.clear();
        content.clear();
        if (!_handler->startNode(names(nodes))) {
          _handler->setErrorString(tr("cannot handle start of node"));
          goto error;
        }
        array.appendHeader("0");
        arrayColumn = 1;
        state = ArrayHeader;
      } else if (pfisquote(c)) {
        quote = c;
        state = Quote;
        quotedState = Name;
      } else if (c == '\\') {
        state = Escape;
        escapedState = Name;
      } else if (pfisspecial(c)) {
        _handler->setErrorString(tr("unexpected character '%1' (in Name state)")
                                 .arg(pfquotechar(c)));
        goto error;
      } else {
        content.append(c);
      }
      break;
    case SpaceInContent:
      if (pfisspace(c)) {
        if (c == '\n') {
          ++line;
          column = 0;
        } else {
          ++column;
        }
        break;
      }
      // otherwise process as Content by falling into Content: label
    case Content:
      if (c == ';') {
        // LATER warn if an array node has text or binary content
        array.clear();
        if (!content.isEmpty()) {
          array.appendHeader(content);
          content.clear();
        } else
          array.appendHeader("0");
        arrayColumn = 1;
        state = ArrayHeader;
      } else if (c == '(') {
        if (content.size()) {
          if (!_handler->text(QString::fromUtf8(content))) {
            _handler->setErrorString(tr("cannot handle text fragment"));
            goto error;
          }
          content.clear();
          nodes.last()._hasContent = true;
        }
        state = Name;
      } else if (c == ')') {
        if (content.size()) {
          if (!_handler->text(QString::fromUtf8(content))) {
            _handler->setErrorString(tr("cannot handle text fragment"));
            goto error;
          }
          content.clear();
          nodes.last()._hasContent = true;
        }
        if (!_handler->endNode(names(nodes))) {
          _handler->setErrorString(tr("cannot handle end of node"));
          goto error;
        }
        nodes.removeLast();
        state = nodes.size() ? Content : TopLevel;
        if (nodes.isEmpty()) {
          switch (options.rootNodesParsingPolicy()) {
          case StopAfterFirstRootNode:
            if (firstNode)
              goto stop_parsing;
            break;
          case FailAtSecondRootNode:
            if (!firstNode) {
              _handler->setErrorString(tr("only one root node is allowed "
                                          "(by option)"));
              goto error;
            }
            break;
          case ParseEveryRootNode:
            ;
          }
        }
      } else if (pfisspace(c)) {
        if (c == '\n') {
          ++line;
          column = 0;
        } else {
          ++column;
        }
        state = SpaceInContent;
      } else if (c == '#') {
        if (content.size()) {
          if (!_handler->text(QString::fromUtf8(content))) {
            _handler->setErrorString(tr("cannot handle text fragment"));
            goto error;
          }
          content.clear();
          nodes.last()._hasContent = true;
        }
        state = Comment;
        quotedState = Content;
      } else if (c == '|') {
        if (content.size()) {
          if (!_handler->text(QString::fromUtf8(content))) {
            _handler->setErrorString(tr("cannot handle text fragment"));
            goto error;
          }
          content.clear();
          nodes.last()._hasContent = true;
        }
        state = BinarySurfaceOrLength;
      } else if (pfisquote(c)) {
        if (state == SpaceInContent)
          content.append(' ');
        quote = c;
        state = Quote;
        quotedState = Content;
      } else if (c == '\\') {
        if (state == SpaceInContent)
          content.append(' ');
        state = Escape;
        escapedState = Content;
      } else if (pfisspecial(c)) {
        _handler->setErrorString(tr("unexpected character '%1' "
                                    "(in Content state)")
                                 .arg(pfquotechar(c)));
        goto error;
      } else {
        if (state == SpaceInContent) {
          if (!content.isEmpty() || nodes.last()._hasContent)
            content.append(' ');
          state = Content;
        }
        content.append(c);
      }
      break;
    case Comment:
      if (c == '\n') {
        if (!options.shouldIgnoreComment()) {
          if (!_handler->comment(comment)) {
            _handler->setErrorString(tr("cannot handle comment"));
            goto error;
          }
        }
        comment.clear();
        ++line;
        column = 0;
        state = quotedState;
      } else {
        if (!options.shouldIgnoreComment())
          comment.append(c);
        ++column;
      }
      break;
    case Quote:
      if (c == quote) {
        state = quotedState;
        ++column;
      } else if (c == '\\' && quote == '"') {
        state = Escape;
        escapedState = Quote;
        ++column;
      } else {
        if (c == '\n') {
          ++line;
          column = 0;
        } else
          ++column;
        content.append(c);
      }
      break;
    case BinarySurfaceOrLength:
      if (c == '\n') {
        if (content.size() == 0) {
          _handler->setErrorString(tr("binary fragment without length"));
          goto error;
        }
        bool ok;
        qint64 l = content.toLongLong(&ok);
        if (!ok) {
          _handler->setErrorString(tr("binary fragment with incorrect length"));
          goto error;
        }
        if (!readAndFinishBinaryFragment(source, &lazyBinaryFragments, "", l,
                                         options))
          goto error;
        content.clear();
        nodes.last()._hasContent = true;
        line = 10000000; // LATER hide line numbers after first binary fragment
        column = 0;
        state = Content;
      } else {
        if (std::isspace(c)) {
          // ignore whitespace, incl. \r
        } else if (c == '|') {
          surface = content;
          content.clear();
          state = BinaryLength;
        } else if (std::isdigit(c) || std::islower(c) || std::isupper(c)
                   || c == ':') {
          content.append(c);
        } else {
          _handler->setErrorString(tr("unexpected character '%1' "
                                      "(in BinarySurfaceOrLength state)")
                                   .arg(pfquotechar(c)));
          goto error;
        }
        ++column;
      }
      break;
    case BinaryLength:
      if (c == '\n') {
        if (content.size() == 0) {
          _handler->setErrorString(tr("binary fragment without length"));
          goto error;
        }
        bool ok;
        qint64 l = content.toLongLong(&ok);
        if (!ok) {
          _handler->setErrorString(tr("binary fragment with incorrect length"));
          goto error;
        }
        if (!readAndFinishBinaryFragment(source, &lazyBinaryFragments, surface,
                                         l, options))
          goto error;
        content.clear();
        nodes.last()._hasContent = true;
        line = 10000000; // LATER hide line numbers after first binary fragment
        column = 0;
        state = Content;
      } else {
        if (std::isspace(c)) {
          // ignore whitespace, incl. \r
        } else if (std::isdigit(c)) {
          content.append(c);
        } else {
          _handler->setErrorString(tr("unexpected character '%1' "
                                      "(in BinaryLength state)")
                                   .arg(pfquotechar(c)));
          goto error;
        }
        ++column;
      }
      break;
    case ArrayHeader:
      if (c == '\n') {
        if (!content.isEmpty()) {
          array.appendHeader(QString::fromUtf8(content));
          content.clear();
        } else
          array.appendHeader(QString::number(arrayColumn));
        ++line;
        column = 0;
        state = ArrayBody;
      } else {
        if (c == ';') {
          if (!content.isEmpty()) {
            array.appendHeader(QString::fromUtf8(content));
            content.clear();
          } else
            array.appendHeader(QString::number(arrayColumn));
          ++arrayColumn;
        } else if (c == ')') {
          content.clear();
          if (!finishArray(_handler, &array, &nodes))
            goto error;
          state = nodes.size() ? Content : TopLevel;
          if (nodes.isEmpty()) {
            switch (options.rootNodesParsingPolicy()) {
            case StopAfterFirstRootNode:
              if (firstNode)
                goto stop_parsing;
              break;
            case FailAtSecondRootNode:
              if (!firstNode) {
                _handler->setErrorString(tr("only one root node is allowed "
                                            "(by option)"));
                goto error;
              }
              break;
            case ParseEveryRootNode:
              ;
            }
          }
        } else if (c == '#') {
          if (!content.isEmpty()) {
            array.appendHeader(QString::fromUtf8(content));
            content.clear();
          } else
            array.appendHeader(QString::number(arrayColumn));
          ++column;
          state = Comment;
          quotedState = ArrayBody;
        } else if (pfisspace(c)) {
          // ignore
        } else if (pfisquote(c)) {
          quote = c;
          state = Quote;
          quotedState = ArrayHeader;
        } else if (c == '\\') {
          state = Escape;
          escapedState = ArrayHeader;
        } else if (pfisspecial(c)) {
          _handler->setErrorString(tr("unexpected character '%1'"
                                      " (in ArrayHeader state)")
                                   .arg(pfquotechar(c)));
          goto error;
        } else {
          content.append(c);
        }
        ++column;
      }
      break;
    case ArrayBody:
      if (c == '\n') {
        array.appendCell(QString::fromUtf8(content));
        content.clear();
        array.appendRow();
        ++line;
        column = 0;
      } else {
        if (c == ';') {
          array.appendCell(QString::fromUtf8(content));
          content.clear();
        } else if (c == ')') {
          if (content.size())
            array.appendCell((QString::fromUtf8(content)));
          array.removeLastRowIfEmpty();
          content.clear();
          if (!finishArray(_handler, &array, &nodes))
            goto error;
          state = nodes.size() ? Content : TopLevel;
          if (nodes.isEmpty()) {
            switch (options.rootNodesParsingPolicy()) {
            case StopAfterFirstRootNode:
              if (firstNode)
                goto stop_parsing;
              break;
            case FailAtSecondRootNode:
              if (!firstNode) {
                _handler->setErrorString(tr("only one root node is allowed "
                                            "(by option)"));
                goto error;
              }
              break;
            case ParseEveryRootNode:
              ;
            }
          }
        } else if (c == '#') {
          if (content.size())
            array.appendCell((QString::fromUtf8(content)));
          content.clear();
          ++column;
          state = Comment;
          quotedState = ArrayBody;
        } else if (pfisspace(c)) {
          // ignore
        } else if (pfisquote(c)) {
          quote = c;
          state = Quote;
          quotedState = ArrayBody;
        } else if (c == '\\') {
          state = Escape;
          escapedState = ArrayBody;
        } else if (pfisspecial(c)) {
          _handler->setErrorString(tr("unexpected character '%1'"
                                      " (in ArrayBody state)")
                                   .arg(pfquotechar(c)));
          goto error;
        } else {
          content.append(c);
        }
        ++column;
      }
      break;
    case Escape:
      if (c == '\n') {
        column = 0;
        ++line;
      } else {
        if (c == 'n')
          c = '\n';
        else if (c == 'r')
          c = '\r';
        else if (c == 't')
          c = '\t';
        else if (c == '0')
          c = 0;
        else if (c == 'x') {
          state = EscapeHex;
          escapeshift = 4;
          escaped = 0;
          break;
        } else if (c == 'u') {
          state = EscapeHex;
          escapeshift = 12;
          escaped = 0;
          break;
        }
        ++column;
      }
      content.append(c);
      state = escapedState;
      break;
    case EscapeHex:
      digit = hexdigits[c];
      if (digit < 0) {
        _handler->setErrorString("bad hexadecimal digit in escape sequence");
        goto error;
      }
      if (escapeshift) {
        escaped |= digit << escapeshift;
        escapeshift -= 4;
      } else {
        if (escaped > 0x7f)
          content.append(QString(QChar(escaped|digit)).toUtf8());
        else
          content.append(QChar(escaped|digit));
        state = escapedState;
      }
      ++column;
      break;
    }
  }
stop_parsing:
  if (state != TopLevel) {
    _handler->setErrorString(tr("unexpected end of document"));
    goto error;
  }
  if (!_handler->endDocument()) {
    _handler->setErrorString(tr("cannot handle end of document"));
    goto error;
  }
  return true;
error:
  _handler->error(line, column);
  return false;
}