void test_right_neighbour() { long *n1 = neighbours(1,4,2); CU_ASSERT_EQUAL(2,n1[1]); free(n1); long *n2 = neighbours(1,2,3); CU_ASSERT_EQUAL(0,n2[1]); free(n2); }
void test_lower_neighbour() { long *n1 = neighbours(13,4,4); CU_ASSERT_EQUAL(9,n1[2]); free(n1); long *n2 = neighbours(1,4,2); CU_ASSERT_EQUAL(5,n2[2]); free(n2); }
void test_left_neighbour() { long *n1 = neighbours(5,4,2); CU_ASSERT_EQUAL(4,n1[3]); free(n1); long *n2 = neighbours(4,2,3); CU_ASSERT_EQUAL(5,n2[3]); free(n2); }
/* Test that neighbours are computed correctly */ void test_upper_neighbour() { long *n1 = neighbours(2,2,3); CU_ASSERT_EQUAL(4,n1[0]); free(n1); long *n2 = neighbours(8,3,3); CU_ASSERT_EQUAL(2,n2[0]); free(n2); }
static int simplify (IMC_Unit * unit) { int changes = 0; int x; SymReg **g; g = unit->reglist; for (x = 0; x < n_symbols; x++) { if (g[x]->color >= 0) /* VTPASM */ g[x]->simplified = 1; } for (x = 0; x < n_symbols; x++) { if (g[x]->simplified) { break; } if ( neighbours(x) < MAX_COLOR) { debug(interpreter, DEBUG_IMC, "#simplifying [%s]\n", g[x]->name); imcstack_push(nodeStack, x); g[x]->simplified = 1; changes = 1; break; } } return changes; }
int main(void) { int maxiters; printf("# Iterations: "); scanf("%d", &maxiters); for (int n = 1; n <= maxiters; n++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { int nn = neighbours(i,j); if (board[i][j] == 1) { if (nn < 2) newboard[i][j] = 0; else if (nn ==2 || nn == 3) newboard[i][j] = 1; else newboard[i][j] = 0; } else if (nn == 3) newboard[i][j] = 1; else newboard[i][j] = 0; } } printf("=== After iteration %d ===\n", n); copyBackAndShow(); } return 0; }
//Move std::vector<mboard> generator::GenerateMove(mboard board) { int i=0; //a list of board positions std::vector<mboard> List; for(i=0;i<board.getPosition().size();i++) { if(board.getPosition_Value(i)=='W') { std::vector<int> nbs=neighbours(i); for (int j=0; j<nbs.size(); j++) { int k=nbs.at(j); if(board.getPosition_Value(k)=='x') { mboard board_copy=board; board_copy.setPosition_Value(i, 'x'); board_copy.setPosition_Value(k, 'W'); if(closeMill(k, board_copy)) { List = GenerateRemove(board_copy, List); } else { List.push_back(board_copy); } } } } } return List; }
/* 2. За избран човек, да се изведат приятели на неговите приятели, които имат поне 2 общи интереса с него. Хората да се изведат по брой общи интереси в низходящ ред. */ LList<Person> findFriendsOfFriends(graph<Person> g, Person person) { LList<Person> result; LList<Person> friends = neighbours<Person>(g, person); elem<Person> * pFriend; friends.IterStart(); while ((pFriend = friends.Iter())) { LList<Person> friendsFriends = neighbours(g, pFriend->inf); elem<Person> * pf; friendsFriends.IterStart(); while ((pf = friendsFriends.Iter())) { if (pf->inf == person) { continue; } if (contains(friends, pf->inf)) { continue; } result.ToEnd(pf->inf); } } return result; }
/* If possible, merge segment with its neighbours - some segments, including s, may be destroyed in the process */ static void merge_segments(Addr a, UInt len) { Segment *s; Segment *next; vg_assert((a & (VKI_BYTES_PER_PAGE-1)) == 0); vg_assert((len & (VKI_BYTES_PER_PAGE-1)) == 0); a -= VKI_BYTES_PER_PAGE; len += VKI_BYTES_PER_PAGE; for(s = VG_(SkipList_Find)(&sk_segments, &a); s != NULL && s->addr < (a+len);) { next = VG_(SkipNode_Next)(&sk_segments, s); if (next && neighbours(s, next)) { Segment *rs; if (0) VG_(printf)("merge %p-%p with %p-%p\n", s->addr, s->addr+s->len, next->addr, next->addr+next->len); s->len += next->len; s = VG_(SkipNode_Next)(&sk_segments, next); rs = VG_(SkipList_Remove)(&sk_segments, &next->addr); vg_assert(next == rs); freeseg(next); } else s = next; } }
edge *transform_graph(const std::vector<Edge>& edges, const std::vector<length_t> &lengths, size_t points, size_t &width) { std::vector<size_t> neighbours(edges.size(), 0); for (size_t i = 0; i < edges.size(); ++i) { ++neighbours[edges[i].p1]; } size_t max_n = *std::max_element(neighbours.begin(), neighbours.end()); edge *graph = new edge[points * max_n]; for (size_t i = 0; i < points; ++i) { size_t k = 0; for (size_t j = 0; j < points; ++j) { graph[i * max_n + j].len = get_length(edges, lengths, i, j); if (graph[i * max_n + j].len != 0) { graph[i * max_n + j].p1 = i; graph[i * max_n + j].p2 = j; } } } width = max_n; }
void DbScan::expandCluster(std::vector<DbScan::Point>::iterator P, unsigned& clusterId) { // Get density reachable points in eps-neighbourhood of P (seeds) std::vector< std::vector<DbScan::Point>::iterator > seeds = neighbours(P); // Point P is noise so do not form cluster and label P as noise if(seeds.size() < minPoints_) { P->label = 0; return; } // Point P is a core point so label all point and seeds with cluster ID P->label = clusterId; for(unsigned i = 0; i <= seeds.size(); ++i) seeds[i]->label = clusterId; // Scan neighbourhood of seeds for other density reachable points for(unsigned i = 0; i < seeds.size(); ++i) { std::vector<DbScan::Point>::iterator seed = seeds[i]; // Get all density reachable points in eps-neighbourhood of seed std::vector< std::vector<DbScan::Point>::iterator > result = neighbours(seed); // Seed is also a core point if(result.size() > minPoints_) { for(unsigned j = 0; j <= result.size(); ++j) { // Point is unclassified or noise if(result[i]->label < 1) { // Point is unclassified so add to seeds if(result[i]->label == -1) seeds.push_back(result[i]); } // Assign unclassified/noise point to current cluster result[i]->label = clusterId; } } } // Done with this cluster, so increment cluster ID for next starting P ++clusterId; }
bool GraphEL::path(vector<vertex> vertices) { for (int i = 0; i < (vertices.size() - 1); i++) { if (!neighbours(vertices[i], vertices[i + 1])) return false; } return true; }
void SandPile::increaseNeighbours(int point,std::vector<int> & lat){ int* neighbour[2*dimension]; neighbours(point,neighbour,lat); for(int i=0;i<dimension*2;i++){ if(!(neighbour[i]==NULL)){ // std::cout << "set neighbour to " << value << std::endl; *(neighbour[i]) = *(neighbour[i]) + 1; } } }
bool Game::check(int num) { if (num < fieldSize) return false; // top border if (num % fieldSize == 0) return false; // left if (num % fieldSize == fieldSize - 1) return false; // right if (num >= fieldSize * (fieldSize - 1)) return false; // bottom QVector <Cell *> nei = neighbours(num); return std::all_of(nei.begin(), nei.end(), [](Cell *c) {return (c->color() == Qt::white) || (c->color() == Qt::lightGray);}); }
void tick() { struct timespec *remaining; /* Holds the remaining time. Not used */ /* t = current tick iteration * x = x coordinate * y = y coordinate * n = neighbours for a coordinate */ int t, x, y, n; /* Sync the buffer with the game area * buffer <- cells. * The buffer and cells are separated so that the calculations from this * round won't affect this round. For example let's imagine that coordinate * (y,x) (1,1) is active but is deemed dead after visiting it. Then we * check the coordinate (y,x) (2,1) which now has only 1 neighbour and also * dies. By separating the cells and buffer we keep the cells untouchable * during the round, but after we have finished the round, we can sync * them. */ COPYC; for(t = 0; t < ticksize; t++) { /* Iterations */ for(x = 0; x < lifecols; x++) { for(y = 0; y < lifelines; y++) { /* Get the neighbours for the coordinate */ n = neighbours(y, x); /* If a cell has less than two neighbours it dies, no matter * what. If a cell has more than three neighbours it's * overcrowded and dies too. */ if(n < 2 || n > 3) { ABCPR(y,x); mvwaddch(life, y, x, ' '); } /* If a cell has 3 neighbours it revives */ if(n == 3) { BCPR(y, x); mvwaddch(life, y, x, 'X'); } /* The only possibility left is that the cell has two * neighbours, which means that the cell stays alive if it's * alive, if it's dead it stays dead. */ } } /* Sync the cells with the buffer */ COPYB; /* Refresh the screen */ wrefresh(life); /* Sleep for while */ nanosleep(&sleeptime, remaining); } }
void Game::mouseMoveEvent(QMouseEvent *event) { if(check(event->pos())) { auto curItem = itemAt(event->x(), event->y()); Cell *cellItem = static_cast<Cell *>(curItem); if(oldCross == -1) { oldCross = cellItem->num; } if (oldCross != cellItem->num) { QVector <Cell *> oldCrossCells = neighbours(oldCross); oldCrossCells << cells.at(oldCross); for (auto c : oldCrossCells) { c->setColor(Qt::white); } } else { if(cells.at(oldCross)->color() != Qt::white) return; } oldCross = cellItem->num; QVector <Cell *> cross = neighbours(cellItem); cross << cellItem; if(turn % 2 == 0) { for (auto c : cross) { c->setColor(Qt::lightGray); } } else { for (auto c : cross) { c->setColor(Qt::lightGray); } } } else { if (oldCross == -1) return; QVector <Cell *> oldCrossCells = neighbours(oldCross); oldCrossCells << cells.at(oldCross); for (auto c : oldCrossCells) { c->setColor(Qt::white); } oldCross = -1; } }
BloomFilter NeighbourList::Neighbours( const RDSize count ) const { BloomFilter neighbours( count, BloomFilter::DesiredFalsePositiveRate ); for( std::vector< std::pair<RDTimeStamp, Node> >::const_iterator i = m_neighbours.begin(); i != m_neighbours.end(); ++i ) { neighbours.Insert( i->second.Address() ); } return neighbours; }
void make_iter(char** src_matrix, char** dst_matrix, int matln) { int i, j; for(i = 0; i < matln; i++) { for(j = 0; j < matln; j++) { int n = neighbours(src_matrix, i, j, matln); if(n < 2) dst_matrix[i][j] = 0; else if(n == 2) dst_matrix[i][j] = src_matrix[i][j]; else if(n == 3) dst_matrix[i][j] = 1; else dst_matrix[i][j] = 0; } } }
list *update(list *ol, elem e, location t, elem *np, int n) { elem *nextElem = neighbours(e, np, n); for(int i = 0; i < n; i++) { //printPos(nextElem[i]); //nextElem[i].h = abs(nextElem[i].l.x - t.x) + abs(nextElem[i].l.y - t.y); // Manhattan Distance nextElem[i].h = sqrt(pow((nextElem[i].l.x-t.x), 2) + pow(nextElem[i].l.y-t.y, 2)); //euclidean distance ol = append(ol, nextElem[i]); } return ol; }
int main(void) { struct json_object *obj; printf("Access-Control-Allow-Origin: *\n"); printf("Content-type: text/event-stream\n\n"); while (1) { obj = neighbours(); printf("data: %s\n\n", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)); fflush(stdout); json_object_put(obj); sleep(1); } return 0; }
// Modified from Aaron, Daniel, Christopher // TODO: Remove all hard coded variables with BOARDX / BOARDY void evolve(int *board) { // copy the board so we can overwrite it int copy[20 * 20]; // TODO(Smerity): Replace with memcpy for improved speed for (int i = 0; i < 20; i++) for (int j = 0; j < 20; j++) copy[i * BOARDX + j] = board[i * BOARDX + j]; // calculate the next step for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { int surrounding = neighbours(copy, i, j); board[i * BOARDX + j] = (surrounding == 3) || (surrounding == 2 && copy[i * BOARDX + j]); } } }
QVector<int> Majority::neighbourhood(QPoint i) { QVector<int> neighbours(8); neighbours[0] = m_state->atObserved(i.x() - 1, i.y() - 1); neighbours[1] = m_state->atObserved(i.x(), i.y() - 1); neighbours[2] = m_state->atObserved(i.x() + 1, i.y() - 1); neighbours[3] = m_state->atObserved(i.x() - 1, i.y()); neighbours[4] = m_state->atObserved(i.x() + 1, i.y()); neighbours[5] = m_state->atObserved(i.x() - 1, i.y() + 1); neighbours[6] = m_state->atObserved(i.x(), i.y() + 1); neighbours[7] = m_state->atObserved(i.x() + 1, i.y() + 1); return neighbours; }
void Game::mousePressEvent(QMouseEvent *event) { QGraphicsItem *curItem; QRect r(scene->width()/2 - (cellSize / 2) * fieldSize, scene->height()/2 - (cellSize / 2) * fieldSize, fieldSize * cellSize, fieldSize * cellSize); if (r.contains(event->pos())) // это клетка! { if(std::all_of(cells.begin(), cells.end(), [](Cell* c) { return c->color() != Qt::white; })) { for(auto c : cells) { c->setColor(Qt::white); } } else { curItem = itemAt(event->x(), event->y()); Cell *cellItem = static_cast<Cell *>(curItem); if (check(cellItem)) { QVector <Cell *> cross = neighbours(cellItem); cross << cellItem; if(turn % 2 == 0) { for (auto c : cross) { c->setColor(Qt::red); } } else { for (auto c : cross) { c->setColor(Qt::blue); } } // костыль cellItem->setPen(* new QPen(Qt::NoPen)); cross.at(1)->setPen(* new QPen(Qt::NoPen)); cross.at(3)->setPen(* new QPen(Qt::NoPen)); oldCross = -1; turn = (turn + 1) % 2; } } } }
void grid::nextgeneration( ) { for(int i = 0; i < xsize * ysize; i++ ) { int friends = neighbours(i); if ( c[i]. s0 && friends < 2 ) c[i]. s1 = false; else if ( c[i]. s0 && friends == 2 || friends == 3 ) c[i]. s1 = true; else if ( c[i]. s0 && friends > 3 ) c[i]. s1 = false; else if ( !c[i]. s0 && friends == 3 ) c[i]. s1 = true; } for(int i = 0; i < xsize*ysize; i++) c[i]. s0 = c[i]. s1; }
void gameoflife(square **map){ int i, j; int curNeigh; for(i = 0; i<MAPX; i++){ for(j = 0; j<MAPY; j++){ curNeigh = neighbours(map, i, j); if(map[i][j].cur == LIVE){ if(curNeigh < 2 || curNeigh > 3){ map[i][j].next = DEAD; }else{ map[i][j].next = LIVE; } } if(map[i][j].cur == DEAD && curNeigh == 3) map[i][j].next = LIVE; } } update(map); draw(map, 0); }
static void order_spilling (IMC_Unit * unit) { int min_score = 0, total_score; int min_node; int x; while (1) { min_node = -1; for (x = 0; x < n_symbols; x++) { /* for now, our score function only takes in account how many times a symbols has appeared + the loop_depth */ /* we have to combine somehow the rank of the node * with the cost of spilling it * * our current function to maximize is: * rank - spill_cost * * I have no clue of how good it is */ if (!(unit->reglist[x]->simplified)) { total_score = unit->reglist[x]->score - neighbours(x); if ( (min_node == -1) || (min_score > total_score) ) { min_node = x; min_score = total_score; } } } if (min_node == -1) return; /* We are finished */ imcstack_push(nodeStack, min_node); unit->reglist[min_node]->simplified = 1; } }
GridWorld::TilePair GridWorld::getMinSuccessor(GridWorld::Tile*& tile){ Tile* minTile = 0; double minCost = PF_INFINITY; std::vector<Tile*> neighbours(getNeighbours(tile)); for (int i = 0; i < neighbours.size(); i++){ double cost = calculateC(tile, neighbours[i]); double g = neighbours[i]->g; if (cost == PF_INFINITY || g == PF_INFINITY){ continue; } if (cost + g < minCost){ //potential overflow? minTile = neighbours[i]; minCost = cost + g; } } return TilePair(minTile, minCost); }
void Board::checkMatches() //checks if bubbles touching are same color { same=neighbours(current); if(!same.isEmpty()) { if(same.size()>2) //if there are at least 3 same color bubbles { //deletes all the same color bubbles touching deleteBubbles(same); deleteConnected(); } else //if same color bubbles touhing is 2 or less { chancesleft--; //reduces turn if(chancesleft<0) //checks if turn gets to zero chancesleft=5; //resets tunn turn->display(chancesleft); //displays new turns left } } else { chancesleft--; //reduce turn if(chancesleft<0) //check if tun is less than 0 chancesleft=5; //resets turn turn->display(chancesleft); //displays new turn } while(!same.isEmpty()) //empty all lists of bubbles touching same.takeLast(); while(!temp.isEmpty()) temp.takeLast(); while(!temp2.isEmpty()) temp2.takeLast(); }
bool Country::communicateWith(const Country* otherCountry) const { if (!otherCountry) { kDebug() << "OUT otherCountry null Country::communicateWith" << endl; return false; } // a country is considered to communicate with itself if (otherCountry == this) {return true;} // kDebug() << "Country::communicateWith (" << name() << ", " << otherCountry-> name() << ")" << endl << flush; // unsigned int nbNeighbours = neighbours().size(); foreach (Country* neighbour, neighbours()) { if (neighbour == otherCountry) { // kDebug() << "OUT true Country::communicateWith" << endl << flush; return true; } } // kDebug() << "OUT false Country::communicateWith" << endl << flush; return false; }
void level2(void) { int i, ii, j, k, l; float trad[3]; int liste[M], inliste, n1liste[POSI], inn1liste; int n2liste[POSI], inn2liste, n3liste[POSI], inn3liste; float seekx[3], esti, acc, vel[3]; int finish, flag, nvel; /* Define some varibles. This is done every time tracking is called (my be not necessary but is not a big problem*/ trad[0]= tpar.dvxmax; trad[1]= tpar.dvymax; trad[2]= tpar.dvzmax; /* BEGIN TRACKING*/ /* Secondly start with second priority this is if particle has already no link to previous timstep but in Particles in neigbourhoud have. Current timstep is t[1]*/ /*first search for tracks with no previous links ancd no next link*/ /*links named -1 or -2 are no links*/ for(inliste=0, i=0; i<m[1]; i++) if (mega[1][i].next < 0 && mega[1][i].prev < 0) { /*check if neighbours wihtin correlation have link*/ for(j=0; j < 3; j++) seekx[j] = mega[1][i].x[j]; /* search points in neigbourhood within coorelation lenght*/ inn1liste = 0; neighbours(seekx, trad, n1liste, &inn1liste, 1); /*check if neighbours have previous link*/ /*n1liste must be greater than 1 because neigbours will find the point i itself*/ if(inn1liste > 1) { for(vel[0]=0.0, vel[1]=0.0, vel[2]=0.0, nvel=0, j=0; j<inn1liste; j++) { if(n1liste[j]!=i) if(mega[1][n1liste[j]].prev > -1){ for(l=0; l<3; l++) vel[l] += mega[1][n1liste[j]].x[l]- mega[0][mega[1][n1liste[j]].prev].x[l]; nvel++; } } if(nvel > 0) { /*intermediate storage of center of position in next frame */ for(l=0; l<3; l++) mega[1][i].decis[l] = vel[l]/(float)nvel; liste[inliste]=i; inliste++; } } } /*calculate possible tracks for t2 and t3 and calculate decision criteria*/ if(inliste > 0) { for(i=0; i < inliste; i++) { for(j=0; j < 3; j++) seekx[j] = mega[1][liste[i]].x[j] + mega[1][liste[i]].decis[j]; /*find neighbours in next timestep t = 2*/ inn2liste = 0; neighbours(seekx, trad, n2liste, &inn2liste, 2); /* if no neighour is found no link will be established*/ /*calculate decision criteria*/ if(inn2liste > 0) { for(k=0; k<inn2liste; k++) { for(j=0; j < 3; j++) seekx[j] = 2*mega[2][n2liste[k]].x[j]-mega[1][liste[i]].x[j]; /*find neigbours in next timestep t = 3*/ inn3liste = 0; neighbours(seekx, trad, n3liste, &inn3liste, 3); if(inn3liste == 0) { /*if no neighour in t3 is found, give decision criteria artifical value (100000) accelaration can be considered as unbelivible large*/ mega[1][liste[i]].decis[k] = 1000000.0; mega[1][liste[i]].linkdecis[k] = n2liste[k]; } else { for(esti = 1000000.0, l=0; l<inn3liste; l++) { /*calculate for estimates the decision value*/ for(acc=0.0, ii=0; ii<3; ii++) acc += sqr(mega[1][liste[i]].x[ii] - 2*mega[2][n2liste[k]].x[ii] + mega[3][n3liste[l]].x[ii]); acc=sqrt(acc); if(esti > acc) esti = acc; }/*for(l....)*/ mega[1][liste[i]].decis[k] = esti; mega[1][liste[i]].linkdecis[k] = n2liste[k]; } /*else(inn2liste >0*/ }/*for (k...)*/ mega[1][liste[i]].inlist = inn2liste; if(inn2liste > 1) sort(inn2liste, mega[1][liste[i]].decis, mega[1][liste[i]].linkdecis); }/*if(inn1liste > 0)*/ }/*for(i=0....)*/ /*establish links by streaming completly through the data*/ do { finish = 0; for(i=0; i<inliste; i++) { if(mega[1][liste[i]].next < 0) { if(mega[1][liste[i]].inlist > 0) { /*in the following is a sorted list of decis assumed*/ flag = 1; j = 0; do { if(mega[2][mega[1][liste[i]].linkdecis[j]].prev < 0) { /*found possible link*/ mega[1][liste[i]].next = mega[1][liste[i]].linkdecis[j]; mega[2][mega[1][liste[i]].linkdecis[j]].prev = liste[i]; mega[2][mega[1][liste[i]].linkdecis[j]].prio = 1; mega[1][liste[i]].finaldecis = mega[1][liste[i]].decis[j]; flag = 0; }/*if(p2 == -1)*/ /*test exiting link if would be better*/ else if((mega[1][mega[2][mega[1][liste[i]].linkdecis[j]].prev].finaldecis > mega[1][liste[i]].decis[j]) && (mega[2][mega[1][liste[i]].linkdecis[j]].prio >= 1)) { /*current link is better and reset other link*/ mega[1][mega[2][mega[1][liste[i]].linkdecis[j]].prev].next = -2; mega[1][liste[i]].next = mega[1][liste[i]].linkdecis[j]; mega[2][mega[1][liste[i]].linkdecis[j]].prev = liste[i]; mega[2][mega[1][liste[i]].linkdecis[j]].prio = 1; mega[1][liste[i]].finaldecis = mega[1][liste[i]].decis[j]; flag = 0; finish = 1; } else { j++; /* if first choice is not possible then try next */ } }while((j < mega[1][liste[i]].inlist) && flag); }/*if(mega[1]....)*/ else { mega[1][liste[i]].next=-1; /*No link could be established*/ }/*else*/ }/*if(mega[1] .next<0)*/ }/*for(i=0....)*/ }while(finish); }/*if(inlist >0)*/ /*END OF second TRAIL*/ }