Exemple #1
0
void QCanvas::razor_CH(QVector<QCanvas::line_t> razor, QVector<QPointF> &poly, bool time_sleep)
{
    for(size_t i = 0; i<razor.size(); ++i)
    {
        QVector<QPointF> tmp;
        for(size_t j = 0; j< poly.size(); ++j)
        {
            QPointF S = poly.value(j);
            QPointF F = poly.value((j+1) % poly.size());
            size_t flag = Intersection(S,F,razor.value(i).S,razor.value(i).F);
            while(flag)
            switch (flag) {
            case 1:
            {
                flag = 0;
                continue;
            }
            case 2:
            {
                QPointF add = LinesCross(S,F,razor.value(i).S,razor.value(i).F);
                if (add == F)
                {
                    flag = 3;
                    continue;
                }
                tmp.push_back(add);
                tmp.push_back(F);
                flag = 0;
                continue;
            }
            case 3:
            {
                tmp.push_back(F);
                flag = 0;
                continue;
            }
            case 4:
            {
                QPointF add = LinesCross(S,F,razor.value(i).S,razor.value(i).F);
                if (add == F)
                {
                    flag = 3;
                    continue;
                }
                tmp.push_back(add);
                flag = 0;
                continue;
            }
            }

        }
        poly.swap(tmp);
    }
}
QVector<double> KolmogorovZurbenko::kz1d(int iterations)
{
    QVector<double> *tmp = new QVector<double>(y);
    QVector<double> *ans = new QVector<double>(y.size());
    ans->fill(0.0);
    for(int i = 0; i < iterations; i++){
        for(int yi = 0; yi < y.size(); yi++){
            ans->operator [](yi) = mavg1d(*tmp, yi, WINDOW);
        }
        ans->swap(*tmp);
    }

    return *ans;
}
QVector<double> KolmogorovZurbenko::kza1d(int window, int iterations, int minimumWindowLength, double tolerance)
{
    int n,q;
    long qh, qt;
    double m;

    n = y.size();

    QVector<double> *d = new QVector<double>(n);
    QVector<double> *prime = new QVector<double>(n);
    QVector<double> *ans = new QVector<double>(n);
    QVector<double> *tmp = new QVector<double>(y);

    q = window;

    differenced(d, prime, q);
    m = *std::max_element(std::begin(*d), std::end(*d));


    for(int i = 0; i < iterations; i++){
        #pragma omp parallel for
        for(int t = 0; t < n; t++){
            if(abs(prime->at(t)) < tolerance){
                qh = (int) floor(q*adaptive(d->at(t), m));
                qt = (int) floor(q*adaptive(d->at(t), m));
            } else if (abs(prime->at(t)) < 0){
                qh = q;
                qt = (int) floor(q*adaptive(d->at(t), m));
            } else {
                qh = (int) floor(q*adaptive(d->at(t), m));
                qt = q;
            }
            qt = (qt < minimumWindowLength) ? minimumWindowLength : qt;
            qh = (qh < minimumWindowLength) ? minimumWindowLength : qh;

            qh = (qh > n-t-1) ? n-t-1 : qh;
            qt = (qt > t) ? t : qt;
            ans->operator [](t) = mavga1d(*tmp, t-qt, t+qh+1);
        }
        ans->swap(*tmp);
    }

    return *ans;
}
Exemple #4
0
int QgsStringUtils::levenshteinDistance( const QString& string1, const QString& string2, bool caseSensitive )
{
  int length1 = string1.length();
  int length2 = string2.length();

  //empty strings? solution is trivial...
  if ( string1.isEmpty() )
  {
    return length2;
  }
  else if ( string2.isEmpty() )
  {
    return length1;
  }

  //handle case sensitive flag (or not)
  QString s1( caseSensitive ? string1 : string1.toLower() );
  QString s2( caseSensitive ? string2 : string2.toLower() );

  const QChar* s1Char = s1.constData();
  const QChar* s2Char = s2.constData();

  //strip out any common prefix
  int commonPrefixLen = 0;
  while ( length1 > 0 && length2 > 0 && *s1Char == *s2Char )
  {
    commonPrefixLen++;
    length1--;
    length2--;
    s1Char++;
    s2Char++;
  }

  //strip out any common suffix
  while ( length1 > 0 && length2 > 0 && s1.at( commonPrefixLen + length1 - 1 ) == s2.at( commonPrefixLen + length2 - 1 ) )
  {
    length1--;
    length2--;
  }

  //fully checked either string? if so, the answer is easy...
  if ( length1 == 0 )
  {
    return length2;
  }
  else if ( length2 == 0 )
  {
    return length1;
  }

  //ensure the inner loop is longer
  if ( length1 > length2 )
  {
    qSwap( s1, s2 );
    qSwap( length1, length2 );
  }

  //levenshtein algorithm begins here
  QVector< int > col;
  col.fill( 0, length2 + 1 );
  QVector< int > prevCol;
  prevCol.reserve( length2 + 1 );
  for ( int i = 0; i < length2 + 1; ++i )
  {
    prevCol << i;
  }
  const QChar* s2start = s2Char;
  for ( int i = 0; i < length1; ++i )
  {
    col[0] = i + 1;
    s2Char = s2start;
    for ( int j = 0; j < length2; ++j )
    {
      col[j + 1] = qMin( qMin( 1 + col[j], 1 + prevCol[1 + j] ), prevCol[j] + (( *s1Char == *s2Char ) ? 0 : 1 ) );
      s2Char++;
    }
    col.swap( prevCol );
    s1Char++;
  }
  return prevCol[length2];
}