Esempio n. 1
0
void GDList::sort(int (*sort_func)(void *p1,void *p2))
{
    int     num = count();

    while(num)
    {
        NIDNode *p1 = GetHead();
        NIDNode *p2 = p1->GetNext();
        for(int k=1;k < num;k++)
        {
            // If this returns true, p1 is sorted toward the end

            if(sort_func(p1->GetObject(),p2->GetObject()))
            {
                GDBase::swap(p1,p2);
                p2 = p1->GetNext();
            }
            else
            {
                p1 = p2;
                p2 = p2->GetNext();
            }
        }
        num--;
    }
}
Esempio n. 2
0
long sort_testnm(int n, int m, void (*sort_func)(int*, int))
    {
    long t1, t2, t;
    int *a;
    a = set_array(a, n, m);
    t1 = get_tick();
    sort_func(a, n);
    t2 = get_tick();
    t = diff_tick(t1, t2);
    if (!is_sorted(a, n))
	t = -1;
    free(a);
    return t;
    }
Esempio n. 3
0
bool SORTPlugin::dofunc(const QString & func_name, const V3DPluginArgList & input, V3DPluginArgList & output, V3DPluginCallback2 & callback,  QWidget * parent)
{
	if (func_name == tr("sort_swc"))
	{
		sort_func(input, output);
	  return true;
	}
	else if (func_name == tr("help"))
	{
		printHelp(input,output);
	  return true;
	}
	else if (func_name == tr("TOOLBOXsort_swc"))
	{
		sort_toolbox(input);
		return true;
	}
	return false;
}
Esempio n. 4
0
void menu::do_sort()
{
	if(sorter_ == NULL || sorter_->column_sortable(sortby_) == false) {
		return;
	}

	const int selectid = selection();

	std::stable_sort(items_.begin(), items_.end(), sort_func(*sorter_, sortby_));
	if (sortreversed_)
		std::reverse(items_.begin(), items_.end());

	recalculate_pos();

	if(selectid >= 0 && selectid < int(item_pos_.size())) {
		move_selection_to(selectid, true, NO_MOVE_VIEWPORT);
	}

	set_dirty();
}
Esempio n. 5
0
static struct slab_info *merge_objs(struct slab_info *a, struct slab_info *b)
{
	struct slab_info sorted_list;
	struct slab_info *curr = &sorted_list;

	while ((a != NULL) && (b != NULL)) {
		if (sort_func(a, b)) {
			curr->next = a;
			curr = a;
			a = a->next;
		} else {
			curr->next = b;
			curr = b;
			b = b->next;
		}
	}

