double rfloat(char *p, int n) { double i, d=0; while(*p==' ' && n) p++, --n; if(*p == '+') return rfloat(p+1, n-1); if(*p == '-') return -rfloat(p+1, n-1); while(*p == ' ' && n) p++, --n; if(n == 0) return 0.0; while(n-- && *p!='.') d = d*10+*p++-'0'; if(n <= 0) return d; p++; i = 1; while(n--) d+=(*p++-'0')/(i*=10.); return d; }
vec3f rvec3f( FILE *fp ) { vec3f v; v.x=rfloat(fp); v.y=rfloat(fp); v.z=rfloat(fp); return v; }
vec4f rvec4f( FILE *fp ) { vec4f v; v.x=rfloat(fp); v.y=rfloat(fp); v.z=rfloat(fp); v.w=rfloat(fp); return v; }
int main (){ /* * Make and display */ std::srand(std::time(0)); std::list<pkt_time> window; int i = 0; for (i = 0; i < 10; i++){ window.emplace_back(i,rfloat(i),rfloat(i+2)); //avoid dobule construction } std::cout << "Front " << window.front().seq << std::endl; std::cout << "Back " << window.back().seq << std::endl; /* * Find the min duration */ pkt_time min(0,0,0,0); //inplace declaration min = window.front(); //copy assignment for (const pkt_time& t : window){ if (t < min){ min = t; } } std::cout << "Min was " << min.dur << " Seq #" << min.seq << std::endl; /* * Iterate over the list forwards */ std::cout << "Forward Iteration" << std::endl; for (const pkt_time& t : window) //rangebased interation with const reference std::cout << "Seq " << t.seq << " Dur " << t.dur << std::endl; /* * Truncate upto a value */ // while (window.back().seq - window.front().seq > 3) //truncate the list // window.pop_front(); std::cout << "Truncate if sequence number is further than 3 from max" << std::endl; window.remove_if([&window](pkt_time& t) {return window.back().seq - t.seq > 3;}); std::cout << "Front " << window.front().seq << std::endl; std::cout << "Back " << window.back().seq << std::endl; /* * Iterate over the list backwards */ std::cout << "Reverse Iteration after truncation" << std::endl; for (auto t = window.rbegin(); t != window.rend(); ++t) // iterate backwards std::cout << "Seq " << t->seq << " Dur " << t->dur << std::endl; }
mat4f rmat4f(FILE *fp) { mat4f m; for(int i=0;i<16;i++) m[i]=rfloat(fp); return m; }
void initialize_noise_1d_inplace(noise* n, unsigned count) { n->count = count; for(unsigned i = 0; i < count; i++) { n->values[i] = rfloat(); //printf("%f %d %d %f\n", n->values[i], rand(), RAND_MAX, rand() / (float)RAND_MAX); } }
void points_arc(Arc a, int *sx, int *sy, int *ex, int *ey) { int cx = valInt(a->position->x); int cy = valInt(a->position->y); float start = valReal(a->start_angle); float size = valReal(a->size_angle); if ( sx ) *sx = cx + rfloat((float) valInt(a->size->w) * COS(start)); if ( sy ) *sy = cy - rfloat((float) valInt(a->size->h) * SIN(start)); if ( ex ) *ex = cx + rfloat((float) valInt(a->size->w) * COS(start + size)); if ( ey ) *ey = cy - rfloat((float) valInt(a->size->h) * SIN(start + size)); }
static status angleInArc(Arc a, int angle) { int start = rfloat(valReal(a->start_angle)); int size = rfloat(valReal(a->size_angle)); if ( size < 0 ) { start += size; size = -size; } while(start < 0) start += 360; start = start % 360; if ( (angle >= start && angle <= start + size) || (angle < start && angle <= start + size - 360) ) succeed; fail; }
int PseudoNoise::rint1(int i, int mmn ) { return mmn * rfloat(i); }
inline static Int compare(Term t1, Term t2) /* compare terms t1 and t2 */ { if (t1 == t2) return 0; if (IsVarTerm(t1)) { if (IsVarTerm(t2)) return Signed(t1) - Signed(t2); return -1; } else if (IsVarTerm(t2)) { /* get rid of variables */ return 1; } if (IsAtomOrIntTerm(t1)) { if (IsAtomTerm(t1)) { if (IsAtomTerm(t2)) return cmp_atoms(AtomOfTerm(t1), AtomOfTerm(t2)); if (IsPrimitiveTerm(t2)) return 1; if (IsStringTerm(t2)) return 1; return -1; } else { if (IsIntTerm(t2)) { return IntOfTerm(t1) - IntOfTerm(t2); } if (IsApplTerm(t2)) { Functor fun2 = FunctorOfTerm(t2); switch ((CELL)fun2) { case double_e: return 1; case long_int_e: return IntOfTerm(t1) - LongIntOfTerm(t2); #ifdef USE_GMP case big_int_e: return Yap_gmp_tcmp_int_big(IntOfTerm(t1), t2); #endif case db_ref_e: return 1; case string_e: return -1; } } return -1; } } else if (IsPairTerm(t1)) { if (IsApplTerm(t2)) { Functor f = FunctorOfTerm(t2); if (IsExtensionFunctor(f)) return 1; else { if (f != FunctorDot) return strcmp(".", RepAtom(NameOfFunctor(f))->StrOfAE); else { return compare_complex(RepPair(t1) - 1, RepPair(t1) + 1, RepAppl(t2)); } } } if (IsPairTerm(t2)) { return ( compare_complex(RepPair(t1) - 1, RepPair(t1) + 1, RepPair(t2) - 1)); } else return 1; } else { /* compound term */ Functor fun1 = FunctorOfTerm(t1); if (IsExtensionFunctor(fun1)) { /* float, long, big, dbref */ switch ((CELL)fun1) { case double_e: { if (IsFloatTerm(t2)) return (rfloat(FloatOfTerm(t1) - FloatOfTerm(t2))); if (IsRefTerm(t2)) return 1; return -1; } case long_int_e: { if (IsIntTerm(t2)) return LongIntOfTerm(t1) - IntOfTerm(t2); if (IsFloatTerm(t2)) { return 1; } if (IsLongIntTerm(t2)) return LongIntOfTerm(t1) - LongIntOfTerm(t2); #ifdef USE_GMP if (IsBigIntTerm(t2)) { return Yap_gmp_tcmp_int_big(LongIntOfTerm(t1), t2); } #endif if (IsRefTerm(t2)) return 1; return -1; } #ifdef USE_GMP case big_int_e: { if (IsIntTerm(t2)) return Yap_gmp_tcmp_big_int(t1, IntOfTerm(t2)); if (IsFloatTerm(t2)) { return 1; } if (IsLongIntTerm(t2)) return Yap_gmp_tcmp_big_int(t1, LongIntOfTerm(t2)); if (IsBigIntTerm(t2)) { return Yap_gmp_tcmp_big_big(t1, t2); } if (IsRefTerm(t2)) return 1; return -1; } #endif case string_e: { if (IsApplTerm(t2)) { Functor fun2 = FunctorOfTerm(t2); switch ((CELL)fun2) { case double_e: return 1; case long_int_e: return 1; #ifdef USE_GMP case big_int_e: return 1; #endif case db_ref_e: return 1; case string_e: return strcmp((char *)StringOfTerm(t1), (char *)StringOfTerm(t2)); } return -1; } return -1; } case db_ref_e: if (IsRefTerm(t2)) return Unsigned(RefOfTerm(t2)) - Unsigned(RefOfTerm(t1)); return -1; } } if (!IsApplTerm(t2)) { if (IsPairTerm(t2)) { Int out; Functor f = FunctorOfTerm(t1); if (!(out = ArityOfFunctor(f)) - 2) out = strcmp((char *)RepAtom(NameOfFunctor(f))->StrOfAE, "."); return out; } return 1; } else { Functor fun2 = FunctorOfTerm(t2); Int r; if (IsExtensionFunctor(fun2)) { return 1; } r = ArityOfFunctor(fun1) - ArityOfFunctor(fun2); if (r) return r; r = cmp_atoms(NameOfFunctor(fun1), NameOfFunctor(fun2)); if (r) return r; else return (compare_complex(RepAppl(t1), RepAppl(t1) + ArityOfFunctor(fun1), RepAppl(t2))); } } }
static Int compare_complex(register CELL *pt0, register CELL *pt0_end, register CELL *pt1) { CACHE_REGS register CELL **to_visit = (CELL **)HR; register Int out = 0; loop: while (pt0 < pt0_end) { register CELL d0, d1; ++pt0; ++pt1; d0 = Derefa(pt0); d1 = Derefa(pt1); if (IsVarTerm(d0)) { if (IsVarTerm(d1)) { out = Signed(d0) - Signed(d1); if (out) goto done; } else { out = -1; goto done; } } else if (IsVarTerm(d1)) { out = 1; goto done; } else { if (d0 == d1) continue; else if (IsAtomTerm(d0)) { if (IsAtomTerm(d1)) out = cmp_atoms(AtomOfTerm(d0), AtomOfTerm(d1)); else if (IsPrimitiveTerm(d1)) out = 1; else out = -1; /* I know out must be != 0 */ goto done; } else if (IsIntTerm(d0)) { if (IsIntTerm(d1)) out = IntOfTerm(d0) - IntOfTerm(d1); else if (IsFloatTerm(d1)) { out = 1; } else if (IsLongIntTerm(d1)) { out = IntOfTerm(d0) - LongIntOfTerm(d1); #ifdef USE_GMP } else if (IsBigIntTerm(d1)) { out = Yap_gmp_tcmp_int_big(IntOfTerm(d0), d1); #endif } else if (IsRefTerm(d1)) out = 1; else out = -1; if (out != 0) goto done; } else if (IsFloatTerm(d0)) { if (IsFloatTerm(d1)) { out = rfloat(FloatOfTerm(d0) - FloatOfTerm(d1)); } else if (IsRefTerm(d1)) { out = 1; } else { out = -1; } if (out != 0) goto done; } else if (IsStringTerm(d0)) { if (IsStringTerm(d1)) { out = strcmp((char *)StringOfTerm(d0), (char *)StringOfTerm(d1)); } else if (IsIntTerm(d1)) out = 1; else if (IsFloatTerm(d1)) { out = 1; } else if (IsLongIntTerm(d1)) { out = 1; #ifdef USE_GMP } else if (IsBigIntTerm(d1)) { out = 1; #endif } else if (IsRefTerm(d1)) { out = 1; } else { out = -1; } if (out != 0) goto done; } else if (IsLongIntTerm(d0)) { if (IsIntTerm(d1)) out = LongIntOfTerm(d0) - IntOfTerm(d1); else if (IsFloatTerm(d1)) { out = 1; } else if (IsLongIntTerm(d1)) { out = LongIntOfTerm(d0) - LongIntOfTerm(d1); #ifdef USE_GMP } else if (IsBigIntTerm(d1)) { out = Yap_gmp_tcmp_int_big(LongIntOfTerm(d0), d1); #endif } else if (IsRefTerm(d1)) { out = 1; } else { out = -1; } if (out != 0) goto done; } #ifdef USE_GMP else if (IsBigIntTerm(d0)) { if (IsIntTerm(d1)) { out = Yap_gmp_tcmp_big_int(d0, IntOfTerm(d1)); } else if (IsFloatTerm(d1)) { out = 1; } else if (IsLongIntTerm(d1)) { out = Yap_gmp_tcmp_big_int(d0, LongIntOfTerm(d1)); } else if (IsBigIntTerm(d1)) { out = Yap_gmp_tcmp_big_big(d0, d1); } else if (IsRefTerm(d1)) out = 1; else out = -1; if (out != 0) goto done; } #endif else if (IsPairTerm(d0)) { if (!IsPairTerm(d1)) { if (IsApplTerm(d1)) { Functor f = FunctorOfTerm(d1); if (IsExtensionFunctor(f)) out = 1; else if (!(out = 2 - ArityOfFunctor(f))) out = strcmp(".", (char *)RepAtom(NameOfFunctor(f))->StrOfAE); } else out = 1; goto done; } #ifdef RATIONAL_TREES to_visit[0] = pt0; to_visit[1] = pt0_end; to_visit[2] = pt1; to_visit[3] = (CELL *)*pt0; to_visit += 4; *pt0 = d1; #else /* store the terms to visit */ if (pt0 < pt0_end) { to_visit[0] = pt0; to_visit[1] = pt0_end; to_visit[2] = pt1; to_visit += 3; } #endif pt0 = RepPair(d0) - 1; pt0_end = RepPair(d0) + 1; pt1 = RepPair(d1) - 1; continue; } else if (IsRefTerm(d0)) { if (IsRefTerm(d1)) out = Unsigned(RefOfTerm(d1)) - Unsigned(RefOfTerm(d0)); else out = -1; goto done; } else if (IsApplTerm(d0)) { register Functor f; register CELL *ap2, *ap3; if (!IsApplTerm(d1)) { out = 1; goto done; } else { /* store the terms to visit */ Functor f2; ap2 = RepAppl(d0); ap3 = RepAppl(d1); f = (Functor)(*ap2); if (IsExtensionFunctor(f)) { out = 1; goto done; } f2 = (Functor)(*ap3); if (IsExtensionFunctor(f2)) { out = -1; goto done; } /* compare functors */ if (f != (Functor)*ap3) { if (!(out = ArityOfFunctor(f) - ArityOfFunctor(f2))) out = cmp_atoms(NameOfFunctor(f), NameOfFunctor(f2)); goto done; } #ifdef RATIONAL_TREES to_visit[0] = pt0; to_visit[1] = pt0_end; to_visit[2] = pt1; to_visit[3] = (CELL *)*pt0; to_visit += 4; *pt0 = d1; #else /* store the terms to visit */ if (pt0 < pt0_end) { to_visit[0] = pt0; to_visit[1] = pt0_end; to_visit[2] = pt1; to_visit += 3; } #endif d0 = ArityOfFunctor(f); pt0 = ap2; pt0_end = ap2 + d0; pt1 = ap3; continue; } } } } /* Do we still have compound terms to visit */ if (to_visit > (CELL **)HR) { #ifdef RATIONAL_TREES to_visit -= 4; pt0 = to_visit[0]; pt0_end = to_visit[1]; pt1 = to_visit[2]; *pt0 = (CELL)to_visit[3]; #else to_visit -= 3; pt0 = to_visit[0]; pt0_end = to_visit[1]; pt1 = to_visit[2]; #endif goto loop; } done: /* failure */ #ifdef RATIONAL_TREES while (to_visit > (CELL **)HR) { to_visit -= 4; pt0 = to_visit[0]; pt0_end = to_visit[1]; pt1 = to_visit[2]; *pt0 = (CELL)to_visit[3]; } #endif return (out); }
vec2 symmetricUnitBall() { return vScale(rfloat(), uniformUnitCirc()); }
float uniformFloat(float f0, float f1) { assert(f0 <= f1); return rfloat() * (f1 - f0) + f0; }
static status RedrawAreaSlider(Slider s, Area a) { int x, y, w, h; int ny, vx, vy, lx, ly, sx, sy, hx, hy; int vv; int bw = (s->look == NAME_x ? BAR_WIDTH : OL_BOX_WIDTH); float lv = convert_value(s->low); float hv = convert_value(s->high); float dv = convert_value(s->displayed_value); int lflags = (s->active == ON ? 0 : LABEL_INACTIVE); if ( dv < lv ) dv = lv; else if ( dv > hv ) dv = hv; if ( hv > lv ) vv = rfloat(((float) (valInt(s->width) - bw) * (dv - lv)) / (hv - lv)); else vv = 0; initialiseDeviceGraphical(s, &x, &y, &w, &h); NormaliseArea(x, y, w, h); r_thickness(valInt(s->pen)); r_dash(s->texture); compute_slider(s, &ny, &vx, &vy, &lx, &ly, &sx, &sy, &hx, &hy); r_clear(x, y, w, h); if ( s->show_label == ON ) { int ex = valInt(getExFont(s->label_font)); RedrawLabelDialogItem(s, accelerator_code(s->accelerator), x, y+ny, vx-ex, 0, s->label_format, NAME_top, lflags); } if ( s->look == NAME_motif ) { int by = y+sy+(SLIDER_HEIGHT-OL_BAR_HEIGHT)/2; int ex = x + sx + valInt(s->width); Elevation z = getClassVariableValueObject(s, NAME_elevation); r_3d_box(x+sx, by, vv, OL_BAR_HEIGHT, 0, z, FALSE); r_3d_box(x+sx+vv+bw, by, ex-(x+sx+vv+bw), OL_BAR_HEIGHT, 0, z, FALSE); r_3d_box(x+sx+vv, y+sy, bw, SLIDER_HEIGHT, 0, z, TRUE); } else if ( s->look == NAME_openLook ) { int by = y+sy+(SLIDER_HEIGHT-OL_BAR_HEIGHT)/2; int ly2 = by+OL_BAR_HEIGHT-1; int ex = x + sx + valInt(s->width); r_fill(x+sx, by+1, 1, OL_BAR_HEIGHT-2, BLACK_IMAGE); r_fill(x+sx+1, by, vv-2, OL_BAR_HEIGHT, BLACK_IMAGE); r_line(x+sx+1+vv+bw, by, ex-2, by); r_line(x+sx+1+vv+bw, ly2, ex-2, ly2); r_line(ex-1, by+1, ex-1, ly2-1); r_shadow_box(x+sx+vv, y+sy, bw, SLIDER_HEIGHT, 0, 1, NIL); } else { r_fill(x+sx, y+sy, vv, SLIDER_HEIGHT, GREY50_IMAGE); r_box(x+sx, y+sy, valInt(s->width), SLIDER_HEIGHT, 0, NIL); r_fill(x+sx+vv, y+sy, bw, SLIDER_HEIGHT, BLACK_IMAGE); } if ( s->show_value == ON ) { char buf[100]; string str; buf[0] = '['; format_value(s, &buf[1], s->displayed_value); strcat(buf, "]"); str_set_ascii(&str, buf); str_label(&str, 0, s->value_font, x+vx, y+vy, 0, 0, NAME_left, NAME_top, lflags); format_value(s, buf, s->low); str_set_ascii(&str, buf); str_label(&str, 0, s->value_font, x+lx, y+ly, 0, 0, NAME_left, NAME_top, lflags); format_value(s, buf, s->high); str_set_ascii(&str, buf); str_label(&str, 0, s->value_font, x+hx, y+hy, 0, 0, NAME_left, NAME_top, lflags); } return RedrawAreaGraphical(s, a); }
static status RedrawAreaArc(Arc a, Area area) { int x, y, w, h; int aw = valInt(a->size->w); int ah = valInt(a->size->h); int sx, sy, ex, ey; int cx, cy; initialiseDeviceGraphical(a, &x, &y, &w, &h); points_arc(a, &sx, &sy, &ex, &ey); cx = valInt(a->position->x); cy = valInt(a->position->y); r_thickness(valInt(a->pen)); r_dash(a->texture); #ifndef WIN32_GRAPHICS r_arcmode(a->close == NAME_none ? NAME_pieSlice : a->close); r_arc(valInt(a->position->x) - aw, valInt(a->position->y) - ah, 2*aw, 2*ah, rfloat(valReal(a->start_angle)*64.0), rfloat(valReal(a->size_angle)*64.0), a->fill_pattern); if ( a->close != NAME_none && a->pen != ZERO ) { if ( a->close == NAME_chord ) { r_line(sx, sy, ex, ey); } else /* if ( a->close == NAME_pieSlice ) */ { r_line(cx, cy, sx, sy); r_line(cx, cy, ex, ey); } } #else /*WIN32_GRAPHICS*/ { int ax, ay, bx, by; double sa = valReal(a->size_angle); int large; if ( sa >= 0.0 ) { ax = sx, ay = sy, bx = ex, by = ey; large = (sa >= 180.0); } else { ax = ex, ay = ey, bx = sx, by = sy; large = (sa <= -180.0); } r_msarc(valInt(a->position->x) - aw, valInt(a->position->y) - ah, 2*aw, 2*ah, ax, ay, bx, by, large, a->close, a->fill_pattern); } #endif /* __WINDOWS__ */ if (notNil(a->first_arrow)) { Any av[4]; av[0] = toInt(sx); av[1] = toInt(sy); if ( valReal(a->size_angle) >= 0.0 ) { av[2] = toInt(sx+(sy-cy)); av[3] = toInt(sy-(sx-cx)); } else { av[2] = toInt(sx-(sy-cy)); av[3] = toInt(sy+(sx-cx)); } if ( qadSendv(a->first_arrow, NAME_points, 4, av) ) { assign(a->first_arrow, displayed, ON); ComputeGraphical(a->first_arrow); RedrawArea(a->first_arrow, area); } } if (notNil(a->second_arrow)) { Any av[4]; av[0] = toInt(ex); av[1] = toInt(ey); if ( valReal(a->size_angle) >= 0.0 ) { av[2] = toInt(ex-(ey-cy)); av[3] = toInt(ey+(ex-cx)); } else { av[2] = toInt(ex+(ey-cy)); av[3] = toInt(ey-(ex-cx)); } if ( qadSendv(a->second_arrow, NAME_points, 4, av) ) { assign(a->second_arrow, displayed, ON); ComputeGraphical(a->second_arrow); RedrawArea(a->second_arrow, area); } } return RedrawAreaGraphical(a, area); }
int PseudoNoise::rint2(int i, int j, int mmn) { return mmn * rfloat(i - j); }
int PseudoNoise::rint3(int i, int j, int k, int mmn) { return mmn * rfloat(i - j + k); }
vec2 symmetricBall(float radius) { return vScale(rfloat() * radius, uniformUnitCirc()); }