コード例 #1
0
ファイル: utils.c プロジェクト: coderodde/pathfinding.c
list_t* traceback_bidirectional_path(directed_graph_node_t* p_middle_node,
                                     unordered_map_t* p_parent_map_a,
                                     unordered_map_t* p_parent_map_b)
{
    list_t* p_ret;
    directed_graph_node_t* p_current;
    
    if (!p_middle_node)  return NULL;
    if (!p_parent_map_a) return NULL;
    if (!p_parent_map_b) return NULL;
    
    p_ret = list_t_alloc(INITIAL_CAPACITY);
    
    if (!p_ret) return NULL;
    
    p_current = p_middle_node;
    
    while (p_current) 
    {
        list_t_push_front(p_ret, p_current);
        p_current = unordered_map_t_get(p_parent_map_a, p_current);
    }
    
    p_current = unordered_map_t_get(p_parent_map_b, p_middle_node);
    
    while (p_current)
    {
        list_t_push_back(p_ret, p_current);
        p_current = unordered_map_t_get(p_parent_map_b, p_current);
    }
    
    return p_ret;
}
コード例 #2
0
ファイル: dijkstra.c プロジェクト: coderodde/pathfinding.c
list_t* dijkstra(directed_graph_node_t* p_source,
                 directed_graph_node_t* p_target,
                 directed_graph_weight_function_t* p_weight_function)
{
    search_state_t            state;
    
    list_t*                   p_weight_list;
    heap_t*                   p_open_set;
    unordered_set_t*          p_closed_set;
    unordered_map_t*          p_parent_map;
    unordered_map_t*          p_cost_map;
    
    list_t*                   p_list;
    directed_graph_node_t*    p_current;
    directed_graph_node_t*    p_child;
    unordered_set_iterator_t* p_child_iterator;
    size_t                    i;
    
    /* Cannot pack a double into a void*, so use this simple structure. */
    weight_t*                 p_weight;

    if (!p_source)          return NULL;
    if (!p_target)          return NULL;
    if (!p_weight_function) return NULL;
    
    search_state_t_alloc(&state);
    
    if (!search_state_t_is_ready(&state)) 
    {
        search_state_t_free(&state);
        return NULL;
    }

    p_weight = malloc(sizeof(*p_weight));
    p_weight->weight = 0.0;

    p_open_set    = state.p_open_set;
    p_closed_set  = state.p_closed_set;
    p_weight_list = state.p_weight_list;
    p_cost_map    = state.p_cost_map;
    p_parent_map  = state.p_parent_map;
    
    heap_t_add(p_open_set, p_source, p_weight);
    unordered_map_t_put(p_parent_map, p_source, NULL);
    unordered_map_t_put(p_cost_map, p_source, p_weight);
    list_t_push_back(p_weight_list, p_weight);

    while (heap_t_size(p_open_set) > 0)
    {
        p_current = heap_t_extract_min(p_open_set);

        if (equals_function(p_current, p_target)) 
        {
            p_list = traceback_path(p_target, p_parent_map);
            search_state_t_free(&state);
            return p_list;
        }

        unordered_set_t_add(p_closed_set, p_current);

        p_child_iterator = 
                unordered_set_iterator_t_alloc(
                    directed_graph_node_t_children_set(p_current));

        while (unordered_set_iterator_t_has_next(p_child_iterator)) 
        {
            unordered_set_iterator_t_next(p_child_iterator, &p_child);

            if (unordered_set_t_contains(p_closed_set, p_child)) {
                continue;
            }

            double tmp_cost = 
                ((weight_t*) unordered_map_t_get(p_cost_map, 
                                                 p_current))->weight;

            tmp_cost += *directed_graph_weight_function_t_get(
                    p_weight_function, 
                    p_current, 
                    p_child);

            if (!unordered_map_t_contains_key(p_parent_map, p_child)) 
            {
                p_weight = malloc(sizeof(*p_weight));
                p_weight->weight = tmp_cost;

                heap_t_add(p_open_set, p_child, p_weight);
                unordered_map_t_put(p_parent_map, p_child, p_current);
                unordered_map_t_put(p_cost_map, p_child, p_weight);
                
                list_t_push_back(p_weight_list, p_weight);
            }
            else if (tmp_cost < 
                    ((weight_t*) unordered_map_t_get(p_cost_map, 
                                                     p_child))->weight)
            {
                p_weight = malloc(sizeof(*p_weight));
                p_weight->weight = tmp_cost;

                heap_t_decrease_key(p_open_set, p_child, p_weight);
                unordered_map_t_put(p_parent_map, p_child, p_current);
                unordered_map_t_put(p_cost_map, p_child, p_weight);
                
                list_t_push_back(p_weight_list, p_weight);
            }
        }

        unordered_set_iterator_t_free(p_child_iterator);
    }

    /* Once here, return a empty path in order to denote the fact that the 
       target node is not reachable from source node. */
    search_state_t_free(&state);
    p_list = list_t_alloc(10);

    /* Deallocate the weights. */
    for (i = 0; i < list_t_size(p_weight_list); ++i) 
    {
        free(list_t_get(p_weight_list, i));
    }

    return p_list;
}
コード例 #3
0
ファイル: nbee_link.cpp プロジェクト: ederlf/of11softswitch
extern "C" int nbee_link_convertpkt(struct ofpbuf * pktin, struct hmap * pktout)
{
	//pkhdr->ts.tv_sec = 0;
	pkhdr->caplen = pktin->size; //need this information
	pkhdr->len = pktin->size; //need this information

	_nbPDMLPacket * curr_packet;

	// Decode packet
	if (Decoder->DecodePacket(LinkLayerType, PacketCounter, pkhdr, (const unsigned char*) (pktin->data)) == nbFAILURE)
	{
		printf("\nError decoding a packet %s\n\n", Decoder->GetLastError());
		// Let's break and save what we've done so far
		return -1;
	}

	PDMLReader->GetCurrentPacket(&curr_packet);

	_nbPDMLProto * proto;
	_nbPDMLField * field;

	proto = curr_packet->FirstProto;

	while (1)
        {
        	field = proto->FirstField;
              	while(1)
               	{
			
			if((char)field->LongName[0]<58 && (char)field->LongName[0]>47 && field->isField )
                        {
				/* A value between 47 and 58 indicates a field defined for Matching */
	                        int i,pow;
                                uint32_t type;
                                uint8_t size;
				packet_fields_t * pktout_field;
		                pktout_field = (packet_fields_t*) malloc(sizeof(packet_fields_t));


                                field_values_t *new_field;
                                new_field = (field_values_t *)malloc(sizeof(field_values_t));
				new_field->len = (uint32_t) field->Size;

                                for (type=0,i=0,pow=100;i<3;i++,pow = (pow==1 ? pow : pow/10))
        	                        type = type + (pow*(field->LongName[i]-48));
		                        
				size = field->Size;

                                pktout_field->header = NXM_HEADER(VENDOR_FROM_TYPE(type),FIELD_FROM_TYPE(type),size); 
                                new_field->value = (uint8_t*) malloc(field->Size);
                                memcpy(new_field->value,((uint8_t*)pktin->data + field->Position),field->Size);

				packet_fields_t *iter;
				bool done=0;
				HMAP_FOR_EACH(iter,packet_fields_t, hmap_node,pktout)
				{
					if(iter->header == pktout_field->header)
					{
						/* Adding entry to existing hash entry */
						list_t_push_back(&iter->fields,&new_field->list_node);
						done=1;
						break;
					}
				}

				if (!done)
				{
					/* Creating new hash map entry */
					list_t_init(&pktout_field->fields);
                                	list_t_push_back(&pktout_field->fields,&new_field->list_node);
                                	hmap_insert(pktout, &pktout_field->hmap_node,
	                        	hash_int(pktout_field->header, 0));
				}
				done =0;

			}

			if(field->NextField == NULL && field->ParentField == NULL) 
			{
				/* Protocol Done */
				break;
			}
			else if (field->NextField == NULL && field->ParentField != NULL)
			{
				field = field->ParentField;
			}
			else if (!field->NextField->isField)
			{

				if ((char)field->NextField->LongName[0]<58 && (char)field->NextField->LongName[0]>47)
				{
		                        int i,pow;
	                                uint32_t type;
					packet_fields_t * pktout_field;
			                pktout_field = (packet_fields_t*) malloc(sizeof(packet_fields_t));

		                        field_values_t *new_field;
                	                new_field = (field_values_t *)malloc(sizeof(field_values_t));

					for (type=0,i=0,pow=100;i<3;i++,pow = (pow==1 ? pow : pow/10))
        	                        	type = type + (pow*(field->NextField->LongName[i]-48));
								
				        new_field->value = (uint8_t*) malloc(field->Size);
					_nbPDMLField * nbPrevField; 
				
					if( !field->isField)
						nbPrevField = field->FirstChild;
					else
						nbPrevField = proto->FirstField;

					string NextHeader ("nexthdr");
					bool found = true;
					while(NextHeader.compare(nbPrevField->Name))
					{
						if(nbPrevField->NextField != NULL)
							nbPrevField=nbPrevField->NextField;
						else
						{
							found = false ;
							break;
						}
					}

					if (found)
					{
                        pktout_field->header = NXM_HEADER(VENDOR_FROM_TYPE(type),FIELD_FROM_TYPE(type),nbPrevField->Size);
	                    memcpy(new_field->value,((uint8_t*)pktin->data + nbPrevField->Position),nbPrevField->Size);	
						new_field->len = (uint32_t) nbPrevField->Size;

						packet_fields_t *iter;
						bool done=0;
						HMAP_FOR_EACH(iter,packet_fields_t, hmap_node,pktout)
						{
							if(iter->header == pktout_field->header)
							{
								/* Adding entry to existing hash entry */
								done=1;
								break;
							}
						}
	
						if (!done)
						{
							/* Creating new hash map entry */
							list_t_init(&pktout_field->fields);
		                                	list_t_push_back(&pktout_field->fields,&new_field->list_node);
		                                	hmap_insert(pktout, &pktout_field->hmap_node,
			                        	hash_int(pktout_field->header, 0));
						}
					}
				}
				/* Next field is a block. */
				field = field->NextField->FirstChild;
			}
			else
			{