Esempio n. 1
0
ntree_t *ntree_splice(ntree_t *tree, ntree_t *node, ntree_cmpfunc cmpfunc) {
  int i;
  ntree_t *cur;

  if(!tree)			/* this will become the root node */
    return node;

  assert(tree->nchildren == node->nchildren);


  cur = ntree_root(tree);

  /* now we actually add it to the tree */
  while(1) {
    i = cmpfunc(node, cur);
    assert(i < node->nchildren);

    if(cur->child[i]) {
      cur = cur->child[i];
      continue;
    } else {
      cur->child[i] = node;
      node->parent = cur;
      break;
    }
  }

  return tree;
}
Esempio n. 2
0
void
bubblesort(void *base, size_t nmemb, size_t size,
    int (*cmpfunc)(const void *, const void *))
{
	int swaptype;
	char *tmp1 = (char *)base;
	char *tmp2 = (char *)base + size;

	SWAPINIT(base, size);

	size_t i = 0;
	size_t n = nmemb;
	size_t nnew;
	do {
		nnew = 0;
		for (i = 1; i <= n - 1; i++) {
			tmp1 = (char *)base + (size * (i - 1));
			tmp2 = (char *)base + (size * ((i - 1) + 1));
			if (cmpfunc(tmp1, tmp2) > 0) {
				swap(tmp1, tmp2);
				nnew = i;
			}
		}
		n = nnew;
	} while (n > 0);
}
/* sort a linked list with selection sort */
void irlist_sort2(irlist_t *list, int (*cmpfunc)(const void *a, const void *b))
{
  irlist_t newlist = {0, 0, 0};
  void *cur;
  void *ltry;
  void *last;

  while ((cur = irlist_get_head(list))) {
    irlist_remove(list, cur);

    ltry = irlist_get_head(&newlist);
    if (!ltry) {
      irlist_insert_head(&newlist, cur);
      continue;
    }

    last = NULL;
    while (ltry) {
      if (cmpfunc(cur, ltry) < 0) {
        break;
      }
      last = ltry;
      ltry = irlist_get_next(ltry);
    }

    if (!last) {
      irlist_insert_head(&newlist, cur);
    } else {
      irlist_insert_after(&newlist, cur, last);
    }
  }

  *list = newlist;
  return;
}
Esempio n. 4
0
void* lsearch(void* key, void* base, int n, int elemSize, int (*cmpfunc)(void*, void*)) {
	int i;
	for(i = 0; i < n; i++) {
		void* elemAddr = (char *)base + sizeof(char) * i * elemSize;
		if(!cmpfunc(key, elemAddr))
			return elemAddr;
	}
	
	return NULL;
}
Esempio n. 5
0
listitem *list_search(list *lp, listsearchfunc cmpfunc, const void *arg)
{
    listitem *lip;

    if(!lp)
        return 0;

    lip = lp->first;
    while(lip) {
        if(cmpfunc(lip->data, arg) == 0)
            return lip;
        lip = lip->next;
    }
    return 0;
}
Esempio n. 6
0
        static int make_chain(
            vec2* V, int n,
            int (*cmp)(vec2*, vec2*)
        ) {
            int i, j, s = 1;
            vec2 t;
            
            ::qsort(V, n, sizeof(vec2), cmpfunc(cmp));

            for (i=2; i<n; i++) {
                for (j=s; j>=1 && ccw(V, i, j, j-1); j--){}
                s = j+1;
                t = V[s]; V[s] = V[i]; V[i] = t;
            }
            return s;
        }
Esempio n. 7
0
/* $ 11.10.2001 IS
   Подсчитать количество фреймов с указанным именем.
*/
int Manager::CountFramesWithName(const wchar_t *Name, BOOL IgnoreCase)
{
	int Counter=0;
	typedef int (__cdecl *cmpfunc_t)(const wchar_t *s1, const wchar_t *s2);
	cmpfunc_t cmpfunc=IgnoreCase ? StrCmpI : StrCmp;
	string strType, strCurName;

	for (int I=0; I<FrameCount; I++)
	{
		FrameList[I]->GetTypeAndName(strType, strCurName);

		if (!cmpfunc(Name, strCurName)) ++Counter;
	}

	return Counter;
}
Esempio n. 8
0
static struct Splay *find(struct Splay **root, SplayCmpFunc cmpfunc, CONST_APTR key) {
	struct Splay *sn = *root;
	int res;

	while (sn != NULL) {
		res = cmpfunc(key, sn->key);
		if (res > 0)
			sn = sn->right;
		else if (res < 0)
			sn = sn->left;
		else
			break;
	}

	return sn;
}
Esempio n. 9
0
static inline void *gt_rbtree_find_with_cmp_g(GtRBTree *tree, void *key,
                                              GtCompareWithData cmpfunc,
                                              void *info)
{
  GtRBTreeNode *it = tree->root;

  while (it != NULL) {
    int cmp = cmpfunc(it->key, key, info);

    if (cmp == 0)
      break;

    it = it->link[(int) (cmp < 0)];
  }

  return it == NULL ? NULL : it->key;
}
Esempio n. 10
0
List *list_find (List *l, const void *data, 
                 int (*cmpfunc)(const void *, const void *))
