Example #1
0
void EnhancedLinkedList<T>::remove_first(const T& key) {
    if (head != NULL) {
        if (head->getData() == key) {
            pop_front();
            return;
        }

        Node<T> *current = head;
        Node<T> *previous = head;

        while (current != NULL) {
            if (current->getData() == key) {
                if (current->getData() == key) {
                    pop_front();
                    return;
                } else {
                    previous->getNext() = current->getNext();
                    delete current;
                    count--;
                    current = previous->getNext();
                    return;
                }
            } else {
                previous = current;
                current = current->getNext();
            }
        }

        if (tail->getData() == key) {
            pop_back();
        }
    }
}
Example #2
0
void moveDownward(Queue * q) {

    Node * head;
    GLfloat timer;
    if (q != NULL) {
        head = q->head;
        while (head != NULL) {
            if (head->circle.movable == TRUE) {
                head->circle.y += head->circle.yVector * SPEED;
            }
            head = head->next;
        }

        head = q->head;
        timer = glfwGetTime();
        if (head != NULL) {
            if (head->circle.y < 0 - RADIUS) {
                pop_front(q);
            } else if (head->circle.fadeAway == TRUE
                    && head->circle.invisibleStartTime > 0
                    && timer - head->circle.invisibleStartTime > ALPHA_TIME) {
                pop_front(q);
            }
        }
    }
}
// Prepares items for dropping by reordering them so that the drop
// cost is minimal and "dependent" items get taken off first.
// Implements the "backpack" logic.
std::list<act_item> reorder_for_dropping( const player &p, const drop_indexes &drop )
{
    auto res  = convert_to_items( p, drop, -1, -1 );
    auto inv  = convert_to_items( p, drop, 0, INT_MAX );
    auto worn = convert_to_items( p, drop, INT_MIN, -2 );

    // Sort inventory items by volume in ascending order
    inv.sort( []( const act_item & first, const act_item & second ) {
        return first.it->volume() < second.it->volume();
    } );
    // Add missing dependent worn items (if any).
    for( const auto &wait : worn ) {
        for( const auto dit : p.get_dependent_worn_items( *wait.it ) ) {
            const auto iter = std::find_if( worn.begin(), worn.end(),
            [ dit ]( const act_item & ait ) {
                return ait.it == dit;
            } );

            if( iter == worn.end() ) {
                worn.emplace_front( dit, dit->count_by_charges() ? dit->charges : 1,
                                    100 ); // @todo: Use a calculated cost
            }
        }
    }
    // Sort worn items by storage in descending order, but dependent items always go first.
    worn.sort( []( const act_item & first, const act_item & second ) {
        return first.it->is_worn_only_with( *second.it )
               || ( ( first.it->get_storage() > second.it->get_storage() )
                    && !second.it->is_worn_only_with( *first.it ) );
    } );

    units::volume storage_loss = 0;                        // Cumulatively increases
    units::volume remaining_storage = p.volume_capacity(); // Cumulatively decreases

    while( !worn.empty() && !inv.empty() ) {
        storage_loss += worn.front().it->get_storage();
        remaining_storage -= p.volume_capacity_reduced_by( storage_loss );

        if( remaining_storage < inv.front().it->volume() ) {
            break; // Does not fit
        }

        while( !inv.empty() && remaining_storage >= inv.front().it->volume() ) {
            remaining_storage -= inv.front().it->volume();

            res.push_back( inv.front() );
            res.back().consumed_moves = 0; // Free of charge

            inv.pop_front();
        }

        res.push_back( worn.front() );
        worn.pop_front();
    }
    // Now insert everything that remains
    std::copy( inv.begin(), inv.end(), std::back_inserter( res ) );
    std::copy( worn.begin(), worn.end(), std::back_inserter( res ) );

    return res;
}
void checkpile(int *pile){
    if(pile[0] < 3){
        return;
    } else if(checkvalid(pile[1], pile[2], pile[pile[0]])){
        push_back(pile[1], deck);
        push_back(pile[2], deck);
        push_back(pile[pile[0]], deck);
        pop_front(pile);
        pop_front(pile);
        pop_back(pile);
        checkpile(pile);
    } else if(checkvalid(pile[1], pile[pile[0]-1], pile[pile[0]])){
        push_back(pile[1], deck);
        push_back(pile[pile[0]-1], deck);
        push_back(pile[pile[0]], deck);
        pop_front(pile);
        pop_back(pile);
        pop_back(pile);
        checkpile(pile);
    } else if(checkvalid(pile[pile[0]-2], pile[pile[0]-1], pile[pile[0]])){
        push_back(pile[pile[0]-2], deck);
        push_back(pile[pile[0]-1], deck);
        push_back(pile[pile[0]], deck);
        pop_back(pile);
        pop_back(pile);
        pop_back(pile);
        checkpile(pile);
    } else {
        return;
    }
}
Example #5
0
void testQ()
{
    std::cout << "Test Lazy Queue\n";

    Queue<int> q0;
    auto q1 = q0.push_back(10);
    printQ(q1);
    auto q2 = q1.push_back(20);
    printQ(q2);
    auto q3 = q2.push_back(30);

    std::cout << "Three element queue\n";

    printQ(q3);
    auto x = q3.front();
    std::cout << "Pop: " << x << std::endl;
    printQ(q3);
    std::cout << "Tail\n";
    auto t1 = q3.pop_front();
    printQ(t1);
    std::cout << std::endl;
    std::cout << "Tail of tail\n";
    auto t2 = t1.pop_front();
    printQ(t2);
    std::cout << "Original\n";
    printQ(q3);
}
Example #6
0
std::list<UString> BitmapFont::wordWrapText(const UString &Text, int MaxWidth)
{
	int txtwidth;
	std::list<UString> lines = Text.splitlist("\n");
	std::list<UString> wrappedLines;

	for (UString str : lines)
	{
		txtwidth = getFontWidth(str);

		if (txtwidth > MaxWidth)
		{
			auto remainingChunks = str.splitlist(" ");
			UString currentLine;

			while (!remainingChunks.empty())
			{
				UString currentTestLine;
				if (currentLine != "")
					currentTestLine = currentLine + " ";

				auto &currentChunk = remainingChunks.front();
				currentTestLine += currentChunk;

				auto estimatedLength = getFontWidth(currentTestLine);

				if (estimatedLength < MaxWidth)
				{
					currentLine = currentTestLine;
					remainingChunks.pop_front();
				}
				else
				{
					if (currentLine == "")
					{
						LogWarning("No break in line \"%s\" found - this will probably overflow "
						           "the control",
						           currentTestLine);
						currentLine = currentTestLine;
						remainingChunks.pop_front();
					}
					else
					{
						wrappedLines.push_back(currentLine);
						currentLine = "";
					}
				}
			}
			if (currentLine != "")
				wrappedLines.push_back(currentLine);
		}
		else
		{
			wrappedLines.push_back(str);
		}
	}

	return wrappedLines;
}
Example #7
0
	void MessageQueue::clear()
	{
		nullmsg* msg = pop_front();
		while(msg)
		{
			free(msg);
			msg = pop_front();
		}
	}
