int main () { srandom ((unsigned) time (NULL)); zmq::context_t context(1); zmq::socket_t server(context, ZMQ_REP); server.bind("tcp://*:5555"); int cycles = 0; while (1) { std::string request = s_recv (server); cycles++; // Simulate various problems, after a few cycles if (cycles > 3 && within (3) == 0) { printf ("I: simulating a crash\n"); break; } else if (cycles > 3 && within (3) == 0) { printf ("I: simulating CPU overload\n"); sleep (5); } printf ("I: normal request (%s)\n", request.c_str()); sleep (1); // Do some heavy work s_send (server, request); } return 0; }
// TODO: shortcut number of comparisons Interval::IntervalComparison Interval::compare(const Interval& other) const { // // Intersect cases // // TODO: rewrite this to be member functions so semantics are clearer. if (intersects(*this, other)) { if (exact(*this, other)) { return INTERVAL_EQUALS; } if (within(*this, other)) { return INTERVAL_WITHIN; } if (within(other, *this)) { return INTERVAL_CONTAINS; } if (precedes(*this, other)) { return INTERVAL_OVERLAPS_BEFORE; } return INTERVAL_OVERLAPS_AFTER; } // // Non-intersect cases // if (precedes(*this, other)) { if (0 == end.woCompare(other.start, false)) { return INTERVAL_PRECEDES_COULD_UNION; } return INTERVAL_PRECEDES; } return INTERVAL_SUCCEEDS; }
// TODO: shortcut number of comparisons Interval::IntervalComparison Interval::compare(const Interval& other) const { // // Intersect cases // if (intersects(*this, other)) { if (exact(*this, other)) { return INTERVAL_EQUALS; } if (within(*this, other)) { return INTERVAL_WITHIN; } if (within(other, *this)) { return INTERVAL_CONTAINS; } if (precedes(*this, other)) { return INTERVAL_OVERLAPS_BEFORE; } return INTERVAL_OVERLAPS_AFTER; } // // Non-intersect cases // if (precedes(*this, other)) { return INTERVAL_PRECEDES; } return INTERVAL_SUCCEDS; }
void AmplitudeWidget::mousePressEvent(QMouseEvent *e) { View *view = gdata->view; int timeX = toInt(view->viewOffset() / view->zoomX()); //int pixelAtCurrentNoiseThresholdY = toInt(gdata->noiseThresholdDB() / gdata->dBFloor() / range() * double(height())); int pixelAtCurrentNoiseThresholdY; dragMode = DragNone; //Check if user clicked on center bar, to drag it if(within(4, e->x(), timeX)) { dragMode = DragTimeBar; mouseX = e->x(); return; } //Check if user clicked on a threshold bar for(int j=0; j<2; j++) { pixelAtCurrentNoiseThresholdY = height() - 1 - toInt((getCurrentThreshold(j) - offsetInv()) / range() * double(height())); if(within(4, e->y(), pixelAtCurrentNoiseThresholdY)) { dragMode = DragNoiseThreshold; thresholdIndex = j; //remember which thresholdIndex the user clicked mouseY = e->y(); return; } } //Otherwise user has clicked on background { mouseX = e->x(); mouseY = e->y(); dragMode = DragBackground; downTime = view->currentTime(); downOffset = offset(); } }
int main () { // Prepare our context and publisher zmq::context_t context (1); zmq::socket_t publisher (context, ZMQ_PUB); publisher.bind("tcp://*:5556"); // publisher.bind("ipc://weather.ipc"); // Initialize random number generator srandom ((unsigned) time (NULL)); while (1) { int zipcode, temperature, relhumidity; // Get values that will fool the boss zipcode = within (100000); temperature = within (215) - 80; relhumidity = within (50) + 10; // Send message to all subscribers zmq::message_t message(20); snprintf ((char *) message.data(), 20 , "%05d %d %d", zipcode, temperature, relhumidity); publisher.send(message); } return 0; }
double distance(struct position a, struct position b, const region_map_t* regs) { // find a region that fits a. bool a_in_reg = false; bool b_in_reg = false; int r; for (r=0; r<SIZE(*regs); ++r) { if (within(a, GET(*regs,r))) { a_in_reg = true; } if (within(b, GET(*regs,r))) { b_in_reg = true; } if (within(a, GET(*regs,r)) && within(b, GET(*regs,r))) { return rawDistance(a,b); } } /* if (!a_in_reg) { iprintf ("%7d WARNING(a): (%4.1f, %4.1f, %4.1f) is not in any region.\n", warn_count, a.x, a.y, a.z); warn_count++; } if (!b_in_reg) { iprintf ("%7d WARNING(b): (%4.1f, %4.1f, %4.1f) is not in any region.\n", warn_count, b.x, b.y, b.z); warn_count++; } */ return INFINITY; }
// Set simple random printable identity on socket // static void s_set_id (zmq::socket_t & socket) { char identity [10]; sprintf (identity, "%04X-%04X", within (0x10000), within (0x10000)); socket.setsockopt(ZMQ_IDENTITY, identity, strlen (identity)); }
int main () { srand ((unsigned) time (NULL)); zmq::context_t context(1); zmq::socket_t server(context, ZMQ_REP); server.bind("tcp://*:5555"); int cycles = 0; while (1) { std::string request = s_recv (server); cycles++; // Simulate various problems, after a few cycles if (cycles > 3 && within (3) == 0) { std::cout << "I: simulating a crash" << std::endl; break; } else if (cycles > 3 && within (3) == 0) { std::cout << "I: simulating CPU overload" << std::endl; s_sleep (2000); } std::cout << "I: normal request (" << request << ")" << std::endl; s_sleep (1000); // Do some heavy work s_send (server, request); } return 0; }
// Set simple random printable identity on socket // inline std::string s_set_id (zmq::socket_t & socket) { std::stringstream ss; ss << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << within (0x10000) << "-" << std::setw(4) << std::setfill('0') << within (0x10000); socket.setsockopt(ZMQ_IDENTITY, ss.str().c_str(), ss.str().length()); return ss.str(); }
// return true if the current cell (i, j) is used by the current stage of calibration bool TouchTracker::Calibrator::isWithinCalibrateArea(int i, int j) { if(mCollectingNormalizeMap) { return(within(i, 1, mWidth - 1) && within(j, 0, mHeight)); } else { return(within(i, 2, mWidth - 2) && within(j, 0, mHeight)); } }
// Returns a pointer to the header of a relative virtual address static PIMAGE_SECTION_HEADER RvaToHdr(LPVOID base, DWORD rva, PIMAGE_NT_HEADERS nt, LPVOID end) { PIMAGE_SECTION_HEADER sect = IMAGE_FIRST_SECTION(nt); unsigned i; for ( i=0; (i < nt->FileHeader.NumberOfSections) && within(base,sect,end); i++, sect++ ) { if ( (rva>=sect->VirtualAddress) && (rva<(sect->VirtualAddress+sect->Misc.VirtualSize))) { return within(base,sect,end)?sect:NULL; } } return NULL; }
void AmplitudeWidget::mouseMoveEvent(QMouseEvent *e) { View *view = gdata->view; int pixelAtCurrentTimeX = toInt(view->viewOffset() / view->zoomX()); //int pixelAtCurrentNoiseThresholdY = -toInt(gdata->noiseThresholdDB() / dBRange() * double(height())); //int pixelAtCurrentNoiseThresholdY = toInt(gdata->noiseThresholdDB() / gdata->dBFloor() / range() * double(height())); int pixelAtCurrentNoiseThresholdY; switch(dragMode) { case DragTimeBar: { int newX = pixelAtCurrentTimeX + (e->x() - mouseX); view->setViewOffset(double(newX) * view->zoomX()); mouseX = e->x(); view->doSlowUpdate(); } break; case DragNoiseThreshold: { //int newY = pixelAtCurrentNoiseThreshold + (e->y() - mouseY); int newY = e->y(); /*if(gdata->amplitudeMode() == FREQ_CHANGENESS) { gdata->setChangenessThreshold(double(newY) / double(height())); } else { gdata->setNoiseThresholdDB(double(newY) / double(height()) * gdata->dBFloor() * range()); }*/ setCurrentThreshold(double(height() - 1 - newY) / double(height()) * range() + offsetInv(), thresholdIndex); mouseY = e->y(); gdata->view->doSlowUpdate(); } break; case DragBackground: //view->setViewBottom(downNote - (mouseY - e->y()) * view->zoomY()); gdata->updateActiveChunkTime(downTime - (e->x() - mouseX) * view->zoomX()); setOffset(downOffset - (double(e->y() - mouseY) / double(height()) * range())); view->doSlowUpdate(); break; case DragNone: if(within(4, e->x(), pixelAtCurrentTimeX)) { setCursor(QCursor(Qt::SplitHCursor)); } else { bool overThreshold = false; for(int j=0; j<2; j++) { pixelAtCurrentNoiseThresholdY = height() - 1 - toInt((getCurrentThreshold(j) - offsetInv()) / range() * double(height())); if(within(4, e->y(), pixelAtCurrentNoiseThresholdY)) overThreshold = true; } if(overThreshold) setCursor(QCursor(Qt::SplitVCursor)); else unsetCursor(); } } }
static void edu_check_range(uint32_t addr, uint32_t size1, uint32_t start, uint32_t size2) { uint32_t end1 = addr + size1; uint32_t end2 = start + size2; if (within(addr, start, end2) && end1 > addr && within(end1, start, end2)) { return; } hw_error("EDU: DMA range 0x%.8x-0x%.8x out of bounds (0x%.8x-0x%.8x)!", addr, end1 - 1, start, end2 - 1); }
/* --- Produce the vector end points produced by the overlap of two other vectors --- */ static void subVector(int *v1, int *v2, int t1, int t2, int o1, int o2) { *v1 = *v2 = -1; if (within(o1, t1, t2)) { *v1 = o1; if (within(o2, t1, t2)) *v2 = o2; else *v2 = t2; } else if (within(o2, t1, t2)) { *v2 = o2; if (within(o1, t1, t2)) *v1 = o1; else *v1 = t1; } else if (within(t1, o1, o2)) { *v1 = t1; if (within(t2, o1, o2)) *v2 = t2; else *v2 = o2; } else if (within(t2, o1, o2)) { *v2 = t2; if (within(t1, o1, o2)) *v1 = t1; else *v1 = o1; } }
void Terrain_SelectAreaPoints( void ) { brush_t *pb; terrainMesh_t *p; int x; int y; vec3_t vec; g_qeglobals.d_numterrapoints = 0; g_nPatchClickedView = -1; for( pb = selected_brushes.next; pb != &selected_brushes; pb = pb->next ) { if ( pb->terrainBrush ) { p = pb->pTerrain; for( x = 0; x < p->width; x++ ) { for( y = 0; y < p->height; y++ ) { Terrain_CalcVertPos( p, x, y, vec ); if ( within( vec, g_qeglobals.d_vAreaTL, g_qeglobals.d_vAreaBR ) ) { if ( g_qeglobals.d_numterrapoints < MAX_TERRA_POINTS ) { g_qeglobals.d_terrapoints[ g_qeglobals.d_numterrapoints++ ] = &p->heightmap[ x + y * p->width ]; } } } } } } }
static int gcov_module_notifier(struct notifier_block *nb, unsigned long event, void *data) { struct module *mod = data; struct gcov_info *info; struct gcov_info *prev; if (event != MODULE_STATE_GOING) return NOTIFY_OK; mutex_lock(&gcov_lock); prev = NULL; /* */ for (info = gcov_info_head; info; info = info->next) { if (within(info, mod->module_core, mod->core_size)) { if (prev) prev->next = info->next; else gcov_info_head = info->next; if (gcov_events_enabled) gcov_event(GCOV_REMOVE, info); } else prev = info; } mutex_unlock(&gcov_lock); return NOTIFY_OK; }
static inline void discard_clusters(Turns& turns, Clusters const& clusters, Geometry0 const& geometry0, Geometry1 const& geometry1) { for (typename Clusters::const_iterator cit = clusters.begin(); cit != clusters.end(); ++cit) { signed_size_type cluster_id = cit->first; // If there are only self-turns in the cluster, the cluster should // be located within the other geometry, for intersection if (is_self_cluster(cluster_id, turns, clusters)) { cluster_info const& cinfo = cit->second; if (! within(turns[*cinfo.turn_indices.begin()], geometry0, geometry1)) { // Discard all turns in cluster for (std::set<signed_size_type>::const_iterator sit = cinfo.turn_indices.begin(); sit != cinfo.turn_indices.end(); ++sit) { turns[*sit].discarded = true; } } } } }
static void * worker_thread (void *arg) { zmq::context_t * context = (zmq::context_t *)arg; zmq::socket_t worker (*context, ZMQ_REQ); // We use a string identity for ease here s_set_id (worker); worker.connect("ipc://routing.ipc"); int total = 0; while (1) { // Tell the router we're ready for work s_send (worker, "ready"); // Get workload from router, until finished std::string workload = s_recv (worker); int finished = (workload.compare("END") == 0); if (finished) { std::cout << "Processed: " << total << " tasks" << std::endl; break; } total++; // Do some random work s_sleep(within (100) + 1); } return (NULL); }
static void * worker_thread (void *context) { void *worker = zmq_socket (context, ZMQ_REQ); // We use a string identity for ease here s_set_id (worker); zmq_connect (worker, "ipc://routing.ipc"); int total = 0; while (1) { // Tell the router we're ready for work s_send (worker, "ready"); // Get workload from router, until finished char *workload = s_recv (worker); int finished = (strcmp (workload, "END") == 0); free (workload); if (finished) { printf ("Processed: %d tasks\n", total); break; } total++; // Do some random work struct timespec t; t.tv_sec = 0; t.tv_nsec = within (100000000) + 1; nanosleep (&t, NULL); } return (NULL); }
OcTree *OcTree::findParent(sceneobj_t *sc) { OcTree *p=NULL; int i=4, max=8; if(sc->userdata) return (OcTree*)sc->userdata; if(within(sc)) { p = this; if(children[0]) { //we are garaunteed all 8 if(sc->pos[AXIS_UP] < pos[AXIS_UP]) { i=0;max=OCTREE_MAX_CHILDREN/2; } for(;i<max;i++) { OcTree *q = children[i]->findParent(sc); if(q) { p = q; break; } } } } sc->userdata = p; return p; }
bool inside(vec3f pos, float fudge) { mat3f rot; //if(ctr == nullptr) { btTransform trans; rigid_body->getMotionState()->getWorldTransform(trans); btVector3 bpos = trans.getOrigin(); btQuaternion bq = trans.getRotation(); quaternion q = {{bq.x(), bq.y(), bq.z(), bq.w()}}; rot = q.get_rotation_matrix(); vec3f v = {bpos.x(), bpos.y(), bpos.z()}; vec3f rel = pos - v; return within(b, rot.transp() * rel, fudge); } /*rot = ctr->rot_quat.get_rotation_matrix(); vec3f my_pos = xyz_to_vec(ctr->pos); return within(b, rot.transp() * (pos - my_pos));*/ }
void SuBlock::persist() { if (persisted) return; if (frame->fn != fn) except_err("orphaned block!"); // won't need to copy locals if another block persist has already done so if (within(tls().proc->stack, frame->local)) { // save locals on heap - only done once per call/frame Value* old_locals = frame->local; int n = frame->fn->nlocals * sizeof (Value); frame->local = (Value*) memcpy(new char[n], frame->local, n); // update any other active frames pointing to the same locals // i.e. nested blocks within one parent frame for (Frame* f = tls().proc->fp; f > tls().proc->frames; --f) if (f->local == old_locals) f->local = frame->local; } // save frame on heap frame = (Frame*) memcpy(new Frame, frame, sizeof (Frame)); persisted = true; }
void TRexGame::AccelerateGame(double delta_v) { double accelerated = game_speed_ + delta_v; if (within(accelerated, kGameMinSpeed, kGameMaxSpeed)) { game_speed_ = accelerated; } }
void main(int argc, string argv[]) { initparam(argv, defv); if (within(getdparam("val"), getparam("range"), getdparam("fuzz"))) printf("within returns TRUE\n"); else printf("within returns FALSE\n"); }
void nemo_main() { stream instr, outstr; string times, ctypei, ctypeo; real tsnap; int i, nbody, bits, mode; Body *btab = NULL, *bp; proc transform, trans2; times = getparam("times"); ctypei = getparam("ctypei"); ctypeo = getparam("ctypeo"); if (streq(ctypei,"cart") && streq(ctypeo,"sph")) transform = cartesian_spherical; else if (streq(ctypei,"sph") && streq(ctypeo,"cart")) transform = spherical_cartesian; else if (streq(ctypei,"cart") && streq(ctypeo,"cyl")) transform = cartesian_cylindrical; else if (streq(ctypei,"cyl") && streq(ctypeo,"cart")) transform = cylindrical_cartesian; else error("Unimplemented ctype i/o : %s -> %s",ctypei,ctypeo); dprintf(0,"converting from %s to %s\n",ctypei,ctypeo); instr = stropen(getparam("in"), "r"); outstr = stropen(getparam("out"), "w"); get_history(instr); put_history(outstr); for (;;) { get_history(instr); /* skip over stuff we can forget */ if (!get_tag_ok(instr, SnapShotTag)) break; /* done with work in loop */ get_snap(instr, &btab, &nbody, &tsnap, &bits); if ((bits & MassBit) == 0 && (bits & PhaseSpaceBit) == 0) { continue; /* just skip it's probably a diagnostics */ } if ((bits & TimeBit) == 0) tsnap = 0.0; else if (!streq(times,"all") && !within(tsnap, times, TIMEFUZZ)) continue; dprintf (1,"Transforming snapshot at time= %f bits=0x%x\n",tsnap,bits); #if 0 Qmass = MassBit & bits; Qphase = PhaseSpaceBit & bits; Qacc = AccelerationBit & bits; Qaux = AuxBit & bits; Qkey = KeyBit & bits; #endif for (bp = btab; bp < btab+nbody; bp++) { (transform)(Pos(bp),Vel(bp),Acc(bp)); } put_snap(outstr, &btab, &nbody, &tsnap, &bits); } }
u16 I2cSoft::waitAck() { _sda.init(GPIO_Mode_IN_FLOATING); _scl.set(Bit_SET); vu16 t; within(_FLAG_TIMEOUT, _sda.getInput() == Bit_SET); _scl.set(Bit_RESET); return t; }
void MLPluginProcessor::setStateFromMIDIProgram (const int idx) { if(within(idx, 0, kMLPluginMIDIPrograms)) { if(mMIDIProgramFiles[idx].exists()) { loadStateFromFile(mMIDIProgramFiles[idx]); } } }
void testing_$RAND_expand(bool verbose) { if (verbose) { fprintf( stdout, "\n----- testing_$RAND_expand ----\n\n"); } // regressions REQUIRE( within(expand("$RANDOM_INTEGER(-30,30,1)"), -30, 30) ); REQUIRE( within(expand("$RANDOM_INTEGER(2,2,1)"), 2, 2) ); REQUIRE( within(expand("$RANDOM_INTEGER(-10,0,2)"), -10, 0) ); REQUIRE( within(expand("$RANDOM_INTEGER(-1000,-99,3)"), -1000, -99) ); REQUIRE( within(expand("$RANDOM_INTEGER(3,9,3)"), 3, 9) ); REQUIRE( within(expand("$RANDOM_INTEGER(0,9,3)"), 0, 9) ); REQUIRE( within(expand("$RANDOM_CHOICE(1,2,2,1)"), 1, 2) ); REQUIRE( within(expand("$RANDOM_CHOICE(aa,bb,cc)"), "aa", "cc") ); REQUIRE( within(expand("$RANDOM_CHOICE(List6c)"), "aa", "ff") ); REQUIRE( within(expand_as("MASTER", "$RANDOM_CHOICE(List6c)"), "JMK", "ZKM") ); REQUIRE( expand("$RANDOM_CHOICE(1)") == "1" ); REQUIRE( expand("$RANDOM_CHOICE(aa bb cc)") == "aa bb cc" ); }
int main(int argc, char *argv[]) { int opt; printf("multiply 1 by 2 a number of times\n"); for(opt=0; opt<=1; ++opt) { printf(" (optimize:%d) result=%d\n", opt, multiply(opt, 24)); } printf("check if abstract value N within threshold T\n"); for(opt=0; opt<=1; ++opt) { printf(" (optimize:%d) result=%d\n", opt, within(opt, -3, 3)); printf(" (optimize:%d) result=%d\n", opt, within(opt, 3, 3)); printf(" (optimize:%d) result=%d\n", opt, within(opt, -4, 3)); printf(" (optimize:%d) result=%d\n", opt, within(opt, 4, 3)); } return 0; }
static inline void apply(Turns& turns, Clusters const& clusters, Geometry0 const& geometry0, Geometry1 const& geometry1) { discard_clusters(turns, clusters, geometry0, geometry1); typedef typename boost::range_value<Turns>::type turn_type; for (typename boost::range_iterator<Turns>::type it = boost::begin(turns); it != boost::end(turns); ++it) { turn_type& turn = *it; if (turn.discarded || ! is_self_turn<overlay_intersection>(turn)) { continue; } segment_identifier const& id0 = turn.operations[0].seg_id; segment_identifier const& id1 = turn.operations[1].seg_id; if (id0.multi_index != id1.multi_index || (id0.ring_index == -1 && id1.ring_index == -1) || (id0.ring_index >= 0 && id1.ring_index >= 0)) { // Not an ii ring (int/ext) on same ring continue; } if (turn.is_clustered() && turn.has_colocated_both) { // Don't delete a self-ii-turn colocated with another ii-turn // (for example #case_recursive_boxes_70) // But for some cases (#case_58_iet) they should be deleted, // there are many self-turns there and also blocked turns there if (! any_blocked(turn.cluster_id, turns, clusters)) { continue; } } // It is a ii self-turn // Check if it is within the other geometry // If not, it can be ignored if (! within(turn, geometry0, geometry1)) { // It is not within another geometry, discard the turn turn.discarded = true; } } }