Пример #1
0
// rearrange unselected shapes in random sequence
bool Model::AutoArrange(vector<Gtk::TreeModel::Path> &path)
{
  // all shapes
  vector<Shape*>   allshapes;
  vector<Matrix4d> transforms;
  objtree.get_all_shapes(allshapes, transforms);

  // selected shapes
  vector<Shape*>   selshapes;
  vector<Matrix4d> seltransforms;
  objtree.get_selected_shapes(path, selshapes, seltransforms);

  // get unselected shapes
  vector<Shape*>   unselshapes;
  vector<Matrix4d> unseltransforms;

  for(uint s=0; s < allshapes.size(); s++) {
    bool issel = false;
    for(uint ss=0; ss < selshapes.size(); ss++)
      if (selshapes[ss] == allshapes[s]) {
	issel = true; break;
      }
    if (!issel) {
      unselshapes.    push_back(allshapes[s]);
      unseltransforms.push_back(transforms[s]);
    }
  }

  // find place for unselected shapes
  int num = unselshapes.size();
  vector<int> rand_seq(num,1); // 1,1,1...
  partial_sum(rand_seq.begin(), rand_seq.end(), rand_seq.begin()); // 1,2,3,...,N

  Glib::TimeVal timeval;
  timeval.assign_current_time();
  srandom((unsigned long)(timeval.as_double()));
  random_shuffle(rand_seq.begin(), rand_seq.end()); // shuffle

  for(int s=0; s < num; s++) {
    int index = rand_seq[s]-1;
    // use selshapes as vector to fill up
    Vector3d trans = FindEmptyLocation(selshapes, seltransforms, unselshapes[index]);
    selshapes.push_back(unselshapes[index]);
    seltransforms.push_back(unseltransforms[index]); // basic transform, not shape
    selshapes.back()->transform3D.move(trans);
    CalcBoundingBoxAndCenter();
  }
  ModelChanged();
  return true;
}
Пример #2
0
int main(int argc, char **argv)
{
   const char *book;
   char *pos;
   struct stat st;
   int **seq;
   int i, count;

   if (argc != 3)
     {
        printf("Usage ./oracle [book] [count]\n");
        exit(2);
     }

   book = argv[1];
   if (stat(book, &st) < 0)
     exit(1);
  
   count = atoi(argv[2]);
   if (count <= 0) exit(1 << 3);

   int total_bytes = st.st_size;
 
   seq = rand_seq(count, total_bytes);

   int fd = open(book, 0444);
   if (fd == -1)
     {
        fprintf(stderr, "ERR: could not open '%s'\n", book);
        exit(1 << 2);
     }

   char *map = (char *) mmap(NULL, total_bytes, PROT_READ, MAP_SHARED, fd, 0);
   close(fd);

   i = 0;

   while (i < count)
     {
        int byte = 0;
        while (byte < total_bytes)
          {
             pos = map + byte++;
             if (byte == *seq[i])
               {
                  char *end, *start = pos;
                  while (*start++ != ' ');
                  end = strchr(start, ' ');
                  size_t len = end - start;
                  char *word = malloc(len);
                  memcpy(word, start, len);
                  word[len] = '\0';
                  _trim(word);
                  printf(" %s ", word); 
                  free(word);
                  free(seq[i]);
                  i++;
                  break;
               }
          } 
     }

   free(seq);
   printf("\n");

   return EXIT_SUCCESS;
}