Example #8
0
   boost::optional<std::string> unwrap() {
      if (m_part_data.size() == 0) {
         return opstring_t();
      }
	  opstring_t addr = pop_front();
      if (address()) {
         pop_front();
      }
      return opstring_t(addr);
   }
Example #9
0
int main()
{
   auto d = std::deque<int>{};
   for (auto i = 0; i < 100; ++i)
      d.push_front(i);
   auto l = Bad_list<int>{};
   for (auto i : d) {
      l.push_back(i);
      assert(l.front() == d.front());
   }

   assert(l.size() == difference_type_t<decltype(l)>(d.size()));
   for (; !l.empty(); d.pop_front(), l.pop_front())
      assert(l.front() == d.front());
}
Example #10
0
int main(int argc,char *argv[]){
        list_t list;

        init_list(&list);

        push_front(&list,1);
        push_front(&list,2);
        push_front(&list,3);
        push_front(&list,4);
        push_front(&list,5);

        printf("Pop\n");
        while( list.size != 0 ){
                printf("%d\n",pop_front(&list));
        }

        push_back(&list,1);
        push_back(&list,2);
        push_back(&list,3);
        push_back(&list,4);
        push_back(&list,5);

        printf("Pop\n");
        while( list.size != 0 ){
                printf("%d\n",pop_back(&list));
        }


        return 0;
}
bool CSquirrelArguments::popVector3(CVector3 &vec3)
{
	// Do we have 3 arguments to pop?
	if(size() >= 3)
	{
		// Get 3 arguments from the front
		CSquirrelArgument * pArguments[3];

		for(int i = 0; i < 3; i++)
		{
			pArguments[i] = front();

			// Ensure this argument is a floating point value
			if(pArguments[i]->GetType() != OT_FLOAT)
				return false;

			pop_front();
		}

		// Set the vector
		vec3.fX = pArguments[0]->GetFloat();
		vec3.fY = pArguments[1]->GetFloat();
		vec3.fZ = pArguments[2]->GetFloat();
		return true;
	}

	// Not enough arguments
	return false;
}
int clear(node_ptr head)
{
	while(!is_empty(head)){
		pop_front(head);
	}
	return 0;
}
Example #13
0
bool
Optimize::decrease_truck(size_t cycle) {

    auto position = cycle;
    for (auto orders = fleet[position].orders_in_vehicle();
            !orders.empty();
            orders.pop_front()) {
        /* Step 2: grab an order */
        auto order = fleet[position].orders()[orders.front()];
        pgassert(order.idx() == orders.front());


        /* Step 3:
         * cycle the fleet
         * insert in first truck possible
         */

        for (size_t i = 0; i < position; ++i) {
            fleet[i].insert(order);
            if (fleet[i].is_feasable()) {
                /*
                 * delete the order from the current truck
                 */
                fleet[position].erase(order);
                break;
            } else {
                fleet[i].erase(order);
            }
        }
    }
    return fleet[position].orders_in_vehicle().empty();
}
Example #14
0
Boolean    remove_dlist_node(Dlist *dlist, 
               Dlist_node *node, void **value)    //删除指定节点
{
    if(dlist == NULL || node == NULL){
        return FALSE;
    }

    if(value != NULL){    //取得被删除节点数据域信息
        *value = node->data;
    }

    if(node->next == NULL){    //node在尾部
        pop_back(dlist);
    }else if(node->prev == NULL){
        pop_front(dlist);
    }else{
        node->prev->next = node->next;
        node->next->prev = node->prev;
        if(dlist->free != NULL){
            dlist->free(node->data); 
        }
        free(node);   //Free(node)
        dlist->count--;
    
        /*
 *
 * #define Free(node) (node->prev->next = node->next;) \
 *                    (node->next->prev = node->prev;) \
 *                    
 *
 * */
    }
    return TRUE;
}
Example #15
0
void test_endOfList(list* l) {
    printf("end of list tests\n");
    struct testdata* last = create_testdata("Alex", 0);
    push_front(l, last);
    push_front(l, create_testdata("Steve", 0));
    push_front(l, create_testdata("Joe", 0));

    // also checks contains pointer return
    struct testdata* me = create_testdata("Alex", 55);
    assert(contains(l, me, testdata_string_val_comp)
            == last);
    free_testdata(me);

    list* l2 = copy_list(l, copy_testdata);
    for (int num_prints = 3; num_prints > 0; num_prints--) {
        print_count = 0;
        last_printed = NULL;
        printf("Expecting %d elements to print:\n", num_prints);
        traverse(l2, print_testdata);
        assert(print_count == num_prints);
        assert(strcmp(last_printed, "Alex") == 0);
        free_testdata(pop_front(l2));
    }
    print_count = 0;
    last_printed = NULL;
    traverse(l2, print_testdata);
    assert(print_count == 0);

    free_td_list(l2);
}
Example #16
0
void free_deque_front(deque* d, void (*freefunc)(void*)) {
	while(front(d))
		pop_front(d, freefunc);
	// free the dummy node
	freefunc(d->begin);
	freefunc(d);
}
Example #17
0
CommandBlock *new_command_block(LinkedList *tokens) {
	CommandBlock *result = malloc(sizeof(CommandBlock));
	result->valid = true;
	result->input = NULL;
	result->output = NULL;
	result->command = NULL;

	int success = parse_command(result, tokens);
	if (success == -1) {
		result->valid = false;
		return result;
	}
	
	while (get_length(tokens) > 0) {
		char *tok = (char *) pop_front(tokens);
		if (tok[0] == '>') {
			free(tok);
			if (parse_output(result, tokens) == -1) {
				result->valid = false;
				return result;
			}
		} else if (tok[0] == '<') {
			free(tok);
			if (parse_input(result, tokens) == -1) {
				result->valid = false;
				return result;
			}
		} else {
			free(tok);
			exit(1);
		}
	}

	return result;
}
Example #18
0
int destroy_cmd_list(struct SLList *cmds) {
  struct ExecutableCmd *curr;
  while ( (curr = pop_front(cmds)) ) {
    destroy_exe_cmd(curr);
  }
  return 0;
}
typename List_doubly_linked<type>::iterator List_doubly_linked<type>::erase
	(typename List_doubly_linked::iterator position){
	if(position.get_node_ptr() == sentinel_head.get_next_ptr()){
		/* Erasing the first element. */
		pop_front(); 
		return this->begin(); 
	}
	else if(position.get_node_ptr() == &sentinel_tail){
		/* Trying to erase the node past the last data node. 
		 * This is not possible. 
		 */
		assert(position.get_node_ptr() != &sentinel_tail); 
	}
	else if(position.get_node_ptr() == sentinel_tail.get_prev_ptr()){
		/* Erase last data node. */
		pop_back(); 
		return iterator(sentinel_tail.get_prev_ptr()); 
	}
	else{
		/* Deleting an element in the middle. 
		 * This list contains at least 3 elements for sure. 
		 */
		Node<type> *temp = position.get_node_ptr()->get_prev_ptr(); 
		temp->set_next_ptr(position.get_node_ptr()->get_next_ptr()); 
		position.get_node_ptr()->get_next_ptr()->set_prev_ptr(temp); 
		delete (position.get_node_ptr()); 
		-- list_size; 
		return iterator(temp->get_next_ptr()); 
	}
}
Example #20
0
void CircularList::clear()
{
	while (head != 0)
	{
		pop_front();
	}
}
Example #21
0
            List& erase( Node* node )
            {
                if ( node )
                {
                    // if we are the head
                    if ( node == head ) {
                        pop_front();
					}
                    else if ( node == tail ) {
                        pop_back();
					}
                    else
                    {

                        Node* prev = node->prev;
                        Node* next = node->next;
                        prev->next = next;
                        next->prev = prev;
                        delete( node->item );
                        delete( node );
                        --m_count;
                    }
                }

                // Always return reference back to us
                return *this;
            }