/* 
 * If data matching 's' is found, return it's address.  If no such data is 
 * found, return NULL.
 *
 * 'cmpfunc' should return 0 for equal, non 0 for unequal
 */
{
    List *h;

    if (!l)  return NULL;

    for (h = l; h; h = h->next)
        if (cmpfunc(data, h->data) == 0)  return h;

    return NULL;
}
Esempio n. 11
0
ircnet *servlist_net_find(char *name, int *pos, int (*cmpfunc)(const char *, const char *))
{
        GSList *list = network_list;
        ircnet *net;
        int i = 0;

        while (list) {
                net = list->data;
                if (cmpfunc(net->name, name) == 0) {
                        if (pos)
                                *pos = i;
                        return net;
                }
                i++;
                list = list->next;
        }

        return NULL;
}
Esempio n. 12
0
ircnet *
servlist_net_find (char *name, int *pos, int (*cmpfunc) (const char *, const char *))
{
	GSList *list = network_list;
	ircnet *net;
	int i = 0;

	while (list)
	{
		net = static_cast<ircnet *>(list->data);
		if (cmpfunc (net->name.c_str(), name) == 0)
		{
			if (pos)
				*pos = i;
			return net;
		}
		i++;
		list = list->next;
	}

	return nullptr;
}
/* sort a linked list with merge sort */
void irlist_sort2(irlist_t *list, int (*cmpfunc)(const void *a, const void *b))
{
  irlist_t newlist = {0, 0, 0};
  irlist_t *p;
  irlist_t *q;
  irlist_t *e;
  irlist_t *tail;
  int insize;
  int nmerges;
  int psize;
  int qsize;
  int i;

  insize = 1;
  for(;;) {
    p = irlist_get_head(list);
    list = 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 = irlist_get_next(q);
        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 = irlist_get_next(q);
	  irlist_remove(list, e);
          qsize--;
        } else if (qsize == 0 || !q) {
          /* q is empty; e must come from p. */
          e = p;
          p = irlist_get_next(p);
	  irlist_remove(list, e);
          psize--;
        } else if (cmpfunc(p, q) <= 0) {
          /* First element of p is lower (or same);
          * e must come from p. */
          e = p;
          p = irlist_get_next(p);
	  irlist_remove(list, e);
          psize--;
        } else {
          /* First element of q is lower; e must come from q. */
          e = q;
          q = irlist_get_next(q);
	  irlist_remove(list, e);
          qsize--;
        }

        /* add the next element to the merged list */
        if (tail) {
          irlist_insert_after(&newlist, e, tail);
        } else {
          list = e;
        }
        tail = e;
      }

      /* now p has stepped `insize' places along, and q has too */
      p = q;
    }

    /* If we have done only one merge, we're finished. */
    if (nmerges <= 1)   /* allow for nmerges==0, the empty list case */
      break;

    /* Otherwise repeat, merging lists twice the size */
    insize *= 2;
  }
  *list = newlist;
}
Esempio n. 14
0
/*
 * str_InsertToTable - insert entry to table
 *
 *	IN:
 *		str   - string to be inserted
 *		ptr   - pointer to table structure
 *		kflag - keep flag (when match occurs which, longer or shorter string, to keep)
 *	OUT:
 *		opaque pointer to table structure
 */
void *str_InsertToTable(char *str, void *ptr, int kflag)
{
	strTbl *tbl = (strTbl *)ptr;
	unsigned int icase =
#ifdef	_MSC_VER
		1
#else
		0
#endif
		;
	unsigned int ix;
	unsigned int count;
	unsigned int slen = strlen(str);
	int (*cmpfunc)();

	/* sanity check */
	if (tbl == NULL) {
		/* there is NO table yet */
		tbl = str_InitTable();
	}

	/* use only lowercase if case is insensitive */
	if (icase) strlwr(str);

	/* search for sub-string match
	 */
	count = tbl->count;
	for (ix = 0; ix < count; ix++)
	{
		unsigned int elen = strlen(tbl->tbl[ix]);
		/*
		 * - check exact match
		 */
		if (strlen(str) == strlen(tbl->tbl[ix]))
		{
			/* select appropriate compare function */
			cmpfunc = icase ? stricmp : strcmp;
			if (cmpfunc(str, tbl->tbl[ix]) == 0) {
				/* no insertion needed */
				return (void*)tbl;
			}
		}
		else if (kflag == STR_KEEP_LONGER_STRING)
		{
			if (elen > slen && strstr(tbl->tbl[ix], str) != NULL) {
			/* longer string already exists */
				return (void*)tbl;
			}
			else
			if (slen > elen && strstr(str, tbl->tbl[ix]) != NULL) {
			/* replace with newer longer string */
				free(tbl->tbl[ix]);
				tbl->tbl[ix] = strdup(str);
				return (void*)tbl;
			}
		}
		else /* if (kflag == STR_KEEP_SHORTER_STRING) */
		{
			if (slen > elen && strstr(str, tbl->tbl[ix]) != NULL) {
			/* shorter string already exists */
				return (void*)tbl;
			}
			else
			if (elen > slen && strstr(tbl->tbl[ix], str) != NULL) {
			/* replace with newer shorter string */
				free(tbl->tbl[ix]);
				tbl->tbl[ix] = strdup(str);
				return (void*)tbl;
			}
		}
	}	/* for (ix = 0; ix < count; ix++) */
	/* no match found in the table
	 */
	(tbl->count)++;
	tbl->tbl = (char **)realloc(tbl->tbl, ((tbl->count)+1)*sizeof(char*));
	tbl->tbl[count] = strdup(str);
	tbl->tbl[tbl->count] = NULL;	/* make sure last entry is NULL */
	return (void*)tbl;
}