void AStarSearch::processOpenedList( const ILandscape& _landscape , const GameObject& _forObject , Tools::Core::Object::Ptr _locateComponent , const IPathFinder::PointsCollection& _targets ) { if ( m_openedList.empty() ) return; int dx[ 8 ] = { 0, 0, -1, 1, 1, 1, -1, -1 }; int dy[ 8 ] = { -1, 1, 0, 0, -1, 1, 1, -1 }; int currentPointIndex = findWithMinDistanceInList( m_openedList ); AStarSearch::PointData pointData = m_openedList[ currentPointIndex ]; if ( pointData.m_h == 0 ) { m_closedList.push_back( pointData ); m_openedList.clear(); return; } for ( int i = 0; i < 8; ++i ) { QPoint neighbor( pointData.m_point.x() + dx[ i ], pointData.m_point.y() + dy[ i ] ); if ( !_landscape.isLocationInLandscape( neighbor ) || !_landscape.canObjectBePlaced( neighbor, _forObject.getMember< QString >( ObjectNameKey ) ) || findInList( neighbor, m_closedList ) != -1 ) { continue; } PointData neighborData; neighborData.m_point = neighbor; neighborData.m_parentPoint = pointData.m_point; neighborData.m_g = pointData.m_g + Geometry::getDistance( pointData.m_point, neighbor ); neighborData.m_h = Geometry::getDistance( neighbor, _targets.front() ); neighborData.m_f = neighborData.m_h + neighborData.m_g; int foundNeighborDataIndex = findInList( neighbor, m_openedList ); if ( foundNeighborDataIndex == -1 ) { m_openedList.push_back( neighborData ); } else if ( m_openedList[ foundNeighborDataIndex ].m_g > neighborData.m_g ) { m_openedList[ foundNeighborDataIndex ] = neighborData; } } m_closedList.push_back( pointData ); m_openedList.erase( m_openedList.begin() + currentPointIndex ); } // AStarSearch::processOpenedList
int main() { int data1,data2,data3,data4,data5; int nonData = 6; int * pFind = NULL; List test = NULL; initList(&test); data1 = 10;data2 = 20;data3 = 30;data4 = 40;data5 = 50; addToList(test, (void*)&data1); addToList(test, (void*)&data2); addToList(test, (void*)&data3); addToList(test, (void*)&data4); addToList(test, (void*)&data5); displayList(test); printf("%s","Searching for 30"); pFind = (int*)findInList(test, (void*)&data3, intGreater); if (pFind) { printf("%s%d","\nFound data value: ", *pFind); } else { printf("%s","\nData not found"); } printf("%s","\nSearching for 99 (shouldn't be in list)"); pFind = (int*)findInList(test, (void*)&nonData, intGreater); if (pFind) { printf("%s%d","\nFound data value: ", *pFind); } else { printf("%s","\nData not found"); } printf("%s","\nremoving one item then displaying\n"); removeFirst(test); displayList(test); printf("%s","removing all items then displaying\n"); deleteList(test); displayList(test); printf("%s","Attempting to remove from an empty list\n"); removeFirst(test); displayList(test); cleanupList(&test); printf("%s","All tests complete\n"); return 0; }
Contract * Contract::get(string name) { if(findInList(name) == NULL) { contracts.push_back(new Contract(name)); PriceSource::listen(name); } Contract * c = findInList(name); assert( c != NULL ); return c; }
InputPlugin * getInputPluginFromName(char * name) { void * plugin = NULL; findInList(inputPlugin_list, name, &plugin); return (InputPlugin *)plugin; }
Node * addToList(List * list, void * ptr, size_t size){ if (list == NULL || ptr == NULL){ return NULL; } Node * existing = findInList(list, ptr); if (existing != NULL){ // update the existing size // this would fail to save actually if the ptr was found somewhere in between region, but that ideally should never be the case existing -> size = size; return existing; } // reaching here means add to end of list if (list -> first == NULL){ list -> first = createNode(ptr, size); return list -> first; } Node * prev = list -> first; Node * curr = prev -> next; while(curr != NULL){ prev = curr; curr = curr -> next; } prev -> next = createNode(ptr, size); return prev -> next; }
static int findTreeInList (bestlist *bt, pllInstance *tr, int numBranches) { topol *tpl; tpl = bt->byScore[0]; saveTree(tr, tpl, numBranches); return findInList((void *) tpl, (void **) (& (bt->byTopol[1])), bt->nvalid, cmpTopol); }
void visitInTagTracker(int type, char * str) { void * item; if(!tagLists[type]) return; if(!findInList(tagLists[type], str, &item)) return; ((TagTrackerItem *)item)->visited = 1; }
static int findTreeInList (bestlist *bt, tree *tr) { topol *tpl; tpl = bt->byScore[0]; saveTree(tr, tpl); return findInList((void *) tpl, (void **) (& (bt->byTopol[1])), bt->nvalid, cmpTopol); }
int wasVisitedInTagTracker(int type, char * str) { void * item; if(!tagLists[type]) return 0; if(!findInList(tagLists[type], str, &item)) return 0; return ((TagTrackerItem *)item)->visited; }
/********************************************************************* * Function: ObjectList findScanByNo( list, scan_no, order ) * * Description: Looks for a scan . * * Parameters: * Input: (1) List pointer * (2) scan number * (3) scan order * Returns: * ObjectList pointer if found , * NULL if not. * *********************************************************************/ ObjectList * findScanByNo( ListHeader *list, long scan_no, long order ) { long value[2]; value[0] = scan_no; value[1] = order; return( findInList( (void *)list, findNoAndOr, (void *)value) ); }
void AStarSearch::findPath( PointsCollection& _pointsCollection , const ILandscape& _landscape , const GameObject& _object , const IPathFinder::PointsCollection& _targets ) { _pointsCollection.clear(); if ( _targets.empty() ) return; Tools::Core::Object::Ptr locateComponent = _object.getMemberObject( LocateComponent::Name ); if ( !locateComponent ) return; QPoint startPoint( locateComponent->getMember< QPoint >( LocateComponent::Location ) ); PointData startPointData; startPointData.m_point = startPoint; startPointData.m_parentPoint = startPoint; startPointData.m_g = 0; startPointData.m_h = Geometry::getDistance( startPoint, _targets.front() ); startPointData.m_f = startPointData.m_h + startPointData.m_g; m_openedList.push_back( startPointData ); while ( !m_openedList.empty() ) processOpenedList( _landscape, _object, locateComponent, _targets ); // Path is not found if ( m_closedList.empty() || m_closedList.back().m_h != 0 ) return; assert( m_closedList.size() > 1 ); PointData currentPointData( m_closedList.back() ); while( currentPointData.m_point != startPoint ) { _pointsCollection.push_front( currentPointData.m_point ); currentPointData = m_closedList[ findInList( currentPointData.m_parentPoint, m_closedList ) ]; } } // AStarSearch::findPath
// Manage the list without rearranging the keys. Returns true if any keys on the list changed state. bool Keypad::updateList() { bool anyActivity = false; // Delete any IDLE keys for (byte i=0; i<LIST_MAX; i++) { if (key[i].kstate==IDLE) { key[i].kchar = NO_KEY; key[i].kcode = -1; key[i].stateChanged = false; } } // Add new keys to empty slots in the key list. for (byte r=0; r<sizeKpd.rows; r++) { for (byte c=0; c<sizeKpd.columns; c++) { boolean button = bitRead(bitMap[r],c); char keyChar = keymap[r * sizeKpd.columns + c]; int keyCode = r * sizeKpd.columns + c; int idx = findInList (keyCode); // Key is already on the list so set its next state. if (idx > -1) { nextKeyState(idx, button); } // Key is NOT on the list so add it. if ((idx == -1) && button) { for (byte i=0; i<LIST_MAX; i++) { if (key[i].kchar==NO_KEY) { // Find an empty slot or don't add key to list. key[i].kchar = keyChar; key[i].kcode = keyCode; key[i].kstate = IDLE; // Keys NOT on the list have an initial state of IDLE. nextKeyState (i, button); break; // Don't fill all the empty slots with the same key. } } } } } // Report if the user changed the state of any key. for (byte i=0; i<LIST_MAX; i++) { if (key[i].stateChanged) anyActivity = true; } return anyActivity; }
struct equivalenceNode *insertEquivalentLabels(struct equivalenceNode *root, int value){ if (findInList(root, value) == 0) { struct equivalenceNode *current = root; struct equivalenceNode *newNode = malloc(sizeof(struct equivalenceNode)); newNode->label = value; if (current->label > value) { newNode->next = current; root = newNode; } else{ while (current->next != NULL && current->next->label < value) { current = current->next; } newNode->next = current->next; current->next = newNode; } } return root; }
/********************************************************************* * Function: ObjectList *findFirstInFile( list, file_offset ) * * Description: Looks for a scan . * * Parameters: * Input: (1) List pointer * (2) scan index * Returns: * ObjectList pointer if found , * NULL if not. * *********************************************************************/ ObjectList * findFirstInFile( ListHeader *list, long file_offset ) { return findInList( list, findFirst, (void *)&file_offset ); }
/********************************************************************* * Function: ObjectList *findScanByIndex( list, index ) * * Description: Looks for a scan . * * Parameters: * Input: (1) List pointer * (2) scan index * Returns: * ObjectList pointer if found , * NULL if not. * *********************************************************************/ ObjectList * findScanByIndex( ListHeader *list, long index ) { return findInList( list, findIndex, (void *)&index ); }
/************************************************************************************************ findCommand() Arguments: myUser - struct with 'my' information saIP - IP address of SA saPort - port of SA Description: Finds requested user IP and TCP port. *************************************************************************************************/ int findCommand(char * buffer, char * saIp, int saPort){ char userToFind[128]; char outBuffer[256]; char inBuffer[256]; char UserName[128]; char IP[50]; char answer [10]; char givenName[50]; char family[50]; int saSock; int retval; int udpPort; int tcpPort; socklen_t len; struct sockaddr_in addr; struct hostent *h; struct in_addr *a; user * usr; if(saSocket <= 0){ printf("\n>Whoops! Looks like you forgot to call join first..\n"); return 1; } /*gets host's ip adress*/ if((h=gethostbyname(saIp)) == NULL){ perror("\n>Gethostbyname Error\n"); return -1; } /*casts first usable name into in_addr struct form*/ a = (struct in_addr*)h -> h_addr_list[0]; memset((void*)&addr, '\0', sizeof(addr)); addr.sin_port = htons(saPort); addr.sin_family = AF_INET; addr.sin_addr = *a; memset(userToFind, '\0', 128); if(connectToUser == 1){ sscanf(buffer, "connect %[^\n]", userToFind); }else{ sscanf(buffer, "find %[^\n]", userToFind); } saSock = socket(AF_INET, SOCK_DGRAM, 0); memset(outBuffer, '\0', 256); snprintf(outBuffer, 256, "QRY %s", userToFind); retval = sendto(saSock, outBuffer, strlen(outBuffer), 0, (struct sockaddr*)&addr, sizeof(addr)); if(retval < 0 ){ perror("\n>Error in QRY sending to SA\n"); return -1; } len = sizeof(addr); memset(inBuffer, '\0', 256); retval = recvfrom(saSock, inBuffer, 256, 0, (struct sockaddr*)&addr, &len); if(retval < 0 ){ perror("\n>ERRO NO QRY RECVING TO SA\n"); return -1; } memset(UserName, '\0', 128); memset(answer, '\0', 10); memset(IP, '\0', 50); if((sscanf(inBuffer, "%[^ ] %[^;];%[^;];%d", answer, UserName, IP, &udpPort)) != 4){ if(strcmp(answer, "FW") == 0){ printf("\n>There seems to be no user by the name of %s\n", userToFind); return 1; } return -1; } if(strcmp(answer, "FW") != 0){ printf("\n>Bad format. Please re-introduce the command\n"); return -99; } /**REPEAT QUERY, BUT TO SNP. MUST WRITE SNP'S ANSWER!*/ inet_pton(AF_INET,IP, (void*)a); addr.sin_port = htons(udpPort); addr.sin_family = AF_INET; addr.sin_addr = *a; memset(outBuffer, '\0', 256); memset(givenName, '\0', 50); memset(family, '\0', 50); snprintf(outBuffer, 256, "QRY %s", userToFind); sscanf(userToFind, "%[^.].%s", givenName, family); if(!((strcmp(family, globalMyUser.surname) == 0) && (snp == 1))){ retval = sendto(saSock, outBuffer, strlen(outBuffer), 0, (struct sockaddr*)&addr, sizeof(addr)); if(retval < 0 ){ perror("\n>ERROR IN QRY SENDING TO SNP\n"); return -1; } len = sizeof(addr); memset(inBuffer, '\0', 256); retval = recvfrom(saSock, inBuffer, 256, 0, (struct sockaddr*)&addr, &len); if(retval < 0 ){ perror("\n>ERROR IN QRY RECVING TO SA\n"); return -1; } memset(UserName, '\0', 50); memset(answer, '\0', 10); memset(IP, '\0', 50); if((sscanf(inBuffer, "%[^ ] %[^;];%[^;];%d", answer, UserName, IP, &tcpPort)) != 4){ if(strcmp(answer, "RPL") == 0){ printf("\n>There seems to be no user by the name of %s\n", userToFind); } return -99; } if(strcmp(answer, "RPL") != 0){ printf("\n>Bad format. Please re-introduce the command\n"); return -99; } }else{ usr = findInList(userToFind); memset(IP, '\0', 50); strcpy(IP, usr->IP); tcpPort = usr->tcpPort; if((!strcmp(givenName, usr->name)) && (!strcmp(family, usr->surname))){ connectUserName = UserName; connectIP = IP; connecttcp = tcpPort; if(connectToUser == 0){ printf("\n>User %s Found at %s using port %d\n", UserName, IP, tcpPort); } close(saSock); return 1; } } if(!strcmp(userToFind, UserName)){ connectUserName = UserName; connectIP = IP; connecttcp = tcpPort; if(connectToUser == 0){ printf("\n>User %s Found at %s using port %d\n", UserName, IP, tcpPort); } close(saSock); return 1; } close(saSock); return 0; }
bool find(MyString &str, const HashTable &hashT) { int hashOfCurStr = hash(str, hashT.size); return findInList(str, hashT.arr[hashOfCurStr]); }
std::vector<Vector3> NavigationGraph::calcPath(const Vector3& currentPosition, const Vector3& targetPosition) { NavigationNode* startNode = getNodeAt(currentPosition); NavigationNode* endNode = getNodeAt(targetPosition); //std::cout << "start: " << startNode->getCenter() << std::endl; //std::cout << "end: " << endNode->getCenter() << std::endl; std::vector<Vector3> path; if(startNode && endNode && (startNode != endNode)) { // A* path finding //std::priority_queue<AStarNode, std::vector<AStarNode>, std::greater<AStarNode>> open_list; std::list<AStarNode> open_list; std::list<AStarNode> closed_list; AStarNode end = AStarNode(endNode, NULL, startNode, 0); open_list.push_back(end); AStarNode curNode = end; while(!open_list.empty() && (curNode.node != startNode)) { curNode = open_list.front(); closed_list.push_back(curNode); open_list.pop_front(); bool toSort = false; std::vector<Connection*> connections = curNode.node->getConnections(); for(int i=0; i<connections.size(); i++) { AStarNode n(connections[i]->getToNode(), &closed_list.back(), startNode, curNode.distToStart+1); if(findInList(n, closed_list) == NULL) { AStarNode* nInOpenList = findInList(n, open_list); if(nInOpenList != NULL) { if(n.distToStart < nInOpenList->distToStart ) { nInOpenList->distToStart = n.distToStart; } } else { open_list.push_back(n); toSort = true; } } } if(toSort) { open_list.sort(AStarNode::compare); } } //std::cout << "FLUUUHTSCH!!!" << std::endl; AStarNode* curPathNode = &closed_list.back(); path.push_back(curPathNode->node->getCenter()); while(curPathNode != NULL) { curPathNode = curPathNode->previousNode; if(curPathNode != NULL) { //curPathNode->printNode(); path.push_back(curPathNode->node->getCenter()); } } if(path.size() < 2) { path.push_back(endNode->getCenter()); } /* NavigationNode* bestNeighbour = connections[0]->getToNode(); Vector3 toTarget = bestNeighbour->getCenter() - endNode->getCenter(); float minDist = toTarget.x + toTarget.z; for(int i=1; i<connections.size(); i++) { toTarget = connections[i]->getToNode()->getCenter() - endNode->getCenter(); float dist = toTarget.x + toTarget.z; if(dist <= minDist) { bestNeighbour = connections[i]->getToNode(); minDist = dist; } } path.push_back(startNode->getCenter()); path.push_back(endNode->getCenter()); */ } else { path.push_back(currentPosition); path.push_back(targetPosition); } return path; }
int saveBestTree (bestlist *bt, tree *tr, boolean keepIdenticalTrees) { topol *tpl, *reuse; int tplNum, scrNum, reuseScrNum, reuseTplNum, i, oldValid, newValid; tplNum = findTreeInList(bt, tr); tpl = bt->byScore[0]; oldValid = newValid = bt->nvalid; if(tplNum > 0) { /* Topology is in list */ if(!keepIdenticalTrees) return 0; reuse = bt->byTopol[tplNum]; /* Matching topol */ reuseScrNum = reuse->scrNum; reuseTplNum = reuse->tplNum; } /* Good enough to keep? */ else { if(tr->likelihood < bt->worst) return 0; else { /* Topology is not in list */ tplNum = -tplNum; /* Add to list (not replace) */ if (newValid < bt->nkeep) bt->nvalid = ++newValid; reuseScrNum = newValid; /* Take worst tree */ reuse = bt->byScore[reuseScrNum]; reuseTplNum = (newValid > oldValid) ? newValid : reuse->tplNum; if (tr->likelihood > bt->start->likelihood) bt->improved = TRUE; } } scrNum = findInList((void *) tpl, (void **) (& (bt->byScore[1])), oldValid, cmpTplScore); scrNum = ABS(scrNum); if (scrNum < reuseScrNum) { for (i = reuseScrNum; i > scrNum; i--) (bt->byScore[i] = bt->byScore[i-1])->scrNum = i; } else { if (scrNum > reuseScrNum) { scrNum--; for (i = reuseScrNum; i < scrNum; i++) (bt->byScore[i] = bt->byScore[i+1])->scrNum = i; } } if(tplNum < reuseTplNum) for (i = reuseTplNum; i > tplNum; i--) (bt->byTopol[i] = bt->byTopol[i-1])->tplNum = i; else { if (tplNum > reuseTplNum) { tplNum--; for (i = reuseTplNum; i < tplNum; i++) (bt->byTopol[i] = bt->byTopol[i+1])->tplNum = i; } } tpl->scrNum = scrNum; tpl->tplNum = tplNum; bt->byTopol[tplNum] = bt->byScore[scrNum] = tpl; bt->byScore[0] = reuse; if (scrNum == 1) bt->best = tr->likelihood; if (newValid == bt->nkeep) bt->worst = bt->byScore[newValid]->likelihood; return scrNum; }
// Manage the list without rearranging the keys. Returns true if any keys on the list changed state. bool Keypad_MAX::updateList() { bool anyActivity = false; // Delete IDLE keys for (byte i=0; i<LIST_MAX; i++) { if (key[i].kstate==IDLE) { key[i].kchar = NO_KEY; key[i].kcode = -1; key[i].stateChanged = false; } } #if 0 // Add new keys to empty slots in the key list. for (byte r=0; r<sizeKpd.rows; r++) { for (byte c=0; c<sizeKpd.columns; c++) { boolean button = bitRead(bitMap[r],c); char keyChar = keymap[r * sizeKpd.columns + c]; int keyCode = r * sizeKpd.columns + c; int idx = findInList (keyCode); // Key was found on the list so set its next state. if (idx > -1) nextKeyState(idx, button); // Key is not on the list so add it. if ((idx == -1) && button) { for (byte i=0; i<LIST_MAX; i++) { if (key[i].kchar==NO_KEY) { // Find an empty slot or don't add key to list. key[i].kchar = keyChar; key[i].kcode = keyCode; key[i].kstate = IDLE; // Keys NOT on the list have an initial state of IDLE. nextKeyState (i, button); break; // Don't fill all the empty slots with the same key. } } } } } #endif // MAX7359 does not need row/column examination of bitmap. Keycodes already have the // row and column values, so just need to read from the Wire buffer filled in getKeys byte keyCode; do { keyCode = TwoWire::read( ); if( keyCode == 0x3f ) { // if key fifo is empty for( byte r=0; r<sizeKpd.rows; r++ ) { for( byte c=0; c<sizeKpd.columns; c++ ) { // check list for all codes keyCode = rowPins[r] * sizeKpd.columns + columnPins[c]; int idx = findInList( (int)keyCode ); if( idx > -1 && key[idx].kstate != PRESSED ) { nextKeyState( (byte)idx, false ); anyActivity = true; } // if( idx > -1 && (key[idx].kstate == PRESSED // || key[idx].kstate == HOLD) ) { // this gets HOLD noticed // nextKeyState( (byte)idx, CLOSED ); // but causes release if held // anyActivity = true; // } } // all columns } // all rows return anyActivity; } bool button = ( (keyCode&0x40) != 0x40 ); // bit6 = 1 ==>release byte krow = rowPins[(keyCode&0x3f)%8]; byte kcol = columnPins[(keyCode&0x3f)/8]; char keyChar = keymap[krow * sizeKpd.columns + kcol]; int idx = findInList( (int)(keyCode&0x3f) ); // key already on list if( idx > -1 ) nextKeyState( (byte)idx, button ); // key needs to be added if( (idx == -1) && button ) { for( byte i=0; i<LIST_MAX; i++ ) { if( key[i].kchar == NO_KEY ) { // find empty slot key[i].kchar = keyChar; key[i].kcode = keyCode&0x3f; key[i].kstate = IDLE; // init entry to IDLE nextKeyState( i, button ); // set to button (PRESSED) break; } // if list slot empty } // for all list } // if not on list } while( (keyCode&0x80) == 0x80 ); // last valid key read from fifo has clear bit7 // Report if the user changed the state of any key. for (byte i=0; i<LIST_MAX; i++) { if (key[i].stateChanged) anyActivity = true; } return anyActivity; }