std::shared_ptr<pcb_base>
fb_algo::get_process() {
	if(this->end())
		throw std::runtime_error("no process under management");

	if(this->current_process_ != nullptr)
		return this->current_process_;

	auto i = pcb_queue_.begin();
	auto i_prev = pcb_queue_.begin();
	auto i_end = pcb_queue_.end();
	for(; i != i_end; ++i) {
		if(i->empty())
			continue;

		this->current_process_ = i->front();
		i->pop_front();
		i_prev = i;
		break;
	}//for
	if(i == i_end && i_prev->empty())
		this->empty_ = true;

	return this->current_process_;
}//get_process()
void dealt(){
    int n = 0;
    ans = 8;
    while(1){
        push_back(deck[1], piles[n]);
        pop_front(deck);
        checkpile(piles[n]);

        make_status();
        if(deck[0] == 0){
            printf("Loss: %d\n", ans);
            return;
        } else if(deck[0] == 52){
            printf("Win : %d\n", ans);
            return;
        } else if(exist(status) == 1){
            printf("Draw: %d\n", ans);
            return;
        }

        n = (n+1)%7;
        ans++;
        while(piles[n][0] == 0)
            n = (n+1)%7;
    }
}
Example #24
0
Boolean out(Queue *queue)    //出队
{
    if(queue == NULL || is_empty(queue)){
        return FALSE;
    }
    return pop_front(queue->dlist);
}
	void clear(){

		while(m_size > 0){
			pop_front();
		}

	}
