Пример #1
0
// {{{ popNum
void popNum(List * l, int num) {
  if (nb(l) == 0) return;
  if (num(begin(l)) == num) { popHead(l); return; }
  if (num(end(l)) == num) {popTail(l); return; }
  Item * it = begin(l);
  while (it != NULL && num(it) < num) it = next(it);
  if (it == NULL || num(it) > num) return;
  else {
    next(prev(it)) = next(it);
    prev(next(it)) = prev(it);
    free(it);
    nb(l)--;
  }
}
Пример #2
0
void List<T>::erase(iterator &pos)
{
	// ... -- el1 -- el2 -- el3 -- ...
	//Deleting el2
	if ((checkEmpty()) || (pos.currentNode==NULL))
	{
		throw listExc(1, __LINE__);
	}
	else
	{
		Node<T> *newNode=pos.currentNode;
		if (pos.currentNode->prevNode==NULL)
		{
			//cout << "deleting head" << endl;
			popHead();
			length++;
			//++pos;
			//delete newNode;
		}
		else if (pos.currentNode->nextNode==NULL)
			{
				//cout << "deleting tail" << endl;
				pop();
				length++;
				//--pos;
				//delete newNode;
			} else
				{
					//cout << "deleting in body" << endl;
					pos.currentNode->prevNode->nextNode=newNode->nextNode;
					//		el2			el1		  ->el2	=	el2		->el3
					//el1->next = el3
					newNode->nextNode->prevNode=pos.currentNode->prevNode;
					//	el2		el3		->el2			el2			->el1
					//el3->prev = el1
					pos.currentNode=pos.currentNode->prevNode;
					delete newNode;
				}

	}
	length--;
}
Пример #3
0
/**
 * \fn MovementList* searchPath(Case start, Case destination)
 * \brief The A star algorithm implementation.
 *
 * \param start The case where a enemy is.
 * \param destination The case a enemy must go.
 * \return The path.
 */     
MovementList* searchPath(Map *map, Case start, Case destination) {
	//Initialisation
	Node* endNode = getNode(destination);
	Node* firstNode = getNode(start);
	firstNode->cumulateNodeCost = 0;
	MovementList* nextMoves;
	
	/*
	 There are two lists :
	 - the openList contains a set of to process node;
	 - the closedList contains a set of already processed node
	 */
	List* openList = newList(NULL);
	openList = head(firstNode, openList);
	
	List* closedList = NULL;
	
	// *A Star*
	
	/*
	 if there aren't any node in openList and the path aren't be found,
	 there aren't any path
	 */
	
	while(openList->nextList) {               
		Node* lowestHeuristic = popHead(&openList);    // we test the most closer node (with the lowest heuristic cost)
		if(theseTwoNodeAreEquals(lowestHeuristic,endNode)) {
			nextMoves = pathReConstruction(map, lowestHeuristic);    // if we have reached the final node, a path have been found
			freeList(openList);
			freeList(closedList);
			return nextMoves;
		}
		
		closedList = push(lowestHeuristic,closedList);    // this node have been computed
		
		/*
		 For each node near the one which is computed,
		 we check if he have already computed or cannot be tested (if it's a wall or a tower)
		 */
		int i, j;
		float k;
		for( i=-1, j=0, k=0.5; k<=2; j =- ((( (int)(-2*k) - 1 )%3)+1), i=(int)(k+=0.5) - 1 ) {    // i={-1,0,0,1} j={0,1,-1,0}
			Case nearCase = *getCase(map, i+lowestHeuristic->x, j+lowestHeuristic->y);    // get the four Case near the one already computed 
			Node *nearNode = getNode(nearCase);
			
			if(nearCase.hasTower || amIInDaList(nearNode, closedList)) {    // we are not open to node already computed or 
				continue;    // wich can't be walked on
			}
			
			/*
			 * then we calculate their heuristic cost (node with less heuristic are prefered
			 * for the path)
			 */
			
			// complete node's construction
			nearNode->previousNode = lowestHeuristic;
			// and calculate it heuristic
			nearNode->cumulateNodeCost = heuristicCost(nearNode,endNode);
			
			Node *openNode = amIInDaList(nearNode,openList);    //if the node is already in the openList
			
			if(openNode){    //we check if the new one have better heuristic
				if(openNode->cumulateNodeCost > nearNode->cumulateNodeCost) {    //if so, we switch the two node
					openNode->cumulateNodeCost = nearNode->cumulateNodeCost;    //else (if it is not in the openList)
					openNode->previousNode = nearNode->previousNode;    //we simply put it in
				}
			}
			else {
				openList = push(nearNode,openList);
			}
		}
	}         
	freeList(openList);
	freeList(closedList);
	return NULL;
}
Пример #4
0
List *multiMerge(unsigned int count, ...) {
  // Pass the number of lists to merge, the lists themselves
  // and the very last argument should be the Comparator
  List *merged = NULL;
#ifdef DEBUG
  printf("Func:: %s\n", __func__);
#endif

  va_list ap;
  va_start(ap, count);

  List **lBlock = (List **)malloc(sizeof(List *) * count);
  List *tmpL = NULL;

  register unsigned int i=0;
  while (i < count) {
    tmpL = va_arg(ap, List *);
    lBlock[i++] = tmpL;
  }

  Comparator matchFunc = va_arg(ap, Comparator);

#ifdef DEBUG
  printf("ap: %p\n", ap);
#endif

  va_end(ap);
  if (i) {
    unsigned int popularCount = count, maxCount = i, thresholdCount = 0.75 * count;
    List **minList = NULL;
    while (popularCount >= thresholdCount) {
    #ifdef DEBUG
      for (i=0; i < maxCount; ++i) {
	printList(lBlock[i]); 
	printf("\n");
      }
      printf("\033[35m\nBlockEntries\n\033[00m");
    #endif

      popularCount = maxCount;
      // Picking min list -- Beginning
      unsigned int minIdx=0;
      while (minIdx < maxCount) {
        minList = lBlock + minIdx;
        if (peek(*minList) != NULL)
          break;
	else
          ++minIdx;
      }
      // Picking min list -- End

      if (peek(*minList) == NULL) {
        printf("No more selections for minList can be made\n");
        goto cleanUpForReturn;
      }

      int gallop = 0, nonEmptyCount = 0;
      for (i=0; i < maxCount; ++i) {
	if (peek(lBlock[i]) == NULL) {
	  --popularCount;
	} else {
	  ++nonEmptyCount;
	  Comparison comp = matchFunc(peek(*minList), peek(lBlock[i]));
	  if (comp == Equal) {
	    ++gallop;
	  } else if (comp == Greater) {
	    minList = lBlock + i;
	  }
	}
      }

      if (gallop == nonEmptyCount) { // All elements were equal
	for (i=0; i < maxCount; ++i) {
	  if (peek(lBlock[i]) != NULL) {
	    merged = append(merged, popHead(lBlock[i]));
	  }
	}
      } else {
	merged = append(merged, popHead(*minList));
      }
    }

    for (i=0; i < maxCount; ++i) {
      while (peek(lBlock[i]) != NULL) {
	merged = append(merged, popHead(lBlock[i]));
      }
    }
  }

  cleanUpForReturn: {
    free(lBlock);
  }
  
  return merged;
}