Пример #1
0
void radixSort (IntList & values) {
    bool flag   = true;
    int divisor = 1;
    
    while (flag)
    {
        DeqVector buckets (10);
        flag = false;

        std::for_each (values.begin (), values.end (), 
            copyIntoBuckets (divisor, buckets, flag));
        std::accumulate (buckets.begin (), buckets.end (), values.begin (), copyList);
        divisor *= 10;
        std::copy (values.begin (), values.end (), OstreamIter (std::cout, " "));
        std::cout << std::endl;
    }
}
Пример #2
0
double
check_multiple( double * tgt, double * src, int & ind, IntList & nb, SeedList & seeds, double & tolerance, int & nx, int & ny ) {
    if ( nb.size() == 1 ) return nb.front();
    if ( nb.size() <  1 ) return 0.0; // dumb protection

    double diff, maxdiff = 0.0, res = 0.0;
    int i;
    IntList::iterator  it;
    SeedList::iterator sit;
    PointXY ptsit, pt = pointFromIndex( ind, nx );
    double distx, dist = FLT_MAX;

    /* maxdiff */
    for ( it = nb.begin(); it != nb.end(); it++ ) {
        if ( !get_seed( seeds, *it, sit ) ) continue;
        diff = fabs( src[ ind ] - src[ (*sit).index ] );
        if ( diff > maxdiff ) {
            maxdiff = diff;
            /* assign result to the steepest until and if it not assigned to closest over the tolerance */
            if ( dist == FLT_MAX )
                res = *it;
        }
        /* we assign to the closest centre which is above tolerance, if none than to maxdiff */
        if ( diff >= tolerance ) {
            ptsit = pointFromIndex( (*sit).index, nx );
            distx = distanceXY( pt, ptsit);
            if ( distx < dist ) {
                dist =  distx;
                res = * it;
            }
        }

    }
    /* assign all that need assignment to res, which has maxdiff */
    for ( it = nb.begin(); it != nb.end(); it++ ) {
        if ( *it == res ) continue;
        if ( !get_seed( seeds, *it, sit ) ) continue;
        if ( fabs( src[ ind ] - src[ (*sit).index ] ) >= tolerance ) continue;
        for ( i = 0; i < nx * ny; i++ )
            if ( tgt[ i ] == *it )
                tgt[ i ] = res;
        seeds.erase( sit );
    }
    return res;
}
Пример #3
0
void WQQuiz::addToList(int aCol, int bCol)
{
  //build a list of row numbers containing text in both columns

  typedef QValueList<int> IntList;
  IntList tempList;
  for (int current = 0; current < m_table ->numRows(); ++current)
  {
    if (!m_table->text(current, 0).isEmpty() && !m_table->text(current, 1).isEmpty())
    {
      tempList.append(current);
    }
  }

  KRandomSequence *rs = new KRandomSequence(0);

  int count = tempList.count();

  IntList::ConstIterator it;
  for ( it = tempList.begin(); it != tempList.end(); ++it )
  {
    WQListItem *li;
    li = new WQListItem();
    li->setQuestion(aCol);
    li->setCorrect(1);
    li->setOneOp(*it);

    if (count > 2)
    {

      int a, b;
      do
        a = rs->getLong(count); //rand() % count;
      while(a==*it);

      li->setTwoOp(a);

      do
        b = rs->getLong(count); //rand() % count;
      while(b == *it || b == a /*|| b < 0*/);

      li->setThreeOp(b);

    }
    m_quizList.append(*li);

  }

}
Пример #4
0
int main () {
    
    std::cout << "Radix sort program"  << std::endl;

    const IntList::value_type data[] = { 624, 852, 426, 987, 269,
                                       146, 415, 301, 730, 78, 593 };

    IntList values (data, data + sizeof data / sizeof *data);

    radixSort (values);
    std::copy (values.begin (), values.end (), OstreamIter (std::cout, " "));

    std::cout << std::endl << "End radix sort program" << std::endl;

    return 0;   
}
Пример #5
0
// Given a list of lists of possible cage values:
//     [[1,2,3], [3,4,5]]
// Recursively generates tuples of combinations from each of the lists as
// follows:
//   [1,3]
//   [1,4]
//   [1,5]
//   [2,3]
//   [2,4]
// ... etc
// Each of these is checked against the target sum, and pushed into a result
// vector if they match.
// Note: The algorithm assumes that the list of possibles/candidates are
// ordered. This allows it to bail out early if it detects there's no point
// going further.
static void subsetSum(const std::vector<IntList> &possible_lists,
                      const std::size_t p_size, IntList &tuple,
                      unsigned tuple_sum, std::vector<IntList> &subsets,
                      const unsigned target_sum, unsigned list_idx) {
  for (unsigned p = list_idx; p < p_size; ++p) {
    for (auto &poss : possible_lists[p]) {
      // Optimization for small target sums: if the candidate is bigger than
      // the target itself then it can't be valid, neither can any candidate
      // after it (ordered).
      if (target_sum < static_cast<unsigned>(poss)) {
        break;
      }

      // Can't repeat a value inside a cage
      if (std::find(tuple.begin(), tuple.end(), poss) != tuple.end()) {
        continue;
      }

      // Pre-calculate the new tuple values to avoid spurious
      // insertions/deletions to the vector.
      const auto new_tuple_sum = tuple_sum + poss;
      const auto new_tuple_size = tuple.size() + 1;

      // If we've added too much then we can bail out (ordered).
      if (new_tuple_sum > target_sum) {
        break;
      }

      // If there are fewer spots left in the tuple than there are options for
      // the sum to reach the target, bail.
      // TODO: This could be more sophisticated (can't have more than one 1, so
      // it's more like the N-1 sum that it should be greater than.
      if ((p_size - new_tuple_size) > (target_sum - new_tuple_sum)) {
        break;
      }

      if (new_tuple_size == p_size) {
        // If we've reached our target size then we can stop searching other
        // possiblities from this list (ordered).
        if (new_tuple_sum == target_sum) {
          tuple.push_back(poss);
          subsets.push_back(tuple);
          tuple.pop_back();
          break;
        }

        // Else, move on to the next candidate in the list.
        continue;
      }

      tuple_sum += poss;
      tuple.push_back(poss);

      subsetSum(possible_lists, p_size, tuple, tuple_sum, subsets, target_sum,
                p + 1);

      tuple.pop_back();
      tuple_sum -= poss;
    }
  }
}
Пример #6
0
/*----------------------------------------------------------------------- */
SEXP
watershed (SEXP x, SEXP _tolerance, SEXP _ext) {
    SEXP res;
    int im, i, j, nx, ny, nz, ext, nprotect = 0;
    double tolerance;

    nx = INTEGER ( GET_DIM(x) )[0];
    ny = INTEGER ( GET_DIM(x) )[1];
    nz = getNumberOfFrames(x,0);
    tolerance = REAL( _tolerance )[0];
    ext = INTEGER( _ext )[0];

    PROTECT ( res = Rf_duplicate(x) );
    nprotect++;
  
    int * index = new int[ nx * ny ];

    for ( im = 0; im < nz; im++ ) {

        double * src = &( REAL(x)[ im * nx * ny ] );
        double * tgt = &( REAL(res)[ im * nx * ny ] );

        /* generate pixel index and negate the image -- filling wells */
        for ( i = 0; i < nx * ny; i++ ) {
	  tgt[ i ] = -src[ i ];
	  index[ i ] = i;
        }
        /* from R includes R_ext/Utils.h */
        /* will resort tgt as well */
        rsort_with_index( tgt, index, nx * ny );
        /* reassign tgt as it was reset above but keep new index */
        for ( i = 0; i < nx * ny; i++ )
            tgt[ i ] = -src[ i ];

        SeedList seeds;  /* indexes of all seed starting points, i.e. lowest values */

        IntList  equals; /* queue of all pixels on the same gray level */
        IntList  nb;     /* seed values of assigned neighbours */
        int ind, indxy, nbseed, x, y, topseed = 0;
        IntList::iterator it;
        TheSeed newseed;
        PointXY pt;
        bool isin;
        /* loop through the sorted index */
        for ( i = 0; i < nx * ny && src[ index[i] ] > BG; ) {
            /* pool a queue of equally lowest values */
            ind = index[ i ];
            equals.push_back( ind );
            for ( i = i + 1; i < nx * ny; ) {
                if ( src[ index[i] ] != src[ ind ] ) break;
                equals.push_back( index[i] );
                i++;
            }
            while ( !equals.empty() ) {
                /* first check through all the pixels if we can assign them to
                 * existing objects, count checked and reset counter on each assigned
                 * -- exit when counter equals queue length */
                for ( j = 0; j < (int) equals.size(); ) {
		  if ((j%1000)==0) R_CheckUserInterrupt();
                    ind = equals.front();
                    equals.pop_front();
                    /* check neighbours:
                     * - if exists one, assign
                     * - if two or more check what should be combined and assign to the steepest
                     * - if none, push back */
                    /* reset j to 0 every time we assign another pixel to restart the loop */
                    nb.clear();
                    pt = pointFromIndex( ind, nx );
                    /* determine which neighbour we have, push them to nb */
                    for ( x = pt.x - ext; x <= pt.x + ext; x++ )
                        for ( y = pt.y - ext; y <= pt.y + ext; y++ ) {
                            if ( x < 0 || y < 0 || x >= nx || y >= ny || (x == pt.x && y == pt.y) ) continue;
                            indxy = x + y * nx;
                            nbseed = (int) tgt[ indxy ];
                            if ( nbseed < 1 ) continue;
                            isin = false;
                            for ( it = nb.begin(); it != nb.end() && !isin; it++ )
                                if ( nbseed == *it ) isin = true;
                            if ( !isin ) nb.push_back( nbseed );
                        }
                    if ( nb.size() == 0 ) {
                        /* push the pixel back and continue with the next one */
                        equals.push_back( ind );
                        j++;
                        continue;
                    }
                    tgt[ ind ] = check_multiple(tgt, src, ind, nb, seeds, tolerance, nx, ny );
                    /* we assigned the pixel, reset j to restart neighbours detection */
                    j = 0;
                }
                /* now we have assigned all that we could */
                if ( !equals.empty() ) {
                    /* create a new seed for one pixel only and go back to assigning neighbours */
                    topseed++;
                    newseed.index = equals.front();
                    newseed.seed = topseed;
                    equals.pop_front();
                    tgt[ newseed.index ] = topseed;
                    seeds.push_back( newseed );
                }
            } // assigning equals
        } // sorted index

        /* now we need to reassign indexes while some seeds could be removed */
        double * finseed = new double[ topseed ];
        for ( i = 0; i < topseed; i++ )
            finseed[ i ] = 0;
        i = 0;
        while ( !seeds.empty() ) {
            newseed = seeds.front();
            seeds.pop_front();
            finseed[ newseed.seed - 1 ] = i + 1;
            i++;
        }
        for ( i = 0; i < nx * ny; i++ ) {
            j = (int) tgt[ i ];
            if ( 0 < j && j <= topseed )
                tgt[ i ] = finseed[ j - 1 ];
        }
        delete[] finseed;

    } // loop through images

    delete[] index;

    UNPROTECT (nprotect);
    return res;
}
Пример #7
0
	TestList()
	{
		typedef List<char, DebugAllocator<int> > IntList;

		IntList a;
		a.push_back('1');
		a.push_back('2');
		a.push_back('3');
		LIST_ASSERT(*a.begin() == '1');
		LIST_ASSERT(*++a.begin() == '2');
		LIST_ASSERT(*++++a.begin() == '3');
		LIST_ASSERT(++++++a.begin() == a.end());

		IntList b;
		b.push_back('a');
		b.push_back('b');
		b.push_back('c');
		LIST_ASSERT(*b.begin() == 'a');
		LIST_ASSERT(*++b.begin() == 'b');
		LIST_ASSERT(*++++b.begin() == 'c');
		LIST_ASSERT(++++++b.begin() == b.end());

		// swap full lists
		a.swap(b);
		LIST_ASSERT(*a.begin() == 'a');
		LIST_ASSERT(*++a.begin() == 'b');
		LIST_ASSERT(*++++a.begin() == 'c');
		LIST_ASSERT(++++++a.begin() == a.end());
		LIST_ASSERT(*b.begin() == '1');
		LIST_ASSERT(*++b.begin() == '2');
		LIST_ASSERT(*++++b.begin() == '3');
		LIST_ASSERT(++++++b.begin() == b.end());

		// swap to/from empty list
		IntList c;
		c.swap(b);
		LIST_ASSERT(b.empty());
		LIST_ASSERT(*c.begin() == '1');
		LIST_ASSERT(*++c.begin() == '2');
		LIST_ASSERT(*++++c.begin() == '3');
		LIST_ASSERT(++++++c.begin() == c.end());

		c.swap(b);
		LIST_ASSERT(c.empty());
		LIST_ASSERT(*b.begin() == '1');
		LIST_ASSERT(*++b.begin() == '2');
		LIST_ASSERT(*++++b.begin() == '3');
		LIST_ASSERT(++++++b.begin() == b.end());

		IntList d;
		c.swap(d);
		LIST_ASSERT(c.empty());
		LIST_ASSERT(d.empty());

		c.splice(c.end(), d);
		LIST_ASSERT(c.empty());
		LIST_ASSERT(d.empty());

		// splice full with empty
		b.splice(b.end(), c);
		LIST_ASSERT(c.empty());
		LIST_ASSERT(*b.begin() == '1');
		LIST_ASSERT(*++b.begin() == '2');
		LIST_ASSERT(*++++b.begin() == '3');
		LIST_ASSERT(++++++b.begin() == b.end());

		// splice empty with full
		c.splice(c.end(), b);
		LIST_ASSERT(b.empty());
		LIST_ASSERT(*c.begin() == '1');
		LIST_ASSERT(*++c.begin() == '2');
		LIST_ASSERT(*++++c.begin() == '3');
		LIST_ASSERT(++++++c.begin() == c.end());

		// splice full with full
		c.splice(c.end(), a);
		LIST_ASSERT(a.empty());
		LIST_ASSERT(*c.begin() == '1');
		LIST_ASSERT(*++c.begin() == '2');
		LIST_ASSERT(*++++c.begin() == '3');
		LIST_ASSERT(*++++++c.begin() == 'a');
		LIST_ASSERT(*++++++++c.begin() == 'b');
		LIST_ASSERT(*++++++++++c.begin() == 'c');
		LIST_ASSERT(++++++++++++c.begin() == c.end());


		c.pop_back();
		LIST_ASSERT(!c.empty());
		c.pop_back();
		LIST_ASSERT(!c.empty());
		c.pop_back();
		LIST_ASSERT(!c.empty());
		c.pop_back();
		LIST_ASSERT(!c.empty());
		c.pop_back();
		LIST_ASSERT(!c.empty());
		c.pop_back();
		LIST_ASSERT(c.empty());
	}