int main() {
    try{
        CQueue<int> cq;
        for(int i = 0; i < 10; i++) 
            cq.appendTail(i);
        for(int i = 0; i < 10; i++) 
            cout << cq.deleteHead() << ' ';
        cout << endl;
        cq.deleteHead();
    } catch(exception &e) {
        cerr << e.what() << endl;
    }


    try{
        CStack<int> cs;
        for(int i = 0; i < 10; i++) 
            cs.push(i);
        for(int i = 0; i < 10; i++) 
            cout << cs.pop() << ' ';
        cout << endl;
        cs.pop();
    } catch(exception &e) {
        cerr << e.what() << endl;
    }
    return 0;
}
int main(int argc, char *argv[])
{
    CQueue<int> q;
    q.appendTail(1);
    q.appendTail(2);

    cout << q.deleteHead() << endl;
    cout << q.deleteHead() << endl;
    q.appendTail(4);
    cout << q.deleteHead() << endl;
    // q.deleteHead();

    CStack<int> s;
    s.push(1);
    s.push(2);
    s.push(3);
    cout << s.top() << endl;
    s.pop();
    cout << s.top() << endl;
    s.pop();
    s.pop();
    // s.pop();

    return 0;
}
示例#3
0
void ShutdownThreading()
{
	if (g_pWorker)
	{
		g_pWorker->Stop(true);
		delete g_pWorker;
		g_pWorker = NULL;
	}

	g_QueueLock->Lock();
	while (!g_ThreadQueue.empty())
	{
		delete g_ThreadQueue.front();
		g_ThreadQueue.pop();
	}
	while (!g_FreeThreads.empty())
	{
		delete g_FreeThreads.front();
		g_FreeThreads.pop();
	}
	g_QueueLock->Unlock();
	g_QueueLock->DestroyThis();

	FreeHandleTable();
}
示例#4
0
int main() {
    CStack<int> s;
    s.push(10);
    s.push(20);
    s.push(30);
    cout << s.size() << endl;
    cout << s.top() << endl;
    s.pop();
    s.pop();
    s.pop();
    cout << s.top() << endl;
}
示例#5
0
void StartFrame()
{
	if (g_pWorker && (g_lasttime < gpGlobals->time))
	{
        g_lasttime = gpGlobals->time + 0.05f;
		g_QueueLock->Lock();
		size_t remaining = g_ThreadQueue.size();
		if (remaining)
		{
			MysqlThread *kmThread;
			do 
			{
				kmThread = g_ThreadQueue.front();
				g_ThreadQueue.pop();
				g_QueueLock->Unlock();
				kmThread->Execute();
				kmThread->Invalidate();
				g_FreeThreads.push(kmThread);
				g_QueueLock->Lock();
			} while (!g_ThreadQueue.empty());
		}

		g_QueueLock->Unlock();
	}

	RETURN_META(MRES_IGNORED);
}
示例#6
0
void OnPluginsUnloading()
{
	if (!g_pWorker)
	{
		return;
	}

	g_pWorker->Stop(false);
	delete g_pWorker;
	g_pWorker = NULL;

	g_QueueLock->Lock();
	size_t remaining = g_ThreadQueue.size();
	if (remaining)
	{
		MysqlThread *kmThread;
		do 
		{
			kmThread = g_ThreadQueue.front();
			g_ThreadQueue.pop();
			g_QueueLock->Unlock();
			kmThread->Execute();
			kmThread->Invalidate();
			g_FreeThreads.push(kmThread);
			g_QueueLock->Lock();
		} while (!g_ThreadQueue.empty());
	}

	g_QueueLock->Unlock();
}
示例#7
0
//Makes a new menu handle (-1 for failure)
//native csdm_makemenu(title[]);
static cell AMX_NATIVE_CALL menu_create(AMX *amx, cell *params)
{
	int len;
	char *title = get_amxstring(amx, params[1], 0, len);
	validate_menu_text(title);
	char *handler = get_amxstring(amx, params[2], 1, len);

	int func = registerSPForwardByName(amx, handler, FP_CELL, FP_CELL, FP_CELL, FP_DONE);
	
	if (func == -1)
	{
		LogError(amx, AMX_ERR_NOTFOUND, "Invalid function \"%s\"", handler);
		return 0;
	}

	Menu *pMenu = new Menu(title, amx, func);

	if (g_MenuFreeStack.empty())
	{
		g_NewMenus.append(pMenu);
		pMenu->thisId = (int)g_NewMenus.length() - 1;
	} else {
		int pos = g_MenuFreeStack.front();
		g_MenuFreeStack.pop();
		g_NewMenus[pos] = pMenu;
		pMenu->thisId = pos;
	}

	return pMenu->thisId;
}
示例#8
0
void OnAmxxDetach()
{
	while (!g_FreeTRs.empty())
	{
		delete g_FreeTRs.front();
		g_FreeTRs.pop();
	}
}
示例#9
0
文件: stack.cpp 项目: Ahan05/iLab
int main()
{
    int temp;
    CStack s;
    s.push(10);
    s.push(15);
    s.add();
    s.pop(temp);
    printf("%d, %d\n",temp, s.GetNumb());
    return 0;
}
示例#10
0
int main()
{
   CStack s;

   s.print();

   s.push(5);
   s.push(10);
   s.push(8);

   s.print();

   cout << "peek at top of stack = " << s.peek() << endl;
   s.print();
   cout << "pop top of stack = " << s.pop() << endl;
   cout << "pop top of stack = " << s.pop() << endl;
   s.print();
   cout << "pop top of stack = " << s.pop() << endl;
   cout << "pop top of stack = " << s.pop() << endl;

   return 0;
}
示例#11
0
文件: fm_tr2.cpp 项目: Javivi/amxmodx
static cell AMX_NATIVE_CALL create_tr2(AMX *amx, cell *params)
{
    TraceResult *tr;
    if (g_FreeTRs.empty())
    {
        tr = new TraceResult;
    } else {
        tr = g_FreeTRs.front();
        g_FreeTRs.pop();
    }
    memset(tr, 0, sizeof(TraceResult));
    return reinterpret_cast<cell>(tr);
}
示例#12
0
void ClearMenus()
{
	for (size_t i = 0; i < g_NewMenus.length(); i++)
	{
		delete g_NewMenus[i];
	}
	
	g_NewMenus.clear();
	while (!g_MenuFreeStack.empty())
	{
		g_MenuFreeStack.pop();
	}
}
示例#13
0
TimerInfo *TimerNatives::CreateTimerInfo()
{
	TimerInfo *pInfo;

	if (m_FreeTimers.empty())
	{
		pInfo = new TimerInfo;
	} else {
		pInfo = m_FreeTimers.front();
		m_FreeTimers.pop();
	}

	return pInfo;
}
示例#14
0
void OnAmxxDetach()
{
	while (!g_FreeTRs.empty())
	{
		delete g_FreeTRs.front();
		g_FreeTRs.pop();
	}

	while (!g_KVDWs.empty())
		delete g_KVDWs.popCopy();

	while (!g_FreeKVDWs.empty())
		delete g_FreeKVDWs.popCopy();
}
示例#15
0
// nothing interesting
int main() {
	CStack stack;

	stack.push(13);
	stack.push(17);
	stack.print();

	int taken_val;
	
	taken_val = stack.pop();
	printf("Taken value: %d\n", taken_val);
	stack.print();
	
	return 0;
}
示例#16
0
int main(int argc, char* argv[])
{
	CStack s;
	int i = 0, t;
	while(i < 10)
		s.push(i++);
	i = 0;
	while(s.pop(t))
	{
		if(s.isEmpty() == true)
			break;
		cout << t << endl;
	}

	return 0;
}
示例#17
0
void LoadExtraPluginsFromDir(const char *initialdir)
{
	CStack<String *> files;
	char path[255];
	BuildPluginFileList(initialdir, files);
	while (!files.empty())
	{
		String *pString = files.front();
		UTIL_Format(path, sizeof(path)-1, "%s/%s", 
			initialdir,
			pString->c_str());
		g_plugins.loadPluginsFromFile(path);
		delete pString;
		files.pop();
	}
}
示例#18
0
void LoadExtraPluginsFromDir(const char *initialdir)
{
	CStack<ke::AString *> files;
	char path[255];
	BuildPluginFileList(initialdir, files);
	while (!files.empty())
	{
		ke::AString *pString = files.front();
		ke::SafeSprintf(path, sizeof(path), "%s/%s",
			initialdir,
			pString->chars());
		g_plugins.loadPluginsFromFile(path);
		delete pString;
		files.pop();
	}
}
示例#19
0
//public QueryHandler(state, Handle:query, error[], errnum, data[], size)
//native SQL_ThreadQuery(Handle:cn_tuple, const handler[], const query[], const data[]="", dataSize=0);
static cell AMX_NATIVE_CALL SQL_ThreadQuery(AMX *amx, cell *params)
{
	if (!g_pWorker)
	{
		MF_LogError(amx, AMX_ERR_NATIVE, "Thread worker was unable to start.");
		return 0;
	}

	SQL_Connection *cn = (SQL_Connection *)GetHandle(params[1], Handle_Connection);
	if (!cn)
	{
		MF_LogError(amx, AMX_ERR_NATIVE, "Invalid info tuple handle: %d", params[1]);
		return 0;
	}

	int len;
	const char *handler = MF_GetAmxString(amx, params[2], 0, &len);
	int fwd = MF_RegisterSPForwardByName(amx, handler, FP_CELL, FP_CELL, FP_STRING, FP_CELL, FP_ARRAY, FP_CELL, FP_CELL, FP_DONE);
	if (fwd < 1)
	{
		MF_LogError(amx, AMX_ERR_NATIVE, "Function not found: %s", handler);
		return 0;
	}

	MysqlThread *kmThread;
	g_QueueLock->Lock();
	if (g_FreeThreads.empty())
	{
		kmThread = new MysqlThread();
	} 
	else 
	{
		kmThread = g_FreeThreads.front();
		g_FreeThreads.pop();
	}
	g_QueueLock->Unlock();

	kmThread->SetInfo(cn->host, cn->user, cn->pass, cn->db, cn->port, cn->max_timeout);
	kmThread->SetForward(fwd);
	kmThread->SetQuery(MF_GetAmxString(amx, params[3], 1, &len));
	kmThread->SetCellData(MF_GetAmxAddr(amx, params[4]), (ucell)params[5]);
	kmThread->SetCharacterSet(cn->charset);

	g_pWorker->MakeThread(kmThread);

	return 1;
}
示例#20
0
void OnAmxxDetach()
{
	ConfigManager->CloseGameConfigFile(CommonConfig);
	ConfigManager->CloseGameConfigFile(GamerulesConfig);

	while (!g_FreeTRs.empty())
	{
		delete g_FreeTRs.front();
		g_FreeTRs.pop();
	}

	while (!g_KVDWs.empty())
		delete g_KVDWs.popCopy();

	while (!g_FreeKVDWs.empty())
		delete g_FreeKVDWs.popCopy();
}
示例#21
0
void StartFrame() {
    if (g_Worker && g_QueueLock) {
        g_QueueLock->Lock();

        CurlThread* thread;
        while (!g_ThreadQueue.empty()) {
            thread = g_ThreadQueue.front();
            g_ThreadQueue.pop();
            g_QueueLock->Unlock();
            thread->Execute();
            delete thread;
            g_QueueLock->Lock();
        }

        g_QueueLock->Unlock();
    }

    RETURN_META(MRES_IGNORED);
}
示例#22
0
void ShutdownThreading() {
    if (g_Worker) {
        g_Worker->SetMaxThreadsPerFrame(8192);
        g_Worker->Stop(true);
    }

    g_QueueLock->Lock();

    CurlThread* thread;
    while (!g_ThreadQueue.empty()) {
        thread = g_ThreadQueue.front();
        g_ThreadQueue.pop();
        g_QueueLock->Unlock();
        delete thread;
        g_QueueLock->Lock();
    }

    g_QueueLock->Unlock();
}
示例#23
0
/*
 *test the template 
 * */
