Esempio n. 1
0
BinTree bt_dup(BinTree bt, DupFuncPtr dupfunc)
{
    BinTree result;
    bt_node *oldnode;

    ErrPushFunc("bt_dup");

    result = bt_create(bt->compare);

    /* Save previoulsy current */

    oldnode = bt->current_node;

    /* Duplicate nodes */

    bt_GoFirst(bt);
    while(!bt_EOT(bt))
    {
        bt_add(result, (dupfunc==NULL) ? bt_GetCur(bt) : dupfunc(bt_GetCur(bt)));
        bt_GoNext(bt);
    }

    /* restore previously current */

    bt->current_node = oldnode;

    result->count = bt->count;

    ErrPopFunc();

    return(result);
}
/*
 * A sample driver
 */
int DoData(Btree *bt, char *buffer, int mode) {
    UR ur;
    char *s;
    s = strchr(buffer, ';');
    if (!s) {
        if (mode == 0)
            s = ""; /* don't need address for deletes */
        else
            return TREE_FAIL; /* trying to add partial data */
    }
    else
        *s++ = '\0';

    strncpy(ur.key, buffer, 10); /* key is first 10 characters */
    ur.key[10] = '\0';
    strncpy(ur.name, buffer, 24);
    ur.name[24] = '\0';
    strncpy(ur.addr, s, 24);
    ur.addr[24] = '\0';

    if (mode == 0)
        return bt_delete(bt, ur.key);
    else
        return bt_add(bt, &ur, ur.key);
}
void main () 
{
	char		c;
	int		n;
	tree_pointer	t;

	t = build_simple_tree();

	printf("************* Command ************\n");
	printf("C: Count tree, A: Add tree data    \n");
	printf("H: Height of tree, S: Show preorder \n");
	printf("F: Free tree, Q: Quit              \n");
	printf("**********************************\n");

	while (1) {
		printf("\nCommand> ");
		c = _getch();
		_putch(c);
		c = toupper(c);
		switch (c) {
			case 'C' : 
				n = bt_count(t);
				printf ("\n Total number of node = %d \n", n);
				break;
			case 'A' : 
				n = bt_add(t);
				printf ("\n Sum of tree data = %d \n", n);
				break;
			case 'H' : 
				n = bt_height(t);
				printf ("\n Height of tree = %d \n", n);
				break;
			case 'S' : 
				printf ("\n");
				bt_show_preorder(t); 
				printf ("\n");
				break;
			case 'F' : 
				printf ("\n");
				free_bt(t); 
				printf ("\n");
				break;
			case 'Q' : 
				printf("\n");
				exit(1);
			default  : break;
		}
	}
}
int
cdx_calcfiltlist(ClipMachine * ClipMachineMemory, RDD_DATA * rd, RDD_ORDER * ro, RDD_FILTER * fp, const char *__PROC__)
{
   int i, er;

   BTREE *bt;

   void *key = malloc(sizeof(unsigned int) + ro->bufsize);

   ClipVar vv, *vp;

   if (fp->list)
   {
      bt = bt_create(0, fp->listlen, sizeof(unsigned int) + ro->bufsize, _cdx_compare);
      for (i = 0; i < fp->listlen; i++)
      {
	 if ((er = rd->vtbl->rawgo(ClipMachineMemory, rd, fp->list[i], 0, __PROC__)))
	    return er;
	 if (ro->simpexpr)
	 {
	    if ((er = rd->vtbl->getvalue(ClipMachineMemory, rd, ro->simpfno, &vv, __PROC__)))
	       return er;
	    vp = &vv;
	 }
	 else
	 {
	    if ((er = rdd_calc(ClipMachineMemory, rd->area, &ro->block, &vv, 0)))
	       return er;
	    vp = _clip_vptr(&vv);
	 }
	 memcpy(key, &rd->recno, sizeof(unsigned int));
	 if ((er = cdx_formatkey(ClipMachineMemory, ro, vp, key + sizeof(unsigned int), __PROC__)))
	    return er;
	 _clip_destroy(ClipMachineMemory, &vv);
	 bt_add(bt, ro, key);
      }
      free(fp->list);
   }
   else
   {
#if 1
      unsigned int bytes = ((fp->size + 1) >> 5) + 1;

      int i, b, bb, t, tt;

      fp->listlen = 0;
      for (i = 0; i < bytes; i++)
      {
	 if (fp->rmap[i])
	 {
	    for (b = (i << 2), bb = 0; bb < 4; b++, bb++)
	    {
	       if (((char *) fp->rmap)[b])
	       {
		  for (t = (b << 3) + 1, tt = 0; tt < 8; t++, tt++)
		  {
		     if (_rm_getbit(fp->rmap, fp->size, t))
			fp->listlen++;
		  }
	       }
	    }
	 }
      }
      if (fp->listlen > 100)
      {
	 free(key);
	 return 0;
      }
#else
      fp->listlen = 0;
      for (i = 1; i <= fp->size; i++)
	 if (_rm_getbit(fp->rmap, fp->size, i))
	    fp->listlen++;
#endif
      bt = bt_create(0, fp->listlen, sizeof(unsigned int) + ro->bufsize, _cdx_compare);
#if 1
      for (i = 0; i < bytes; i++)
      {
	 if (fp->rmap[i])
	 {
	    for (b = (i << 2), bb = 0; bb < 4; b++, bb++)
	    {
	       if (((char *) fp->rmap)[b])
	       {
		  for (t = (b << 3) + 1, tt = 0; tt < 8; t++, tt++)
		  {
		     if (_rm_getbit(fp->rmap, fp->size, t))
		     {
			if ((er = rd->vtbl->rawgo(ClipMachineMemory, rd, t, 0, __PROC__)))
			   return er;
			if (ro->simpexpr)
			{
			   if ((er = rd->vtbl->getvalue(ClipMachineMemory, rd, ro->simpfno, &vv, __PROC__)))
			      return er;
			   vp = &vv;
			}
			else
			{
			   if ((er = rdd_calc(ClipMachineMemory, rd->area, &ro->block, &vv, 0)))
			      return er;
			   vp = _clip_vptr(&vv);
			}
			memcpy(key, &rd->recno, sizeof(unsigned int));
			if ((er = cdx_formatkey(ClipMachineMemory, ro, vp, key + sizeof(unsigned int), __PROC__)))
			   return er;
			_clip_destroy(ClipMachineMemory, &vv);
			bt_add(bt, ro, key);
		     }
		  }
	       }
	    }
	 }
      }
#else
      for (i = 1; i <= fp->size; i++)
      {
	 if (_rm_getbit(fp->rmap, fp->size, i))
	 {
	    if ((er = rd->vtbl->rawgo(ClipMachineMemory, rd, i, 0, __PROC__)))
	       return er;
	    if (ro->simpexpr)
	    {
	       if ((er = rd->vtbl->getvalue(ClipMachineMemory, rd, ro->simpfno, &vv, __PROC__)))
		  return er;
	       vp = &vv;
	    }
	    else
	    {
	       if ((er = rdd_calc(ClipMachineMemory, rd->area, &ro->block, &vv, 0)))
		  return er;
	       vp = _clip_vptr(&vv);
	    }
	    memcpy(key, &rd->recno, sizeof(unsigned int));
	    if ((er = cdx_formatkey(ClipMachineMemory, ro, vp, key + sizeof(unsigned int), __PROC__)))
	       return er;
	    _clip_destroy(ClipMachineMemory, &vv);
	    bt_add(bt, ro, key);
	 }
      }
#endif
   }
   fp->list = malloc(sizeof(unsigned int) * (fp->listlen + 1));
   if (!ro->descend)
   {
      bt_first(bt);
      if (bt_key(bt))
      {
	 i = 0;
	 fp->list[i] = *(unsigned int *) bt_key(bt);
	 while (!bt_next(bt))
	 {
	    i++;
	    fp->list[i] = *(unsigned int *) bt_key(bt);
	 }
      }
   }
   else
   {
      bt_last(bt);
      if (bt_key(bt))
      {
	 i = 0;
	 fp->list[i] = *(unsigned int *) bt_key(bt);
	 while (!bt_prev(bt))
	 {
	    i++;
	    fp->list[i] = *(unsigned int *) bt_key(bt);
	 }
      }
   }
   bt_destroy(bt);
   free(key);
   return 0;
}
int bt_add(tree_pointer ptr){ // 트리의 데이터 합을 계산
	if(ptr==NULL)
		return 0;
	return bt_add(ptr->left)+bt_add(ptr->right)+ptr->data;
}
Esempio n. 6
0
int main(void)
{
	bt_arg_t bt_arg;
	bt_t *bt = NULL;

	int seeding = 1;
	struct btstat tstat[10];
	char *torrents[10];
	int key,rv;
	int ntorrents = 1,i = 0;
	int tids[32] = {0};
	int ntids = 32;
	char id[32] = {0};

	torrents[0] = "./torrents/test.torrent";

	bt_arg.net_port = 0;
	bt_arg.empty_start = 1;
	bt_arg.use_upnp = 1;
	bt_arg.method = 1;


	if(bt_start_daemon(&bt_arg,&bt) != 0)
		return -1;
	rv = bt_add("download",torrents[0],bt);
	//bt_del(ntorrents,torrents,bt);
	//rv = bt_add_url("downloads",torrents[0],"http://127.0.0.1/test.torrent",bt);
	if(rv == 1)
	{
		bt_start(ntorrents,torrents,bt);
	}
	else if(rv < 0)
	{
		bt_stop_daemon(bt);
		return 0;
	}

	for(i = 0; i < 2; ++i)
	rv = bt_add_p2sp(torrents[0],"http://down1.chinaunix.net/distfiles/",bt);
	//rv = bt_add_p2sp(torrents[0],"http://127.0.0.1",bt);
	//bt_rate(30*1024,30*1024,bt);

	for(i = 0; i < ntorrents;++i)
	{
		bt_stat(torrents[i],bt,&tstat[i]);
		tids[i] = tstat[i].num;
	}

	rv = bt_tids(tids,&ntids,bt);

	while(1)
	{
		for(i = 0; i < ntorrents; ++i)
		{
			_snprintf(id,sizeof(id),"%d",tids[i]);
			bt_stat(id,bt,&tstat[i]);
			printf("%-20s ",strrchr(torrents[i],'/') + 1);
			print_stat(&tstat[i]);
		}
		if(_kbhit())
		{
			if((key =_getch()) == 115/*s key*/)
				break;
		}
		Sleep(1000);
	}

	bt_stop_daemon(bt);
	return 0;
}
Esempio n. 7
0
int
clip_BT_FIELDFILL(ClipMachine * ClipMachineMemory)
{
   const char *__PROC__ = "BT_FIELDFILL";

   BTREE *bt = _clip_fetch_c_item(ClipMachineMemory, _clip_parni(ClipMachineMemory, 1),
				  _C_ITEM_TYPE_BTREE);

   DBWorkArea *wa = cur_area(ClipMachineMemory);

   u4 oldrecno;

   int oldbof, oldeof, fno, er;

   ClipVar v;

   CHECKWA(wa);
   CHECKARG1(1, NUMERIC_type_of_ClipVarType);
   CHECKARG2(2, CHARACTER_type_of_ClipVarType, NUMERIC_type_of_ClipVarType);
   CHECKBTREE(bt);

   if (_clip_parinfo(ClipMachineMemory, 2) == NUMERIC_type_of_ClipVarType)
   {
      fno = _clip_parni(ClipMachineMemory, 2) - 1;
   }
   else
   {
      int l;

      const char *fname = _clip_parcl(ClipMachineMemory, 2, &l);

      fno = _rdd_fieldno(wa->rd, _clip_casehashword(fname, l));
   }

   if (fno < 0 || fno >= wa->rd->nfields)
   {
      er = rdd_err(ClipMachineMemory, EG_ARG, 0, __FILE__, __LINE__, __PROC__, er_nofield);
      goto err;
   }
   oldrecno = wa->rd->recno;
   oldbof = wa->rd->bof;
   oldeof = wa->rd->eof;
   if ((er = wa->rd->vtbl->gotop(ClipMachineMemory, wa->rd, __PROC__)))
      goto err;
   while (!wa->rd->eof)
   {
      if ((er = wa->rd->vtbl->next(ClipMachineMemory, wa->rd, 1, __PROC__)))
	 goto err;
      if (wa->rd->eof)
	 break;
      if ((er = wa->rd->vtbl->getvalue(ClipMachineMemory, wa->rd, fno, &v, __PROC__)))
	 goto err;
      if (bt->type == 'I')
      {
	 u4 n = (u4) v.ClipNumVar_n_of_ClipVar.double_of_ClipNumVar;

	 if (bt_add(bt, NULL, &n))
	 {
	    er = rdd_err(ClipMachineMemory, -1, 0, __FILE__, __LINE__, __PROC__, er_internal);
	    goto err;
	 }
      }
   }
   wa->rd->recno = oldrecno;
   wa->rd->bof = oldbof;
   wa->rd->eof = oldeof;
   if ((er = rdd_childs(ClipMachineMemory, wa->rd, __PROC__)))
      return er;
   return 0;
 err:
   return er;
}