Пример #1
0
DWORD TTimerUnregisterCallBack( DWORD dwHandle )
{
    int i;
    struct STTimer * pTTimer;
    
    for ( pTTimer = stCBTimer, i = 0; i < GET_ARRAY_LEN( stCBTimer ); i++, pTTimer++ )
    {
        if ( pTTimer->dwHandle == dwHandle )
        {
            pTTimer->dwHandle       = TIMER_ENTRY_NULL;
            pTTimer->iCount        = 0;
            pTTimer->iReloadValue  = 0;
            pTTimer->type           = TimerOneShot;
            pTTimer->lpParam        = NULL;
            pTTimer->callback_func  = NULL;
            pTTimer->bStarted       = 0;
            
            break;
        }
    }
    
    if ( i == GET_ARRAY_LEN( stCBTimer ) )
    {
        return EFAULT;
    }
    
    bInstanceCounter--;
    
    return 0;
}
Пример #2
0
void T01_01_LinuxEventTest(void)
{
	int retVal;


	retVal = LibIPC_Event_BatchCreate(gEventAry, GET_ARRAY_LEN(gEventAry));
	ASSERT_IF(retVal);


    ThreadEntryFunc funcAry[] = {
        T01_01_TestThread00,
        T01_01_TestThread01,
        T01_01_TestThread02,
        T01_01_TestThread03,
    };
    LibThreadMgr_BatchCreate(gThreadAry, funcAry, GET_ARRAY_LEN(gThreadAry));

	LibOs_SleepMiliSeconds(10); // For linux, prevent SetEvent() is running before WaitEvent() !!
	LibIPC_Event_Set(gEventAry[0]);

	LibTime_StartMicroSecondClock();
	retVal = LibThreadMgr_BatchWait(gThreadAry, GET_ARRAY_LEN(gThreadAry));
	ASSERT_IF(retVal);
	LibTime_StopMicroSecondClock_ShowResult();
	PRINT_NEXT_LINE;

	retVal = LibIPC_Event_BatchDestroy(gEventAry, GET_ARRAY_LEN(gEventAry));
	ASSERT_IF(retVal);

	retVal = LibThreadMgr_BatchDestroy(gThreadAry, GET_ARRAY_LEN(gThreadAry));
	ASSERT_IF(retVal);

	printf("%s() ... [Success]\n", __func__);
}
Пример #3
0
int main()
{
	srand((unsigned)time(NULL));

	int playerLen;
	int weaponsLen;
	GET_ARRAY_LEN(s_playerType, playerLen);
	GET_ARRAY_LEN(s_weapons, weaponsLen);

	// 假设,游戏中有十位玩家
	for (int i = 0; i < 10; i++) {
		// 获取随机玩家和武器
		int typeIndex = rand() % playerLen;
		int weaponIndex = rand() % weaponsLen;
		std::string type = s_playerType[typeIndex];
		std::string weapon = s_weapons[weaponIndex];

		// 获取玩家
		IPlayer *p = PlayerFactory::getPlayer(type);

		// 从武器库中随机分配武器
		p->assignWeapon(weapon);

		// 派玩家去执行任务
		p->mission();
	}

	getchar();

	return 0;
}
Пример #4
0
//测试
void addTwoNumbersMain()
{
    struct ListNode l1[]={ {7, NULL}, {5, NULL}, {1, NULL}, {2, NULL}, {3, NULL} };
    struct ListNode l2[]={ {1, NULL}, {0, NULL}, {9, NULL}, {7, NULL}, {6, NULL}, {9, NULL}, {4, NULL} };
    int len1;
    int len2;
    GET_ARRAY_LEN(l1, len1);
    GET_ARRAY_LEN(l2, len2);

    int i = 0;
    for(i = 0; i<len1-1; i++)
    {
        l1[i].next = &l1[i+1];
    }
    for(i = 0; i<len2-1; i++)
    {
        l2[i].next = &l2[i+1];
    }

    struct ListNode *result = addTwoNumbers(l1, l2);

    while(result)
    {
        printf("%d", result->val);
        result = result->next;
    }
}
Пример #5
0
DWORD TTimerSetTime( DWORD dwHandle, DWORD dwNewDelay )
{
    int i;
    int count;
    struct STTimer* pTTimer;
    
    if ( dwNewDelay < dwTimerTimeBase )
    {
      dwNewDelay = dwTimerTimeBase;
    }
    
    count = (DWORD)((dwNewDelay / (float)dwTimerTimeBase) + 0.5f);    
    
    
    for ( i = 0, pTTimer = stCBTimer; i < GET_ARRAY_LEN( stCBTimer ); i++, pTTimer++ )
    {
        if ( pTTimer->dwHandle == dwHandle )
        {
            pTTimer->bStarted  = 0;
            pTTimer->iReloadValue = count;
            pTTimer->iCount = pTTimer->iReloadValue;
            break;
        }
    }
    
    if ( i == GET_ARRAY_LEN( stCBTimer ) )
    {
        return (DWORD)-1;
    }
    
    return 0;    
}
Пример #6
0
DWORD TTimerRegisterCallBack( DWORD dwDelay, TimerType type, callbacktimer_func callback_func, void* lpParam, DWORD* cbHandle )
{
    int i;
    int count;
    struct STTimer * pTTimer;
    
    if ( !bInit )
    {
      /* the library isn't initialized  */
      return EPERM;
    }
    
    if ( callback_func == NULL )
    {
      /* invalid parameter */
      return EPERM;
    }
    
    if ( bInstanceCounter++ > TIMER_ENTRY_CB )
    {
      /* exceed the timers limit. See at the TIMER_ENTRY_CB macro  */
      return ENOMEM;
    }
    
    if ( dwDelay < dwTimerTimeBase )
    {
      dwDelay = dwTimerTimeBase;
    }
    
    count = (DWORD)((dwDelay / (float)dwTimerTimeBase) + 0.5f);
    
    for ( pTTimer = stCBTimer, i = 0; i < GET_ARRAY_LEN( stCBTimer ); i++, pTTimer++ )
    {
        if ( pTTimer->dwHandle == TIMER_ENTRY_NULL )
        {
            pTTimer->dwHandle       = (TIMER_HANDLE_INITIAL_VALUE) + (++dwHandleCount);
            pTTimer->iReloadValue   = count;            
            pTTimer->iCount         = pTTimer->iReloadValue;
            pTTimer->type           = (TimerType)type;
            pTTimer->lpParam        = lpParam;
            pTTimer->callback_func  = callback_func;
            pTTimer->bStarted       = 0;
            
            break;
        }
    }
    
    if ( i == GET_ARRAY_LEN( stCBTimer ) )
    {
        return EFAULT;
    }
    
    if ( cbHandle != NULL )
    {
        *cbHandle = pTTimer->dwHandle;
    }
    
    return 0;
}
Пример #7
0
int main(int argc, char *argv[]){
	int data[] = {8,5,3,1,10,2,7,9,4,6};
	int len;
	GET_ARRAY_LEN(data, len);

	initList();
	struct node add_node;
	for(int i = 0; i < len; i++){
		add_node.num = data[i];
		addNodeToHead(&add_node);
		// addNodeToTail(&add_node);		/* 插入到链表尾部 */
		// addNodeAscend(&add_node);
	}
	print(head);

	int num = getNode(3);
	printf("%d", num);
	return 1;

	//删除一个链节点
	add_node.num = 5;
	deleteNode(&add_node);

	//删除一个头节点
	// deleteNodeFromHead();

	print(head);
}
Пример #8
0
DWORD TTimerStop( DWORD dwHandle )
{
    int i;
    struct STTimer* pTTimer;
    
    for ( i = 0, pTTimer = stCBTimer; i < GET_ARRAY_LEN( stCBTimer ); i++, pTTimer++ )
    {
        if ( pTTimer->dwHandle == dwHandle )
        {
            pTTimer->bStarted  = 0;
            break;
        }
    }
    
    if ( i == GET_ARRAY_LEN( stCBTimer ) )
    {
        return (DWORD)-1;
    }
    
    return 0;
}
Пример #9
0
int main()
{
	int j,len;
	GET_ARRAY_LEN(arr,len);
	quicksort(arr,0,len-1);
	for(j=0;j<len;j++)
	{
		printf("%d ",arr[j]);
	}

	printf("\n length: %d\n",len);
}
Пример #10
0
void Timer_ISR_Handler( void )
{
    int i;
    struct STTimer * pTTimer;
    DWORD dwExecuteCBFlag = 0;
    DWORD dwCBRet = 0;
    
    for ( i = 0, pTTimer = stCBTimer; i < GET_ARRAY_LEN( stCBTimer ); i++, pTTimer++ )
    {
        if ( pTTimer->dwHandle != TIMER_ENTRY_NULL )
        {
            if ( pTTimer->bStarted )
            {
                pTTimer->iCount--;
                if ( !dwExecuteCBFlag )
                {
                    if ( pTTimer->iCount <= 0 )
                    {
                        if ( pTTimer->callback_func != NULL )
                        {
                            dwExecuteCBFlag = 1;
                            
                            dwCBRet = pTTimer->callback_func( pTTimer->lpParam );	// execute the callback code
                            
                            if ( dwCBRet )
                            {
                                // an error ocurred in the call back execution
                                TTimerUnregisterCallBack( pTTimer->dwHandle );
                                continue;
                            }
                            
                            if ( pTTimer->type == TimerPeriodic )
                            {
                                pTTimer->iCount = pTTimer->iReloadValue;
                            }
                            else
                            {
                                pTTimer->bStarted = 0;
                            }
                        }
                    }
                }
            }
        }
    }
    
    TimerIntClear( TIMER_BASE, TIMER_TIMA_TIMEOUT );
    
    return;
}
Пример #11
0
int main(){
	int data[] = {8,5,3,1,9,2,7,4,6};
	int len;
	GET_ARRAY_LEN(data, len);

	// 打印原始需排序成员
	printf("original : ");
	printIntArray(data, len);

	// 冒泡排序
	SelectSort(data, len);

	// 打印最终结果
	printf("result : ");
	printIntArray(data, len);
}
Пример #12
0
int main(){
	int data[] = {8,5,3,1,10,2,7,9,4,6};
	int len;
	GET_ARRAY_LEN(data, len);
	int *tmpData = (int *)malloc(sizeof(int) * len);	/* 归并排序中用到的数组,用于保存排序好之后的成员,其长度应跟data一样大小 */

	// 打印原始需排序成员
	printf("original : ");
	printIntArray(tmpData, len);

	// 快速排序
	MyMergeSort(data, tmpData, 0, len - 1);

	// 打印最终结果
	printf("result : ");
	printIntArray(tmpData, len);
}
Пример #13
0
void TTimerCfgTimeOut( DWORD dwTimeMicro )
{
    DWORD dwCountValue;
    
    if ( dwTimeMicro < 10 )
    {
        dwTimeMicro = 10;
    }
        
    dwCountValue = (DWORD)((dwTimeMicro * TIMER_TIMEBASE_us)+0.5);

    if ( !bInit )
    {
        DWORD dwCounter = 0;
        struct STTimer * pTTimer = &stCBTimer[0];
        
        for( ; dwCounter < GET_ARRAY_LEN( stCBTimer ); dwCounter++, pTTimer++ )
        {
            pTTimer->dwHandle       = TIMER_ENTRY_NULL;
            pTTimer->iCount         = 0;
            pTTimer->iReloadValue   = 0;
            pTTimer->type           = TimerOneShot;
            pTTimer->lpParam        = NULL;
            pTTimer->callback_func  = NULL;
            pTTimer->bStarted       = 0;
        }    


	/* hardware timer initialization */
        SysCtlPeripheralEnable( TIMER_SYSCTL );
        TimerConfigure(TIMER_BASE, TIMER_CFG_PERIODIC);
        TimerLoadSet( TIMER_BASE, TIMER_A, dwCountValue );
        TimerIntRegister( TIMER_BASE, TIMER_A, Timer_ISR_Handler );
        TimerIntClear( TIMER_BASE, TIMER_TIMA_TIMEOUT );
        TimerIntEnable( TIMER_BASE, TIMER_TIMA_TIMEOUT );
        TimerEnable(TIMER_BASE, TIMER_A);
        
        bInit = 1;
    }
   
    dwTimerTimeBase = dwTimeMicro;
    
    return;
}
int main(int argc, char *argv[]){
	int data[] = {8,5,3,1,10,2,7,9,4,6};
	int len;
	GET_ARRAY_LEN(data, len);

	initList();
	struct node add_node;
	for(int i = 0; i < len; i++){
		add_node.num = data[i];
		// addNodeToTheEnd(&add_node);		/* 插入到链表尾部 */
		addNodeAscend(&add_node);		/* 以降序插入链表 */
	}
	print(head);

	//删除一个链节点
	add_node.num = 5;
	deleteNode(&add_node);

	print(head);
}
Пример #15
0
int main(int argc, char * argv[]){
	// int data[] = {20,8,5,3,1,10,2,7,9,4,6,18,11,12,15};
	// int data[] = {20,8,5,3,1,10,2,7,9,4,6,18,11};
	int data[] = {20,8,5,3,1,10,2,7,9,4,6,18};
	int len;
	GET_ARRAY_LEN(data, len);

	init_btree();

	//插入节点
	for (int i = 0; i < len; i++)
	{
		insert(&bt_root, data[i]);
	}

	//测试查找函数
	// int pos = -1;
	// bnode * want = find(bt_root, 9, &pos);
	// if(pos != -1)
	// {
	//  printf("%d\n", want -> parent -> keys[0]);
	// 	printf("pos :%d\n", pos);
	// 	printf("p_pos :%d\n", want -> p_pos);
	// }
	// else
	// {
	// 	printf("%s\n","没找到元素");
	// }

	//层级打印B树
	printf("层级打印:\n");
	hierarchy_traversal_recurse(bt_root, 1);

	printf("普通打印:\n");
	print_btree(bt_root);


	//测试删除数据 示例 1
	// remove_key( &bt_root, 7 );
	// remove_key( &bt_root, 5 );
	// remove_key( &bt_root, 4 );
	// remove_key( &bt_root, 1 );
	// remove_key( &bt_root, 2 );
	// remove_key( &bt_root, 3 );
	// remove_key( &bt_root, 18 );


	//测试删除数据 示例 2
	// remove_key( &bt_root, 20 );
	// remove_key( &bt_root, 7 );


	//测试find_max函数
	// int pos,key = -1;
	// bnode * want = find_max(bt_root -> children[2], &pos, &key);
	// if(pos != -1)
	// {
	// 	printf("%d,%d\n",pos,want -> keys[pos]);
	// }
	// else
	// {
	// 	printf("%s\n","没找到元素");
	// }

	printf("层级打印:\n");
	hierarchy_traversal_recurse(bt_root, 1);

	printf("普通打印:\n");
	print_btree(bt_root);

	return 1;
}