int main(int argc, char **argv)
{
    CStack<CMsg> msgStack;
    int i;
    CMsg msg[10];
    CMsg msgPop;
    for (i = 0; i < 10; i++ )
    {
        msg[i].msgNo = i;
        msg[i].msgInfo = "I am ready!";
        msgStack.push(msg[i]);
    }
    
    while(i-- > 0)
    {
        msgStack.pop(msgPop);
        cout<<"msgStack:"<<"msgNo-"<<msgPop.msgNo<<" "<<"msgInfo '"<<msgPop.msgInfo<<"'"<<endl;
    }
    return 1;
}
示例#24
0
文件: main.cpp 项目: 88er/tutorials
int main()
{
	CStack stack; // Default constructor is called.  Our stack will be 
				 // kDefaultStackSize (10) elements large.

	// Let's push some integers (0, 1, 2, 3, 4) on the stack
	for(int i = 0; i < 5; i++)
		stack.push(i);

	// Now lets pop all the elements off of the stack printing each one 
	// as they come off
	while(stack.isEmpty() == false)
	{
		cout << "Top = " << stack.getTop() << endl;
		stack.pop();
	}
	
	return EXIT_SUCCESS;

} // end of main()
示例#25
0
// Check if the parenthesis in an expression are balanced
// PRE: str points to the expression to check
// POST: returns 1 if balanced, otherwise returns 0
bool CheckParens(const char* str) {
	CStack stack;		// Stack used for balance checking
	char temp[2];		// String used to push the parens on the stack

	// Loop through pushing lefts and popping/comparing rights
	temp[1]=0;
	for(int x=0;str[x];x++) {
		*temp=str[x];
		if(isLeftParen(*temp)) {
			stack.push(temp);
		} else if(isRightParen(*temp)) {
			if(stack.empty() || getRightParen(stack.pop()[0])!=*temp)
				return 0;
		}
	}

	// If the stack isn't empty, parens aren't balanced
	if(stack.empty())
		return 1;
	else
		return 0;
}
示例#26
0
void OnPluginsUnloading() {
    if (!g_Worker) {
        return;
    }

    g_Worker->SetMaxThreadsPerFrame(8192);
    g_Worker->Stop(false);
    delete g_Worker;
    g_Worker = NULL;

    g_QueueLock->Lock();

    CurlThread* thread;
    while (!g_ThreadQueue.empty()) {
        thread = g_ThreadQueue.front();
        g_ThreadQueue.pop();
        g_QueueLock->Unlock();
        delete thread;
        g_QueueLock->Lock();
    }

    g_QueueLock->Unlock();
}
示例#27
0
MsgListenerWrapper *UsrMessageNatives::CreateListener(IPluginContext *pCtx)
{
	MsgWrapperList *pList;
	MsgListenerWrapper *pListener;
	IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());

	if (m_FreeListeners.empty())
	{
		pListener = new MsgListenerWrapper;
	} else {
		pListener = m_FreeListeners.front();
		m_FreeListeners.pop();
	}

	if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
	{
		pList = new List<MsgListenerWrapper *>;
		pl->SetProperty("MsgListeners", pList);
	}

	pList->push_back(pListener);

	return pListener;
}
示例#28
0
// Convert an infix expression to postfix
// PRE: expr is the infix expression to convert
//      output is a string for the output
// POST: output is the postfix expression, returns 1 on
//       success or 0 on error
bool SolveInfix(const char *input,char* output) {
	char op[2];					// "string" for the operator stack
	char expr[MAX_LENGTH+1];	// Modifible input string
	int length;					// The current length of the output string [+1]
	CStack opStack;				// Operation stack
	int x;						// Index variable for reading input
	bool foundError;			// 1 if error found, otherwise 0

	// 
	memcpy(expr,input,MAX_LENGTH*sizeof(char));
	InsertZeros(expr);

	// Initialize some variables
	op[1]=0;
	length=0;
	foundError=0;
	output[0]=0;

	// Read each character of the expression
	for(x=0;expr[x];x++) {
		*op=expr[x];

		// If we have a left paren, push it
		if(isLeftParen(*op)) {
			opStack.push(op);

		// If we have a number, push it
		} else if(isNumber(*op)) {
			do {
				output[length++]=expr[x++];
			} while(isNumber(expr[x]));
			output[length++]=' ';
			output[length]=0;
			--x;

		// If we have an operator, pop lower precedence operators and push it
		} else if(isOperator(*op)) {
			while(!opStack.empty()
				&& !isLeftParen(opStack.top()[0])
				&& isOperator(opStack.top()[0])<=isOperator(*op)) {
				if(!isOperator(opStack.top()[0])) foundError=1;
				output[length++]=opStack.pop()[0];
				output[length]=0;
			}
			opStack.push(op);

		// If we have a right paren, pop operators between the parens
		} else if(isRightParen(*op)) {
			for(;;) {
				if(opStack.empty()) break;
				*op=opStack.pop()[0];
				if(!*op || isLeftParen(*op)) break;
				if(!isOperator(*op)) {
					foundError=1;
					break;
				}
				output[length++]=*op;
			}
			output[length]=0;

		// If this happens, we have a syntax error (unless whitespace)
		} else {
			switch(*op) {
			case ' ':
			case '\t':
			case '\n':
				break;
			default:
				foundError=1;
				break;
			}
		}

		// No need to continue if we have errors
		if(foundError) break;
	}

	// Pop any remaining operations off the stack
	while(!opStack.empty()) {
		if(!isOperator(opStack.top()[0])) foundError=1;
		output[length++]=opStack.pop()[0];
		output[length]=0;
	}

	return !foundError;
}
示例#29
0
// Solve a postfix expression
// PRE: expr is the postfix expression to solve
// POST: answer is the answer, returns 1 if
//       successful, otherwise returns 0
bool SolvePostfix(const char* expr,double &answer) {
	int x,y;					// Index variables
	double right,left;			// Right and left parts of the value
	char str[MAX_LENGTH+1];		// String for number to string conversion
	char ch;					// Temporary char to hold expr[x] etc
	CStack stack;				// The stack to hold the values

	// Loop through the postfix expression
	for(x=0;x<signed(strlen(expr));x++) {
		ch=expr[x];

		// If we have an operator, set up left/right
		if(isOperator(ch)) {
			right=atof(stack.pop());
			if(stack.empty()) {
				break;
			} else {
				left=atof(stack.pop());
			}
		}

		// Do an operation or push a number
		switch(ch) {
		case '+':
			sprintf(str,"%lf",left+right);
			stack.push(str);
			break;
		case '-':
			sprintf(str,"%lf",left-right);
			stack.push(str);
			break;
		case '*':
			sprintf(str,"%lf",left*right);
			stack.push(str);
			break;
		case '/':
			if(!right)
				break;
			sprintf(str,"%lf",left/right);
			stack.push(str);
			break;
		case '^':
			sprintf(str,"%lf",pow(left,right));
			stack.push(str);
			break;
		case ' ':		// Skip white space and various delimiters
		case ',':
		case '\t':
		case '\n':
			break;
		default:	// Number (hopefully)
			for(y=0;y<=MAX_LENGTH && expr[x];y++,x++) {
				if(!isNumber(expr[x])) {
					--x;		// Revisit this character
					break;
				}
				str[y]=expr[x];
			}
			if(!y) {	// Bad input
				return 0;
			}
			str[y]=0;
			stack.push(str);
			break;
		}
	}

	// If the stack is empty, no answer!
	if(stack.empty())
		return 0;
	else
		answer=atof(stack.pop());

	// If the stack is not empty, too few operations
	if(!stack.empty())
		return 0;

	return 1;
}
示例#30
0
void CPluginMngr::CacheAndLoadModules(const char *plugin)
{
	size_t progsize;
	char *prog = ReadIntoOrFromCache(plugin, progsize);

	if (!prog)
		return;

	AMX_HEADER hdr;
	memcpy(&hdr, prog, sizeof(AMX_HEADER));

	uint16_t magic = hdr.magic;
	amx_Align16(&magic);

	if (magic != AMX_MAGIC)
	{
		return;
	}

	if (hdr.file_version < MIN_FILE_VERSION ||
		hdr.file_version > CUR_FILE_VERSION)
	{
		return;
	}
	if ((hdr.defsize != sizeof(AMX_FUNCSTUB)) &&
		(hdr.defsize != sizeof(AMX_FUNCSTUBNT)))
	{
		return;
	}

	amx_Align32((uint32_t*)&hdr.nametable);
	uint16_t *namelength=(uint16_t*)((unsigned char*)prog + (unsigned)hdr.nametable);
	amx_Align16(namelength);
	if (*namelength>sNAMEMAX)
	{
		return;
	}

	if (hdr.stp <= 0)
	{
		return;
	}

	AMX amx;
	memset(&amx, 0, sizeof(AMX));
	amx.base = (unsigned char *)prog;

	int num;
	char name[sNAMEMAX+1];

	num = amx_GetLibraries(&amx);
	for (int i=0; i<num; i++)
	{
		amx_GetLibrary(&amx, i, name, sNAMEMAX);
		if (stricmp(name, "Float")==0)
			continue;
		//awful backwards compat hack
		if (stricmp(name, "socket")==0)
			strcpy(name, "sockets");
		//we don't want to report failed modules here...
		LoadModule(name, PT_ANYTIME, true, true);
	}

	cell tag_id;
	amx_NumTags(&amx, &num);

	ke::Vector<LibDecoder *> expects;
	ke::Vector<LibDecoder *> defaults;
	CStack<LibDecoder *> delstack;
	for (int i=0; i<num; i++)
	{
		amx_GetTag(&amx, i, name, &tag_id);
		if (name[0] == '?')
		{
			LibDecoder *dc = new LibDecoder;
			delstack.push(dc);
			if (DecodeLibCmdString(name, dc))
			{
				if (dc->cmd == LibCmd_ForceLib)
				{
					RunLibCommand(dc);
				} else if ( (dc->cmd == LibCmd_ExpectClass) ||
							(dc->cmd == LibCmd_ExpectLib) )
				{
					expects.append(dc);
				} else if (dc->cmd == LibCmd_DefaultLib) {
					defaults.append(dc);
				}
			}
		}
	}

	for (size_t i=0; i<expects.length(); i++)
	{
		RunLibCommand(expects[i]);
	}
	for (size_t i=0; i<defaults.length(); i++)
	{
		RunLibCommand(defaults[i]);
	}

	expects.clear();
	defaults.clear();

	while (!delstack.empty())
	{
		delete delstack.front();
		delstack.pop();
	}

	return;
}