コード例 #1
0
static inline void fix_priority_queue(queue the_queue)
{
  int left, right;
  int index;
  int smallest;

  index = 1;

  while (index < the_queue->num_elements) 
    {      
      left = 2*index;
      right = 2*index+1;
      
      if (left <= the_queue->num_elements && 
	  the_queue->data_array[left-1]->utility <
	  the_queue->data_array[index-1]->utility)
	smallest = left;
      else
	smallest = index;
      if (right <= the_queue->num_elements && 
	  the_queue->data_array[right-1]->utility <
	  the_queue->data_array[smallest - 1]->utility)
	smallest = right;
      
      if (smallest != index)
	{
	  swap_entries(smallest, index, the_queue);
	  index = smallest;
	}
      else
	break;
    }
}
コード例 #2
0
ファイル: conventional.c プロジェクト: acekiller/MasterThesis
static carmen_inline void
fix_priority_queue(queue the_queue)
{
  int left, right;
  int index;
  int largest;

  index = 1;

  while (index < the_queue->num_elements) 
    {      
      left = 2*index;
      right = 2*index+1;
      
      if (left <= the_queue->num_elements && 
	  the_queue->data_array[left-1]->cost > the_queue->data_array[index-1]->cost)
	largest = left;
      else
	largest = index;
      if (right <= the_queue->num_elements && 
	  the_queue->data_array[right-1]->cost > the_queue->data_array[largest - 1]->cost)
	largest = right;
      
      if (largest != index)
	{
	  swap_entries(largest, index, the_queue);
	  index = largest;
	}
      else
	break;
    }
}
コード例 #3
0
ファイル: topocache.c プロジェクト: HunterChen/GRAPES
void cache_randomize(const struct peer_cache *c)
{
  int i;

  for (i = 0; i < c->current_size - 1; i++) {
    int j, k;
    uint32_t ts = c->entries[i].timestamp;

    for (j = i + 1; j < c->current_size; j++) {
      if (c->entries[j].timestamp != ts) {
        break;
      }
    }

    //permutate entries from i to j-1
    if (j - 1 > i) {
      for (k = i; k < j - 1; k++) {
        int r;
        double rd;
        r = (rand() / (RAND_MAX + 1.0)) * (j - k);
        swap_entries(c, k, k + r);
      }
    }

    i = j - 1;
  }
}
コード例 #4
0
ファイル: playlist.c プロジェクト: dsheeler/xmms2
/**
 * Shuffle the playlist.
 *
 */
