// Listing 4 code/ch17 int processRecord (ALLOCATOR *shmem_allocator) { ACE_GUARD_RETURN (ACE_Process_Mutex, ace_mon, coordMutex, -1); QUEUE* queue = squeue (shmem_allocator); if (queue == 0) { delete shmem_allocator; ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("Could not obtain queue")), -1); } if (queue->is_empty ()) // Check for anything to process. return 0; Record record; if (queue->dequeue_head (record, shmem_allocator) == -1) { ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("dequeue_head\n")), -1); } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Processing record|name: %C") ACE_TEXT ("|Record id1:%d|Record id2:%d\n"), record.name (), record.id1 (), record.id2 ())); if (record.id1 () == -1) queue->enqueue_tail (record, shmem_allocator); return record.id1 (); }
int main(void) { QUEUE q; char v; q.insert('a'); q.insert('b'); }
template <typename ELEMENT, typename QUEUE> static void dynamic_push_3(QUEUE & i_queue) { auto const type = QUEUE::runtime_type::template make<ELEMENT>(); i_queue.dyn_push(type); ELEMENT copy_source; i_queue.dyn_push_copy(type, ©_source); ELEMENT move_source; i_queue.dyn_push_move(type, &move_source); }
int main(){ int a = 0; /* init(); for (a = 0; a < MAX_SIZE; a++) push(a+1); for (a = 0; a < MAX_SIZE ; a++) pop(); */ QUEUE q; for (a = 0; a < MAX_SIZE; a++) q.push(a + 1); for (a = 0; a < MAX_SIZE; a++) q.pop(); return 0; }
/** Basic tests LfHeterQueue with a non-polymorphic base */ template <typename QUEUE> static void lf_heterogeneous_queue_basic_void_tests() { { QUEUE queue; DENSITY_TEST_ASSERT(queue.empty()); } { QUEUE queue; queue.clear(); queue.push(1); DENSITY_TEST_ASSERT(!queue.empty()); queue.clear(); DENSITY_TEST_ASSERT(queue.empty()); queue.clear(); } }
void BFS(NodeMap* nMap, string startNode, string goalNode) { QUEUE *q = new QUEUE(); NodeList* nlist = nMap->find(startNode)->second; for (NodeList::iterator it = nlist->begin(); it != nlist->end(); it++) { (*it)->path->push_back(startNode); (*it)->visitedNodes->insert(pair<string, NodeList*> (startNode, nlist)); q->push(*it); } cout << startNode << endl; while(!q->empty()) { Node* firstNode = q->front(); q->pop(); cout << firstNode->name << endl; if (firstNode->name == goalNode) { cout << "END" << endl; cout << "Final path : "; for(list<string>::iterator sit = firstNode->path->begin(); sit != firstNode->path->end(); sit++) { cout << (*sit) << endl; cout << firstNode->name << endl; } break; } if (firstNode->visitedNodes->find(firstNode->name) != firstNode->visitedNodes->end()) { continue; } nlist = nMap->find(firstNode->name)->second; for (NodeList::iterator it = nlist->begin(); it != nlist->end(); it++) { (*it)->path = new list<string>(firstNode->path->begin(), firstNode->path->end()); (*it)->visitedNodes = new NodeMap(firstNode->visitedNodes->begin(), firstNode->visitedNodes->end()); (*it)->path->push_back(firstNode->name); (*it)->visitedNodes->insert(pair<string, NodeList*> (firstNode->name, NULL)); q->push(*it); } } }
int main() { // Declaration of variabless string sentence, cont; char charsent[50], c1, c2; // Creation of STACK and QUEUE STACK<char> S; QUEUE<char> Q; S.CreateStack(); Q.CreateQueue(); // While-loop for continue while(true) { cout << endl; cout << "Enter a sentence: "; getline(cin, sentence); strcpy(charsent, sentence.c_str()); // Pushing user input into STACK and QUEUE for (int i = 0; i < sentence.size(); i++) { c1 = charsent[i]; // Checking for alphanumeric if (isalnum(c1)) { S.Push(c1); Q.Push(c1); } else { // do nothing } } // STACK and QUEUE comparision while (!S.EmptyStack()) { c1 = S.Pop(); c2 = Q.Pop(); c1 = toupper(c1); c2 = toupper(c2); if (c1 != c2) { break; } } // If STACK and QUEUE was emptied with no break, Palindrome found if (S.EmptyStack() && Q.EmptyQueue()) { cout << "[" << sentence << "] is a Palindrome!" << endl; } else { cout << "[" << sentence << "] is NOT Palindrome." << endl; } // While-loop continue check cout << endl << "CONTINUE (y/n)? "; cin >> cont; if (cont != "y") { break; } else { // Clearing the STACK and QUEUE for new user input while(!S.EmptyStack() || !Q.EmptyQueue()) { S.Pop(); Q.Pop(); } } cin.ignore(); } return 0; }
static int chained_block_test (void) { QUEUE q; const char * s = "123456789"; // Will be length 10 when copied to block const size_t slen = 10; const size_t num_blks = 10; ACE_Message_Block b[num_blks]; size_t i; int status = 0; for (i = 0; i < num_blks; ++i) { b[i].init (slen); b[i].copy (s); } // Test enqueueing single and chained blocks and be sure they end up with // the proper enqueued block count and sizes. Then be sure they are dequeued // in the proper order. b[0].next (&b[1]); b[1].next (&b[2]); // b[3] and b[4] are unchained. b[5].next (&b[6]); b[6].next (&b[7]); b[7].next (&b[8]); // b[9] is unchained q.enqueue_tail (&b[3]); q.enqueue_tail (&b[4]); int num = q.enqueue_head (&b[0]); if (num != 5) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Chained enqueue expected 5; has %d\n"), num)); status = -1; } num = q.enqueue_tail (&b[5]); if (num != 9) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Chained enqueue expected 9; has %d\n"), num)); status = -1; } num = q.enqueue_tail (&b[9]); if (num != 10) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Chained enqueue expected 10; has %d\n"), num)); status = -1; } size_t msgs, bytes; msgs = q.message_count (); bytes = q.message_bytes (); if (msgs != 10 || bytes != 100) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Chained enqueue totals: %d msgs, %d bytes; ") ACE_TEXT ("should be 10 msgs, 100 bytes\n"), (int)msgs, (int)bytes)); status = -1; } // Now see if we can dequeue them, checking the order. ACE_Time_Value nowait (ACE_OS::gettimeofday ()); ACE_Message_Block *bp; int qstat; for (i = 0; i < num_blks; ++i) { qstat = q.dequeue_head (bp, &nowait); if (qstat == -1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Checking chained blocks, pass %d: %p\n"), (int)i, ACE_TEXT ("dequeue_head"))); status = -1; } else if (bp != &b[i]) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Checking chained blocks, pass %d: ") ACE_TEXT ("block out of order\n"), (int)i)); status = -1; } } if (status == 0) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Chained block test OK\n"))); return status; }
static int iterator_test (void) { const int ITERATIONS = 5; ACE_TCHAR buffer[ITERATIONS][BUFSIZ]; // Use queue size from of 32 Kb (more if using wide-char), instead of the // default of 16 Kb (defined by ACE_Message_Queue_Base::DEFAULT_HWM), // so that the test runs on machines with 8Kb pagesizes. // QUEUE queue (32 * 1024 * sizeof (ACE_TCHAR)); QUEUE queue (sizeof(buffer)); int i; for (i = 0; i < ITERATIONS; i++) { ACE_OS::sprintf (buffer[i], ACE_TEXT ("%d"), i + 1); ACE_Message_Block *entry = 0; ACE_NEW_RETURN (entry, ACE_Message_Block ((char *) buffer[i], sizeof buffer[i]), -1); if (queue.is_full ()) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("QUEUE:: the message queue is full on iteration %u!\n"), i + 1), -1); if (queue.enqueue (entry) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("QUEUE::enqueue\n")), -1); } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nForward Iterations\n"))); { ITERATOR iterator (queue); for (ACE_Message_Block *entry = 0; iterator.next (entry) != 0; iterator.advance ()) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%s\n"), entry->base ())); } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nReverse Iterations\n"))); { REVERSE_ITERATOR iterator (queue); for (ACE_Message_Block *entry = 0; iterator.next (entry) != 0; iterator.advance ()) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%s\n"), entry->base ())); } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nForward Iterations\n"))); { QUEUE::ITERATOR iterator (queue); for (ACE_Message_Block *entry = 0; iterator.next (entry) != 0; iterator.advance ()) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%s\n"), entry->base ())); } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nReverse Iterations\n"))); { QUEUE::REVERSE_ITERATOR iterator (queue); for (ACE_Message_Block *entry = 0; iterator.next (entry) != 0; iterator.advance ()) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%s\n"), entry->base ())); } return 0; }
static int single_thread_performance_test (void) { const char test_message[] = "ACE_Message_Queue_Ex Test Message"; const ACE_TCHAR *message = ACE_TEXT ("ACE_Message_Queue_Ex<ACE_NULL_SYNCH>, single thread"); // Create a message queue. QUEUE *msgq = 0; ACE_NEW_RETURN (msgq, QUEUE, -1); // Create the messages. Allocate off the heap in case messages is // large relative to the amount of stack space available. User_Class **send_block = 0; ACE_NEW_RETURN (send_block, User_Class *[max_messages], -1); int i; for (i = 0; i < max_messages; ++i) ACE_NEW_RETURN (send_block[i], User_Class (test_message), -1); User_Class **receive_block_p = 0; ACE_NEW_RETURN (receive_block_p, User_Class *[max_messages], -1); timer->start (); // Send/receive the messages. for (i = 0; i < max_messages; ++i) { if (msgq->enqueue_tail (send_block[i]) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("enqueue")), -1); if (msgq->dequeue_head (receive_block_p[i]) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("dequeue_head")), -1); } timer->stop (); ACE_Time_Value tv; timer->elapsed_time (tv); ACE_DEBUG ((LM_INFO, ACE_TEXT ("%s: %u messages took %u msec (%f msec/message)\n"), message, max_messages, tv.msec (), (double) tv.msec () / max_messages)); timer->reset (); delete [] receive_block_p; for (i = 0; i < max_messages; ++i) delete send_block[i]; delete [] send_block; delete msgq; return 0; }