Example #26
0
AccessLogFileData::AccessLogFileData(const std::string& fil,
                                     const std::string& lnk,
                                     const std::string& fmt,
                                     int mpl)
  : file(fil)
  , symLink(lnk)
  , format(fmt)
  , periodMultiplier(mpl)
{
  /*
   * a LogWriter with it's format can be selected between colons like:
   * Format = :thrift: [["%{%s}t", "out-name", "STRING"], ...]
   */
  m_logOutputType = ClassicWriter::handle;
  auto fmt_ = folly::StringPiece(fmt);
  while (!fmt_.empty() && std::isspace(fmt_.front())) fmt_.pop_front();
  if (fmt_.removePrefix(':')) {
    size_t close = fmt_.find(':');
    if (close != fmt_.npos) {
      m_logOutputType = fmt_.subpiece(0, close).str();
      fmt_.advance(close + 1);
      format = folly::trimWhitespace(fmt_).str();
    }
  }
}
Example #27
0
	void clear()
	{
		while ( size() > 0 )
		{
			pop_front();
		}
	}
Example #28
0
void test_emptyList(list* l) {
    printf("empty list tests\n");
    assert(is_empty(l));
    assert(pop_front(l) == NULL);

    empty_list(l, free_testdata);
    empty_list(l, free_testdata);
    assert(is_empty(l));
    assert(size(l) == 0);

    print_count = 0;
    last_printed = NULL;
    traverse(l, print_testdata);
    assert(print_count == 0);

    list* l2 = split_list(l, is_integer_val_23_or_greater);
    assert(is_empty(l));
    assert(is_empty(l2));

    list* l3 = copy_list(l, copy_testdata);
    assert(is_empty(l3));

    assert(get(l, 0) == NULL);
    assert(front(l) == NULL);
    struct testdata* td = create_testdata("Alex", 0);
    assert(contains(l, td, testdata_string_val_comp) == NULL);
    free_testdata(td);

    free_td_list(l2);
    free_td_list(l3);
}
Example #29
0
int parse_input(CommandBlock *result, LinkedList *tokens) {

	//Check if there already was an output read

	if (result->input != NULL) {
		return -1;
	}

	//Now check there isn't an incorrect structure of a leading redirect
	char *first = (char *) pop_front(tokens);
	if (first[0] == '>' || first[0] == '<') {
		free(first);
		return -1;
	}

	int i = 0;
	while (first[i] != '\0')
		i++;

	i++;

	result->input = malloc(i);
	copy_string(first, result->input, i);

	free(first);
	return 0;
}
Example #30
0
    T* Set(size_t k, T* v) {
        if (count() >= size()) {
            auto tmp = front();
            pop_front();
            erase(tmp->key);
        }

        size_t hash_value = hash(k);

        while (table_[hash_value]) {
            hash_value = rehash(hash_value);
        }

        auto& entry = table_[hash_value];
        if (!entry) {
            entry = alloc();
            push_back(entry);
        }
        entry->key = k;
        if (v) {
            memcpy(&entry->value, v, sizeof(T));
        }
        Promote(hash_value);
        return &entry->value;
    }