static void
xmms_playlist_client_shuffle (xmms_playlist_t *playlist, const gchar *plname,
                              xmms_error_t *err)
{
	guint j,i;
	gint len, currpos;
	xmmsv_coll_t *plcoll;

	g_return_if_fail (playlist);

	g_mutex_lock (playlist->mutex);

	plcoll = xmms_playlist_get_coll (playlist, plname, err);
	if (plcoll == NULL) {
		xmms_error_set (err, XMMS_ERROR_NOENT, "no such playlist");
		g_mutex_unlock (playlist->mutex);
		return;
	}

	currpos = xmms_playlist_coll_get_currpos (plcoll);
	len = xmms_playlist_coll_get_size (plcoll);
	if (len > 1) {
		/* put current at top and exclude from shuffling */
		if (currpos != -1) {
			swap_entries (plcoll, 0, currpos);
			currpos = 0;
			xmms_collection_set_int_attr (plcoll, "position", currpos);
		}

		/* knuth <3 */
		for (i = currpos + 1; i < len; i++) {
			j = g_random_int_range (i, len);

			if (i != j) {
				swap_entries (plcoll, i, j);
			}
		}

	}

	XMMS_PLAYLIST_CHANGED_MSG (XMMS_PLAYLIST_CHANGED_SHUFFLE, 0, plname);
	XMMS_PLAYLIST_CURRPOS_MSG (currpos, plname);

	g_mutex_unlock (playlist->mutex);
}
コード例 #5
0
//******************************************
BOOLEAN		PERMUTATION::dict_advance()
{
	//dict = dictionary
	//In dictionary ordering,
	//first permutation is the identity permutation
	//(pmut[0]=0, pmut[1]=1, etc. ).
	//This method advances the permutation to
	//the next permutation in dictionary ordering.
	//It returns false if we've reached the last one.
	//Otherwise it returns true.
	//Ref:book "Combinatorial Generation", by Joe Sawada
	/*
	Example
	04321
	     step:14320
	     step:10324
	     step:10234
	*/
		
	SHORT	 k, j;
	SHORT	 s, r;

	ThrowIf_(its_len<=1); 
	k = its_len - 2;
	while(its_array_p[k] > its_array_p[k+1]){
	 	k--;
	 	if(k==-1)break;
	}
	if (k == -1){
		return false;
	}else{
		j = its_len - 1;
		while(its_array_p[k] > its_array_p[j]){
			j--;
		}
		swap_entries(j, k);
		s = k+1; r = its_len - 1;
		while(s<r){
			swap_entries(r, s);
			s++; r--;
		}
	}
	return true;
}
コード例 #6
0
ファイル: rom.c プロジェクト: mrzzzrm/mooboy
static void sort_entries() {
    int swapped, e;

    do {
        swapped = 0;
        for(e  = 0; e < list->num_entries - 1; e++) {
            if(direntries[e]->is_file && !direntries[e+1]->is_file) {
                swap_entries(e);
                swapped = 1;
            }
            if(!direntries[e]->is_file && direntries[e+1]->is_file) {
                continue;
            }
            if(strcasecmp(direntries[e]->name, "..") > 0 &&
               strcasecmp(direntries[e]->name, direntries[e+1]->name) > 0) {
                swap_entries(e);
                swapped = 1;
            }
        }
    } while(swapped);
}
コード例 #7
0
//******************************************
BOOLEAN		PERMUTATION::rev_dict_advance()
{
	//rev_dict = reverse dictionary
	//In reverse dictionary ordering,
	//last permutation is the identity permutation
	//(pmut[0]=0, pmut[1]=1, etc. ).
	//This method advances the permutation to
	//the next permutation in reverse dictionary ordering.
	//It returns false if we've reached the last one.
	//Otherwise it returns true.
	//see PERMUTATION::dict_advance()		
	SHORT	 k, j;
	SHORT	 s, r;

	ThrowIf_(its_len<=1); 
	k = its_len - 2;
	while(its_array_p[its_len -1 - k] > its_array_p[its_len -2 - k]){
	 	k--;
	 	if(k==-1)break;
	}
	if (k == -1){
		return false;
	}else{
		j = its_len - 1;
		while(its_array_p[its_len -1 - k] > its_array_p[its_len -1 - j]){
			j--;
		}
		swap_entries(its_len -1 - j, its_len -1 - k);
		s = k+1; r = its_len - 1;
		while(s<r){
			swap_entries(its_len -1 - r, its_len -1 - s);
			s++; r--;
		}
	}
	return true;
}
コード例 #8
0
static inline void insert_into_queue(state_ptr new_state, queue the_queue) 
{
  int index;

  if (!the_queue->queue_size || 
      the_queue->queue_size == the_queue->num_elements) 
    resize_queue(the_queue);

  the_queue->data_array[the_queue->num_elements] = new_state;
  the_queue->num_elements++;

  /* Fix up priority queue */

  index = the_queue->num_elements;
  
  while (index > 1 && get_parent_value(the_queue, index) > new_state->utility) 
    {
      swap_entries(carmen_trunc(index/2), index, the_queue);
      index = carmen_trunc(index/2);
    }
}
コード例 #9
0
static inline void lower_cost(int id, int parent_id, double cost, 
			      double new_utility, queue state_queue)
{
  int i;

  for (i = 0; i < state_queue->num_elements; i++) {
    if (state_queue->data_array[i]->id == id) {
      state_queue->data_array[i]->parent_id = parent_id;
      state_queue->data_array[i]->cost = cost;
      state_queue->data_array[i]->utility = new_utility;      
    }
  }

  /* Fix up priority queue */

  i = state_queue->num_elements;
  
  while (i > 1 && get_parent_value(state_queue, i) > new_utility) {
    swap_entries(carmen_trunc(i/2), i, state_queue);
    i = carmen_trunc(i/2);
  }
}
コード例 #10
0
ファイル: linklist.c プロジェクト: chixsh/libhl
int
list_swap_values(linked_list_t *list,  size_t pos1, size_t pos2)
{
    return swap_entries(list, pos1, pos2);
}
コード例 #11
0
ファイル: statistc.cpp プロジェクト: coffeesam/tesseract-ocr
DLLSYM inT32
choose_nth_item (                //fast median
inT32 index,                     //index to choose
void *array,                     //array of items
inT32 count,                     //no of items
size_t size,                     //element size
                                 //comparator
int (*compar) (const void *, const void *)
) {
  int result;                    //of compar
  inT32 next_sample;             //next one to do
  inT32 next_lesser;             //space for new
  inT32 prev_greater;            //last one saved
  inT32 equal_count;             //no of equal ones
  inT32 pivot;                   //proposed median

  if (count <= 1)
    return 0;
  if (count == 2) {
    if (compar (array, (char *) array + size) < 0) {
      return index >= 1 ? 1 : 0;
    }
    else {
      return index >= 1 ? 0 : 1;
    }
  }
  if (index < 0)
    index = 0;                   //ensure lergal
  else if (index >= count)
    index = count - 1;
  pivot = (inT32) (rand () % count);
  swap_entries (array, size, pivot, 0);
  next_lesser = 0;
  prev_greater = count;
  equal_count = 1;
  for (next_sample = 1; next_sample < prev_greater;) {
    result =
      compar ((char *) array + size * next_sample,
      (char *) array + size * next_lesser);
    if (result < 0) {
      swap_entries (array, size, next_lesser++, next_sample++);
      //shuffle
    }
    else if (result > 0) {
      prev_greater--;
      swap_entries(array, size, prev_greater, next_sample);
    }
    else {
      equal_count++;
      next_sample++;
    }
  }
  if (index < next_lesser)
    return choose_nth_item (index, array, next_lesser, size, compar);
  else if (index < prev_greater)
    return next_lesser;          //in equal bracket
  else
    return choose_nth_item (index - prev_greater,
      (char *) array + size * prev_greater,
      count - prev_greater, size,
      compar) + prev_greater;
}