Use C++ std::{min,max,swap} instead of custom ones.

The main benefit is that std::swap will ensure that the type
of arguments is copy-constructible and move-constructible.
It is more concise as well.

When min and max are defined as macros, they will conflict
with STL header files included by other C++ libraries;
in this case STL will #undef any other definition.
pull/4/head
whitequark 2015-03-27 18:43:28 +03:00
parent 063dfc4f98
commit 28166e6200
24 changed files with 56 additions and 59 deletions

View File

@ -25,7 +25,7 @@ SBsp3 *SBsp3::FromMesh(SMesh *m) {
while(n > 1) {
int k = rand() % n;
n--;
SWAP(STriangle, mc.l.elem[k], mc.l.elem[n]);
swap(mc.l.elem[k], mc.l.elem[n]);
}
for(i = 0; i < mc.l.n; i++) {

View File

@ -316,7 +316,7 @@ void TextWindow::ShowConfiguration(void) {
bool TextWindow::EditControlDoneForConfiguration(const char *s) {
switch(edit.meaning) {
case EDIT_LIGHT_INTENSITY:
SS.lightIntensity[edit.i] = min(1, max(0, atof(s)));
SS.lightIntensity[edit.i] = min(1.0, max(0.0, atof(s)));
InvalidateGraphics();
break;
@ -341,7 +341,7 @@ bool TextWindow::EditControlDoneForConfiguration(const char *s) {
break;
}
case EDIT_CHORD_TOLERANCE: {
SS.chordTol = min(10, max(0.1, atof(s)));
SS.chordTol = min(10.0, max(0.1, atof(s)));
SS.GenerateAll(0, INT_MAX);
break;
}
@ -351,7 +351,7 @@ bool TextWindow::EditControlDoneForConfiguration(const char *s) {
break;
}
case EDIT_CAMERA_TANGENT: {
SS.cameraTangent = (min(2, max(0, atof(s))))/1000.0;
SS.cameraTangent = (min(2.0, max(0.0, atof(s))))/1000.0;
if(!SS.usePerspectiveProj) {
Message("The perspective factor will have no effect until you "
"enable View -> Use Perspective Projection.");

View File

@ -369,7 +369,7 @@ void Constraint::MenuConstrain(int id) {
if((l1->group.v != SS.GW.activeGroup.v) ||
(l1->construction && !(l0->construction)))
{
SWAP(Entity *, l0, l1);
swap(l0, l1);
}
c.ptA = l1->point[0];
c.ptB = l1->point[1];
@ -476,7 +476,7 @@ void Constraint::MenuConstrain(int id) {
Entity *nfree = SK.GetEntity(c.entityA);
Entity *nref = SK.GetEntity(c.entityB);
if(nref->group.v == SS.GW.activeGroup.v) {
SWAP(Entity *, nref, nfree);
swap(nref, nfree);
}
if(nfree->group.v == SS.GW.activeGroup.v &&
nref ->group.v != SS.GW.activeGroup.v)
@ -491,7 +491,7 @@ void Constraint::MenuConstrain(int id) {
// There might be an odd*90 degree rotation about the
// normal vector; allow that, since the numerical
// constraint does
SWAP(Vector, ru, rv);
swap(ru, rv);
}
fu = fu.Dot(ru) > 0 ? ru : ru.ScaledBy(-1);
fv = fv.Dot(rv) > 0 ? rv : rv.ScaledBy(-1);
@ -582,7 +582,7 @@ void Constraint::MenuConstrain(int id) {
Entity *line = SK.GetEntity(gs.entity[0]);
Entity *arc = SK.GetEntity(gs.entity[1]);
if(line->type == Entity::ARC_OF_CIRCLE) {
SWAP(Entity *, line, arc);
swap(line, arc);
}
Vector l0 = SK.GetEntity(line->point[0])->PointGetNum(),
l1 = SK.GetEntity(line->point[1])->PointGetNum();
@ -606,7 +606,7 @@ void Constraint::MenuConstrain(int id) {
Entity *line = SK.GetEntity(gs.entity[0]);
Entity *cubic = SK.GetEntity(gs.entity[1]);
if(line->type == Entity::CUBIC) {
SWAP(Entity *, line, cubic);
swap(line, cubic);
}
Vector l0 = SK.GetEntity(line->point[0])->PointGetNum(),
l1 = SK.GetEntity(line->point[1])->PointGetNum();

View File

@ -592,7 +592,7 @@ void ConstraintBase::GenerateReal(IdList<Equation,hEquation> *l) {
EntityBase *a = SK.GetEntity(entityA);
EntityBase *b = SK.GetEntity(entityB);
if(b->group.v != group.v) {
SWAP(EntityBase *, a, b);
swap(a, b);
}
ExprVector au = a->NormalExprsU(),
@ -744,7 +744,7 @@ void ConstraintBase::GenerateReal(IdList<Equation,hEquation> *l) {
case PARALLEL: {
EntityBase *ea = SK.GetEntity(entityA), *eb = SK.GetEntity(entityB);
if(eb->group.v != group.v) {
SWAP(EntityBase *, ea, eb);
swap(ea, eb);
}
ExprVector a = ea->VectorGetExprs();
ExprVector b = eb->VectorGetExprs();

View File

@ -727,7 +727,7 @@ void Constraint::DrawOrGetDistance(Vector *labelPos) {
// a bit of bias to stop it from flickering between the
// two possibilities
if(fabs(u.Dot(ru)) < fabs(v.Dot(ru)) + LENGTH_EPS) {
SWAP(Vector, u, v);
swap(u, v);
}
if(u.Dot(ru) < 0) u = u.ScaledBy(-1);
}

View File

@ -216,7 +216,7 @@ public:
void Reverse(void) {
int i;
for(i = 0; i < (n/2); i++) {
SWAP(T, elem[i], elem[(n-1)-i]);
swap(elem[i], elem[(n-1)-i]);
}
}
};

View File

@ -48,7 +48,7 @@ void SolveSpaceUI::ExportSectionTo(const char *filename) {
vt = vt.WithMagnitude(1);
if(fabs(SS.GW.projUp.Dot(vt)) < fabs(SS.GW.projUp.Dot(ut))) {
SWAP(Vector, ut, vt);
swap(ut, vt);
}
if(SS.GW.projRight.Dot(ut) < 0) ut = ut.ScaledBy(-1);
if(SS.GW.projUp. Dot(vt) < 0) vt = vt.ScaledBy(-1);
@ -280,11 +280,11 @@ void SolveSpaceUI::ExportLinesAndMesh(SEdgeList *sel, SBezierList *sbl, SMesh *s
// And calculate lighting for the triangle
Vector n = tt.Normal().WithMagnitude(1);
double lighting = SS.ambientIntensity +
max(0, (SS.lightIntensity[0])*(n.Dot(l0))) +
max(0, (SS.lightIntensity[1])*(n.Dot(l1)));
double r = min(1, tt.meta.color.redF() *lighting),
g = min(1, tt.meta.color.greenF()*lighting),
b = min(1, tt.meta.color.blueF() *lighting);
max(0.0, (SS.lightIntensity[0])*(n.Dot(l0))) +
max(0.0, (SS.lightIntensity[1])*(n.Dot(l1)));
double r = min(1.0, tt.meta.color.redF() * lighting),
g = min(1.0, tt.meta.color.greenF() * lighting),
b = min(1.0, tt.meta.color.blueF() * lighting);
tt.meta.color = RGBf(r, g, b);
smp.AddTriangle(&tt);
}

View File

@ -112,7 +112,7 @@ void DxfFileWriter::Bezier(SBezier *sb) {
theta1 = atan2(sb->ctrl[2].y - c.y, sb->ctrl[2].x - c.x),
dtheta = WRAP_SYMMETRIC(theta1 - theta0, 2*PI);
if(dtheta < 0) {
SWAP(double, theta0, theta1);
swap(theta0, theta1);
}
fprintf(f,

View File

@ -384,7 +384,7 @@ void GraphicsWindow::ZoomToFit(bool includingInvisibles) {
if(EXACT(dy != 0)) scaley = 0.9*height/dy;
scale = min(scalex, scaley);
scale = min(300, scale);
scale = min(300.0, scale);
scale = max(0.003, scale);
}

View File

@ -97,7 +97,7 @@ void Group::MenuGroup(int id) {
vt = vt.WithMagnitude(1);
if(fabs(SS.GW.projUp.Dot(vt)) < fabs(SS.GW.projUp.Dot(ut))) {
SWAP(Vector, ut, vt);
swap(ut, vt);
g.predef.swapUV = true;
}
if(SS.GW.projRight.Dot(ut) < 0) g.predef.negateU = true;
@ -317,7 +317,7 @@ void Group::Generate(IdList<Entity,hEntity> *entity,
Vector n = u.Cross(v);
v = (n.Cross(u)).WithMagnitude(1);
if(predef.swapUV) SWAP(Vector, u, v);
if(predef.swapUV) swap(u, v);
if(predef.negateU) u = u.ScaledBy(-1);
if(predef.negateV) v = v.ScaledBy(-1);
q = Quaternion::From(u, v);

View File

@ -149,7 +149,7 @@ void Group::GenerateForStepAndRepeat(T *steps, T *outs) {
scratch->MakeFromUnionOf(soFar, &transd);
}
SWAP(T *, scratch, soFar);
swap(scratch, soFar);
scratch->Clear();
transd.Clear();
}

View File

@ -312,7 +312,7 @@ void SMesh::MakeFromTransformationOf(SMesh *a,
tt.c = (tt.c).ScaledBy(scale);
if(scale < 0) {
// The mirroring would otherwise turn a closed mesh inside out.
SWAP(Vector, tt.a, tt.b);
swap(tt.a, tt.b);
}
tt.a = (q.Rotate(tt.a)).Plus(trans);
tt.b = (q.Rotate(tt.b)).Plus(trans);
@ -373,7 +373,7 @@ SKdNode *SKdNode::From(SMesh *m) {
while(n > 1) {
int k = rand() % n;
n--;
SWAP(STriangle, tra[k], tra[n]);
swap(tra[k], tra[n]);
}
STriangleLl *tll = NULL;

View File

@ -96,7 +96,7 @@ void GraphicsWindow::ParametricCurve::MakeFromEntity(hEntity he, bool reverse) {
p0 = e->EndpointStart(),
p1 = e->EndpointFinish();
if(reverse) {
SWAP(Vector, p0, p1);
swap(p0, p1);
}
} else if(e->type == Entity::ARC_OF_CIRCLE) {
isLine = false;
@ -105,7 +105,7 @@ void GraphicsWindow::ParametricCurve::MakeFromEntity(hEntity he, bool reverse) {
r = (pe.Minus(p0)).Magnitude();
e->ArcGetAngles(&theta0, &theta1, &dtheta);
if(reverse) {
SWAP(double, theta0, theta1);
swap(theta0, theta1);
dtheta = -dtheta;
}
EntityBase *wrkpln = SK.GetEntity(e->workplane)->Normal();

View File

@ -43,8 +43,8 @@ bool STriangle::ContainsPointProjd(Vector n, Vector p) {
}
void STriangle::FlipNormal(void) {
SWAP(Vector, a, b);
SWAP(Vector, an, bn);
swap(a, b);
swap(an, bn);
}
STriangle STriangle::From(STriMeta meta, Vector a, Vector b, Vector c) {

View File

@ -16,6 +16,7 @@
#include <stdarg.h>
#include <math.h>
#include <limits.h>
#include <algorithm>
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
@ -51,19 +52,16 @@
abort(); } while(0)
#endif
#ifndef min
# define min(x, y) ((x) < (y) ? (x) : (y))
#endif
#ifndef max
# define max(x, y) ((x) > (y) ? (x) : (y))
#endif
#ifndef isnan
# define isnan(x) (((x) != (x)) || (x > 1e11) || (x < -1e11))
#endif
namespace SolveSpace {
using std::min;
using std::max;
using std::swap;
inline int WRAP(int v, int n) {
// Clamp it to the range [0, n)
while(v >= n) v -= n;
@ -86,7 +84,6 @@ inline double WRAP_SYMMETRIC(double v, double n) {
// Why is this faster than the library function?
inline double ffabs(double v) { return (v > 0) ? v : (-v); }
#define SWAP(T, a, b) do { T temp = (a); (a) = (b); (b) = temp; } while(0)
#define ZERO(v) memset((v), 0, sizeof(*(v)))
#define CO(v) (v).x, (v).y, (v).z

View File

@ -277,7 +277,7 @@ static void DEBUGEDGELIST(SEdgeList *sel, SSurface *surf) {
for(se = sel->l.First(); se; se = sel->l.NextAfter(se)) {
Vector mid = (se->a).Plus(se->b).ScaledBy(0.5);
Vector arrow = (se->b).Minus(se->a);
SWAP(double, arrow.x, arrow.y);
swap(arrow.x, arrow.y);
arrow.x *= -1;
arrow = arrow.WithMagnitude(0.01);
arrow = arrow.Plus(mid);

View File

@ -74,8 +74,8 @@ Vector SBezier::Finish(void) {
void SBezier::Reverse(void) {
int i;
for(i = 0; i < (deg+1)/2; i++) {
SWAP(Vector, ctrl[i], ctrl[deg-i]);
SWAP(double, weight[i], weight[deg-i]);
swap(ctrl[i], ctrl[deg-i]);
swap(weight[i], weight[deg-i]);
}
}

View File

@ -471,9 +471,9 @@ bool SShell::ClassifyEdge(int *indir, int *outdir,
}
if(fabs(dotp[1]) < DOTP_TOL) {
SWAP(double, dotp[0], dotp[1]);
SWAP(Vector, inter_surf_n[0], inter_surf_n[1]);
SWAP(Vector, inter_edge_n[0], inter_edge_n[1]);
swap(dotp[0], dotp[1]);
swap(inter_surf_n[0], inter_surf_n[1]);
swap(inter_edge_n[0], inter_edge_n[1]);
}
int coinc = (surf_n.Dot(inter_surf_n[0])) > 0 ? COINC_SAME : COINC_OPP;

View File

@ -459,15 +459,15 @@ void SSurface::Reverse(void) {
int i, j;
for(i = 0; i < (degm+1)/2; i++) {
for(j = 0; j <= degn; j++) {
SWAP(Vector, ctrl[i][j], ctrl[degm-i][j]);
SWAP(double, weight[i][j], weight[degm-i][j]);
swap(ctrl[i][j], ctrl[degm-i][j]);
swap(weight[i][j], weight[degm-i][j]);
}
}
STrimBy *stb;
for(stb = trim.First(); stb; stb = trim.NextAfter(stb)) {
stb->backwards = !stb->backwards;
SWAP(Vector, stb->start, stb->finish);
swap(stb->start, stb->finish);
}
}
@ -494,7 +494,7 @@ void SShell::MakeFromExtrusionOf(SBezierLoopSet *sbls, Vector t0, Vector t1, Rgb
// Make the extrusion direction consistent with respect to the normal
// of the sketch we're extruding.
if((t0.Minus(t1)).Dot(sbls->normal) < 0) {
SWAP(Vector, t0, t1);
swap(t0, t1);
}
// Define a coordinate system to contain the original sketch, and get
@ -780,8 +780,8 @@ void SShell::MakeFromRevolutionOf(SBezierLoopSet *sbls, Vector pt, Vector axis,
srf->weight[1][1] = 1;
if(oldn.Dot(srf->NormalAt(0.5, 0.5)) < 0) {
SWAP(Vector, srf->ctrl[0][0], srf->ctrl[1][0]);
SWAP(Vector, srf->ctrl[0][1], srf->ctrl[1][1]);
swap(srf->ctrl[0][0], srf->ctrl[1][0]);
swap(srf->ctrl[0][1], srf->ctrl[1][1]);
}
continue;
}

View File

@ -262,8 +262,8 @@ void SSurface::IntersectAgainst(SSurface *b, SShell *agnstA, SShell *agnstB,
b_axis0 = (b->ctrl[0][0]).Dot(axis),
b_axis1 = (b->ctrl[0][1]).Dot(axis);
if(a_axis0 > a_axis1) SWAP(double, a_axis0, a_axis1);
if(b_axis0 > b_axis1) SWAP(double, b_axis0, b_axis1);
if(a_axis0 > a_axis1) swap(a_axis0, a_axis1);
if(b_axis0 > b_axis1) swap(b_axis0, b_axis1);
double ab_axis0 = max(a_axis0, b_axis0),
ab_axis1 = min(a_axis1, b_axis1);

View File

@ -659,7 +659,7 @@ bool TextWindow::EditControlDoneForStyles(const char *str) {
} else {
v = atof(str);
}
v = max(0, v);
v = max(0.0, v);
if(edit.meaning == EDIT_STYLE_TEXT_HEIGHT) {
s->textHeight = v;
} else {

View File

@ -199,9 +199,9 @@ bool System::SolveLinearSystem(double X[], double A[][MAX_UNKNOWNS],
// Swap row imax with row i
for(jp = 0; jp < n; jp++) {
SWAP(double, A[i][jp], A[imax][jp]);
swap(A[i][jp], A[imax][jp]);
}
SWAP(double, B[i], B[imax]);
swap(B[i], B[imax]);
// For rows i+1 and greater, eliminate the term in column i.
for(ip = i+1; ip < n; ip++) {

View File

@ -442,8 +442,8 @@ void TextWindow::DrawOrHitTestIcons(int how, double mx, double my)
ox += 3;
oy -= 3;
int tw = ((int)strlen(str) + 1)*CHAR_WIDTH;
ox = min(ox, (width - 25) - tw);
oy = max(oy, 5);
ox = min(ox, (double) (width - 25) - tw);
oy = max(oy, 5.0);
ssglCreateBitmapFont();
glLineWidth(1);
@ -838,7 +838,7 @@ void TextWindow::Paint(void) {
if(ltop < (scrollPos-1)) continue;
if(ltop > scrollPos+halfRows) break;
for(c = 0; c < min((width/CHAR_WIDTH)+1, MAX_COLS); c++) {
for(c = 0; c < min((width/CHAR_WIDTH)+1, (int) MAX_COLS); c++) {
int x = LEFT_MARGIN + c*CHAR_WIDTH;
int y = (ltop-scrollPos)*(LINE_HEIGHT/2) + 4;

View File

@ -442,7 +442,7 @@ bool TtfFont::LoadFontFromFile(bool nameOnly) {
uint16_t hmtxAdvanceWidth = 0;
int16_t hmtxLsb = 0;
for(i = 0; i < min(glyphs, hheaNumberOfMetrics); i++) {
for(i = 0; i < min(glyphs, (int)hheaNumberOfMetrics); i++) {
hmtxAdvanceWidth = GetUSHORT();
hmtxLsb = (int16_t)GetUSHORT();