	curr->next = (a == NULL) ? b : a;
	return sorted_list.next;
}
Esempio n. 6
0
void do_benchmark(unsigned iterations, void (*sort_func)(int*, size_t), const int data_set[], size_t data_len)
{
    clock_t clk[2];
    unsigned long average_time = 0;
    unsigned i;

    /* Make a copy of the data and use this rather than the original */
    int data_cpy[data_len];
    memcpy(data_cpy, data_set, data_len);

    for (i = 0; i < iterations; i++)
    {
	clk[0] = clock();
	sort_func(data_cpy, data_len);
	clk[1] = clock();

	/* Add the difference in time to `average_time` */
	average_time += clk[1] - clk[0];
    }

    printf("Average CPU time: %f\n", (double)average_time / data_len);
}
Esempio n. 7
0
void awards_sort_players( int award )
{
	int sort_order = AWARD_LIST[ award ].sort_order;
	int i, j;
	int p1, p2;
	long (*sort_func)( int, int, int );
	long sort_compare;
	
	// init index;
	for ( i=0; i< player_count; i++ ) {
		player_index[ i ] = i;
	}

	sort_func = AWARD_LIST[ award ].sort_func;

	for ( i = 0 ; i < player_count ; i++ ) {
		for ( j = i + 1 ; j < player_count ; j++ ) {
			
			p1 = player_index[ i ];
			p2 = player_index[ j ];
			sort_compare = sort_func( AWARD_LIST[ award ].awardID,p1, p2 ); 

			switch( sort_order ) {
				case SO_ASCENDING :
					if ( sort_compare > 0 ) {
						awards_sort_swap( i, j );
					}
					break;
				case SO_DESCENDING :
					if ( sort_compare < 0 ) {
						awards_sort_swap( i, j );
					}
					break;
			}
		}
	}
}
Esempio n. 8
0
ESource *
em_utils_guess_mail_account_with_recipients_and_sort (ESourceRegistry *registry,
                                                      CamelMimeMessage *message,
                                                      CamelFolder *folder,
                                                      const gchar *message_uid,
                                                      EMailUtilsSourtSourcesFunc sort_func,
                                                      gpointer sort_func_data)
{
	ESource *source = NULL;
	GHashTable *recipients;
	CamelInternetAddress *addr;
	GList *list, *iter;
	const gchar *extension_name;
	const gchar *type;
	const gchar *key;

	/* This policy is subject to debate and tweaking,
	 * but please also document the rational here. */

	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
	g_return_val_if_fail (CAMEL_IS_MIME_MESSAGE (message), NULL);

	/* Build a set of email addresses in which to test for membership.
	 * Only the keys matter here; the values just need to be non-NULL. */
	recipients = g_hash_table_new (g_str_hash, g_str_equal);

	type = CAMEL_RECIPIENT_TYPE_TO;
	addr = camel_mime_message_get_recipients (message, type);
	if (addr != NULL) {
		gint index = 0;

		while (camel_internet_address_get (addr, index++, NULL, &key))
			g_hash_table_add (recipients, (gpointer) key);
	}

	type = CAMEL_RECIPIENT_TYPE_CC;
	addr = camel_mime_message_get_recipients (message, type);
	if (addr != NULL) {
		gint index = 0;

		while (camel_internet_address_get (addr, index++, NULL, &key))
			g_hash_table_add (recipients, (gpointer) key);
	}

	/* First Preference: We were given a folder that maps to an
	 * enabled mail account, and that account's address appears
	 * in the list of To: or Cc: recipients. */

	if (folder != NULL)
		source = guess_mail_account_from_folder (
			registry, folder, message_uid);

	if (source == NULL)
		goto second_preference;

	if (mail_account_in_recipients (registry, source, recipients))
		goto exit;

second_preference:

	/* Second Preference: Choose any enabled mail account whose
	 * address appears in the list to To: or Cc: recipients. */

	if (source != NULL) {
		g_object_unref (source);
		source = NULL;
	}

	extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
	list = e_source_registry_list_sources (registry, extension_name);

	if (sort_func)
		sort_func (&list, sort_func_data);

	for (iter = list; iter != NULL; iter = g_list_next (iter)) {
		ESource *temp = E_SOURCE (iter->data);

		if (e_source_registry_check_enabled (registry, temp) &&
		    mail_account_in_recipients (registry, temp, recipients)) {
			source = g_object_ref (temp);
			break;
		}
	}

	g_list_free_full (list, (GDestroyNotify) g_object_unref);

	if (source != NULL)
		goto exit;

	/* Last Preference: Defer to em_utils_guess_mail_account(). */
	source = em_utils_guess_mail_account (
		registry, message, folder, message_uid);

exit:
	g_hash_table_destroy (recipients);

	return source;
}
Esempio n. 9
0
void saim_list_sort(saim_list * list, bool (*sort_func)(const void*, const void*))
{
	// Using merge sort algorithm
	saim_list_node *p, *q, *e, *head, *tail;
	int insize, nmerges, psize, qsize, i;

	if (list->head == NULL || list->head->next == NULL)
		return;

	head = list->head;
	insize = 1;

	while (1)
	{
		p = head;
		head = NULL;
		tail = NULL;

		nmerges = 0;  /* count number of merges we do in this pass */

		while (p)
		{
            ++nmerges;  /* there exists a merge to be done */
            /* step `insize' places along from p */
            q = p;
            psize = 0;
            for (i = 0; i < insize; ++i)
            {
                ++psize;
				q = q->next;
                if (!q) break;
            }

            /* if q hasn't fallen off end, we have two lists to merge */
            qsize = insize;

            /* now we have two lists; merge them */
            while (psize > 0 || (qsize > 0 && q))
            {

                /* decide whether next element of merge comes from p or q */
                if (psize == 0) {
					/* p is empty; e must come from q. */
					e = q; q = q->next; --qsize;
				} else if (qsize == 0 || !q) {
					/* q is empty; e must come from p. */
					e = p; p = p->next; --psize;
				} else if (sort_func(p->data, q->data)) {
					/* First element of p is lower (or same);
					 * e must come from p. */
					e = p; p = p->next; --psize;
				} else {
					/* First element of q is lower; e must come from q. */
					e = q; q = q->next; --qsize;
				}

                /* add the next element to the merged list */
				if (tail) {
					tail->next = e;
				} else {
					head = e;
				}
				/* Maintain reverse pointers in a doubly linked list. */
				e->prev = tail;
				tail = e;
            }

            /* now p has stepped `insize' places along, and q has too */
            p = q;
        }
		tail->next = NULL;

        /* If we have done only one merge, we're finished. */
        if (nmerges <= 1)   /* allow for nmerges==0, the empty list case */
        {
        	list->head = head;
        	list->tail = tail;
            return;
        }

        /* Otherwise repeat, merging lists twice the size */
        insize *= 2;
    }
}
Esempio n. 10
0
gboolean
sort_func_simple(GNode *node, gpointer data)
{
  return sort_func(node,data,FALSE);
}
Esempio n. 11
0
gboolean
sort_func_priority(GNode *node, gpointer data)
{
  return sort_func(node,data,TRUE);
}