Replaced Min/Max/Clamp/Abs with use of Qt versions of them.

Sun, 30 Aug 2015 15:01:10 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 30 Aug 2015 15:01:10 +0300
changeset 966
a834e43a57da
parent 965
d1b0aa40db91
child 967
eb586d3e1a6a

Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Renamed the other utility functions.

src/actions.cpp file | annotate | diff | comparison | revisions
src/actionsEdit.cpp file | annotate | diff | comparison | revisions
src/addObjectDialog.cpp file | annotate | diff | comparison | revisions
src/basics.cpp file | annotate | diff | comparison | revisions
src/basics.h file | annotate | diff | comparison | revisions
src/colors.cpp file | annotate | diff | comparison | revisions
src/editmodes/magicWandMode.cpp file | annotate | diff | comparison | revisions
src/editmodes/selectMode.cpp file | annotate | diff | comparison | revisions
src/glCompiler.cpp file | annotate | diff | comparison | revisions
src/glRenderer.cpp file | annotate | diff | comparison | revisions
src/ldDocument.cpp file | annotate | diff | comparison | revisions
src/ldObject.cpp file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/miscallenous.cpp file | annotate | diff | comparison | revisions
src/partDownloader.cpp file | annotate | diff | comparison | revisions
src/primitives.cpp file | annotate | diff | comparison | revisions
src/ringFinder.cpp file | annotate | diff | comparison | revisions
--- a/src/actions.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/actions.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -253,7 +253,7 @@
 			colors << obj->color();
 	}
 
-	RemoveDuplicates (colors);
+	removeDuplicates (colors);
 	CurrentDocument()->clearSelection();
 
 	for (LDObject* obj : CurrentDocument()->objects())
@@ -281,8 +281,8 @@
 			subfilenames << static_cast<LDSubfile*> (obj)->fileInfo()->name();
 	}
 
-	RemoveDuplicates (types);
-	RemoveDuplicates (subfilenames);
+	removeDuplicates (types);
+	removeDuplicates (subfilenames);
 	CurrentDocument()->clearSelection();
 
 	for (LDObject* obj : CurrentDocument()->objects())
@@ -771,7 +771,7 @@
 	// be carried over to the subfile.
 	for (LDObjectIterator<LDBFC> it (CurrentDocument()); it.isValid(); ++it)
 	{
-		if (Eq (it->statement(), BFCStatement::CertifyCCW, BFCStatement::CertifyCW, BFCStatement::NoCertify))
+		if (isOneOf (it->statement(), BFCStatement::CertifyCCW, BFCStatement::CertifyCW, BFCStatement::NoCertify))
 		{
 			bfctype = it->statement();
 			break;
--- a/src/actionsEdit.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/actionsEdit.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -687,7 +687,7 @@
 
 	for (LDObject* obj : Selection())
 	{
-		if (not Eq (obj->type(), OBJ_Line, OBJ_CondLine))
+		if (not isOneOf (obj->type(), OBJ_Line, OBJ_CondLine))
 			continue;
 
 		QVector<LDObject*> newsegs;
--- a/src/addObjectDialog.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/addObjectDialog.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -351,7 +351,8 @@
 		case OBJ_BFC:
 		{
 			LDBFC* bfc = InitObject<LDBFC> (obj);
-			if (IsWithin (dlg.rb_bfcType->value(), 0, int (BFCStatement::NumValues) - 1))
+			int value = dlg.rb_bfcType->value();
+			if (value == qBound (0, value, int (BFCStatement::NumValues) - 1))
 				bfc->setStatement (BFCStatement (dlg.rb_bfcType->value()));
 		} break;
 
--- a/src/basics.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/basics.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -277,12 +277,12 @@
 //
 void LDBoundingBox::calcVertex (const Vertex& vertex)
 {
-	m_vertex0.setX (Min (vertex.x(), m_vertex0.x()));
-	m_vertex0.setY (Min (vertex.y(), m_vertex0.y()));
-	m_vertex0.setZ (Min (vertex.z(), m_vertex0.z()));
-	m_vertex1.setX (Max (vertex.x(), m_vertex1.x()));
-	m_vertex1.setY (Max (vertex.y(), m_vertex1.y()));
-	m_vertex1.setZ (Max (vertex.z(), m_vertex1.z()));
+	m_vertex0.setX (qMin (vertex.x(), m_vertex0.x()));
+	m_vertex0.setY (qMin (vertex.y(), m_vertex0.y()));
+	m_vertex0.setZ (qMin (vertex.z(), m_vertex0.z()));
+	m_vertex1.setX (qMax (vertex.x(), m_vertex1.x()));
+	m_vertex1.setY (qMax (vertex.y(), m_vertex1.y()));
+	m_vertex1.setZ (qMax (vertex.z(), m_vertex1.z()));
 	setEmpty (false);
 }
 
@@ -312,8 +312,8 @@
 	elif (yscale > zscale)
 		size = yscale;
 
-	if (Abs (size) >= 2.0)
-		return Abs (size / 2);
+	if (qAbs (size) >= 2.0)
+		return qAbs (size / 2);
 
 	return 1.0;
 }
--- a/src/basics.h	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/basics.h	Sun Aug 30 15:01:10 2015 +0300
@@ -199,93 +199,44 @@
 // =============================================================================
 // Plural expression
 template<typename T>
-static inline const char* Plural (T n)
+static inline const char* plural (T n)
 {
 	return (n != 1) ? "s" : "";
 }
 
-// =============================================================================
-// Templated clamp
 template<typename T>
-static inline T Clamp (T a, T min, T max)
-{
-	return (a > max) ? max : (a < min) ? min : a;
-}
-
-// Templated minimum
-template<typename T>
-static inline T Min (T a, T b)
+bool isZero (T a)
 {
-	return (a < b) ? a : b;
-}
-
-// Templated maximum
-template<typename T>
-static inline T Max (T a, T b)
-{
-	return (a > b) ? a : b;
-}
-
-// Templated absolute value
-template<typename T>
-static inline T Abs (T a)
-{
-	return (a >= 0) ? a : -a;
+	return qFuzzyCompare (a + 1.0, 1.0);
 }
 
 template<typename T>
-inline bool IsZero (T a)
-{
-	return Abs<T> (a) < 0.00001;
-}
-
-template<typename T>
-inline bool IsIntegral (T a)
+bool isInteger (T a)
 {
-	return (Abs (a - floor(a)) < 0.00001) or (Abs (a - ceil(a)) < 0.00001);
-}
-
-template<typename T>
-inline bool IsWithin (T a, T b, T c)
-{
-	return a >= b and a <= c;
+	return (qAbs (a - floor(a)) < 0.00001) or (qAbs (a - ceil(a)) < 0.00001);
 }
 
 template<typename T>
-void RemoveDuplicates (T& a)
+void removeDuplicates (T& a)
 {
 	std::sort (a.begin(), a.end());
 	a.erase (std::unique (a.begin(), a.end()), a.end());
 }
 
-inline QString UTF16 (const char16_t* a)
-{
-	if (Q_LIKELY (sizeof(char16_t) == sizeof(unsigned short)))
-		return QString::fromUtf16 (reinterpret_cast<const unsigned short*> (a));
-
-	QVector<unsigned short> data;
-
-	for (const char16_t* ap = &a[0]; *ap != '\u0000'; ++ap)
-		data << *ap;
-
-	data << '\u0000';
-	return QString::fromUtf16 (data.constData());
-}
-
 //
 // Returns true if first arg is equal to any of the other args
 //
 template<typename T, typename Arg, typename... Args>
-bool Eq (T const& a, Arg const& arg, Args const&... args)
+bool isOneOf (T const& a, Arg const& arg, Args const&... args)
 {
 	if (a == arg)
 		return true;
 
-	return Eq (a, args...);
+	return isOneOf (a, args...);
 }
 
 template<typename T>
-bool Eq (T const&)
+bool isOneOf (T const&)
 {
 	return false;
 }
--- a/src/colors.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/colors.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -215,7 +215,7 @@
 
 		// Parse alpha if given.
 		if (pars.parseLDConfigTag ("ALPHA", valuestr))
-			alpha = Clamp (valuestr.toInt(), 0, 255);
+			alpha = qBound (0, valuestr.toInt(), 255);
 
 		ColorDataEntry& entry = ColorData[code];
 		entry.name = name;
--- a/src/editmodes/magicWandMode.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/editmodes/magicWandMode.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -46,14 +46,14 @@
 	// of candidates.
 	for (auto it = candidates.begin(); it != candidates.end(); ++it)
 	{
-		if (not Eq ((*it)->type(), OBJ_Line, OBJ_CondLine) or (*it)->vertex (0) == (*it)->vertex (1))
+		if (not isOneOf ((*it)->type(), OBJ_Line, OBJ_CondLine) or (*it)->vertex (0) == (*it)->vertex (1))
 			continue;
 
 		int matches = 0;
 
 		for (int i = 0; i < obj->numVertices(); ++i)
 		{
-			if (not Eq (obj->vertex (i), (*it)->vertex (0), (*it)->vertex (1)))
+			if (not isOneOf (obj->vertex (i), (*it)->vertex (0), (*it)->vertex (1)))
 				continue;
 
 			if (++matches == 2)
@@ -117,7 +117,7 @@
 	for (int i = 0; i < obj->numVertices(); ++i)
 		candidates += m_vertices[obj->vertex (i)];
 
-	RemoveDuplicates (candidates);
+	removeDuplicates (candidates);
 
 	// If we're dealing with surfaces, get a list of boundaries.
 	if (matchesneeded > 1)
@@ -160,8 +160,8 @@
 			// Check if a boundary gets in between the objects.
 			for (auto boundary : boundaries)
 			{
-				if (Eq (matches[0], std::get<0> (boundary), std::get<1> (boundary)) and
-					Eq (matches[1], std::get<0> (boundary), std::get<1> (boundary)))
+				if (isOneOf (matches[0], std::get<0> (boundary), std::get<1> (boundary)) and
+					isOneOf (matches[1], std::get<0> (boundary), std::get<1> (boundary)))
 				{
 					throw 0;
 				}
--- a/src/editmodes/selectMode.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/editmodes/selectMode.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -76,10 +76,10 @@
 			}
 			else
 			{
-				int const x = Min (m_rangeStart.x(), mx);
-				int const y = Min (m_rangeStart.y(), my);
-				int const width = Abs (m_rangeStart.x() - mx);
-				int const height = Abs (m_rangeStart.y() - my);
+				int const x = qMin (m_rangeStart.x(), mx);
+				int const y = qMin (m_rangeStart.y(), my);
+				int const width = qAbs (m_rangeStart.x() - mx);
+				int const height = qAbs (m_rangeStart.y() - my);
 				area = QRect (x, y, width, height);
 			}
 
--- a/src/glCompiler.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/glCompiler.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -260,7 +260,7 @@
 //
 void GLCompiler::compileStaged()
 {
-	RemoveDuplicates (m_staged);
+	removeDuplicates (m_staged);
 
 	for (auto it = m_staged.begin(); it != m_staged.end(); ++it)
 	{
--- a/src/glRenderer.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/glRenderer.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -485,7 +485,7 @@
 void GLRenderer::drawVBOs (EVBOSurface surface, EVBOComplement colors, GLenum type)
 {
 	// Filter this through some configuration options
-	if ((Eq (surface, VBOSF_Quads, VBOSF_Triangles) and cfg::DrawSurfaces == false) or
+	if ((isOneOf (surface, VBOSF_Quads, VBOSF_Triangles) and cfg::DrawSurfaces == false) or
 		(surface == VBOSF_Lines and cfg::DrawEdgeLines == false) or
 		(surface == VBOSF_CondLines and cfg::DrawConditionalLines == false))
 	{
@@ -636,7 +636,7 @@
 			QPoint v0 = coordconv3_2 (currentDocumentData().overlays[camera()].v0),
 					   v1 = coordconv3_2 (currentDocumentData().overlays[camera()].v1);
 
-			QRect targRect (v0.x(), v0.y(), Abs (v1.x() - v0.x()), Abs (v1.y() - v0.y())),
+			QRect targRect (v0.x(), v0.y(), qAbs (v1.x() - v0.x()), qAbs (v1.y() - v0.y())),
 				  srcRect (0, 0, overlay.img->width(), overlay.img->height());
 			paint.drawImage (targRect, *overlay.img, srcRect);
 		}
@@ -796,7 +796,7 @@
 {
 	int dx = ev->x() - m_mousePosition.x();
 	int dy = ev->y() - m_mousePosition.y();
-	m_totalmove += Abs (dx) + Abs (dy);
+	m_totalmove += qAbs (dx) + qAbs (dy);
 	setCameraMoving (false);
 
 	if (not m_editmode->mouseMoved (ev))
@@ -868,7 +868,7 @@
 	doMakeCurrent();
 
 	zoomNotch (ev->delta() > 0);
-	zoom() = Clamp (zoom(), 0.01, 10000.0);
+	zoom() = qBound (0.01, zoom(), 10000.0);
 	setCameraMoving (true);
 	update();
 	ev->accept();
@@ -937,10 +937,10 @@
 	int y1 = y0 + range.height();
 
 	// Clamp the values to ensure they're within bounds
-	x0 = Max (0, x0);
-	y0 = Max (0, y0);
-	x1 = Min (x1, m_width);
-	y1 = Min (y1, m_height);
+	x0 = qMax (0, x0);
+	y0 = qMax (0, y0);
+	x1 = qMin (x1, m_width);
+	y1 = qMin (y1, m_height);
 	const int areawidth = (x1 - x0);
 	const int areaheight = (y1 - y0);
 	const qint32 numpixels = areawidth * areaheight;
@@ -969,7 +969,7 @@
 			indices << idx;
 	}
 
-	RemoveDuplicates (indices);
+	removeDuplicates (indices);
 
 	for (qint32 idx : indices)
 	{
@@ -1082,7 +1082,7 @@
 		glDisable (GL_DITHER);
 
 		// Use particularly thick lines while picking ease up selecting lines.
-		glLineWidth (Max<double> (cfg::LineThickness, 6.5));
+		glLineWidth (qMax<double> (cfg::LineThickness, 6.5));
 	}
 	else
 	{
--- a/src/ldDocument.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/ldDocument.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -140,7 +140,7 @@
 		if (file == null)
 			continue;
 
-		if (Eq (name, file->name(), file->defaultName()))
+		if (isOneOf (name, file->name(), file->defaultName()))
 			return file;
 	}
 
@@ -1251,7 +1251,7 @@
 	for (QVector<Vertex> const& verts : m_objectVertices)
 		m_vertices << verts;
 
-	RemoveDuplicates (m_vertices);
+	removeDuplicates (m_vertices);
 	m_needVertexMerge = false;
 }
 
--- a/src/ldObject.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/ldObject.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -470,7 +470,7 @@
 		obj->swap (file->getObject (target));
 	}
 
-	RemoveDuplicates (objsToCompile);
+	removeDuplicates (objsToCompile);
 
 	// The objects need to be recompiled, otherwise their pick lists are left with
 	// the wrong index colors which messes up selection.
@@ -510,7 +510,7 @@
 		if (not text.isEmpty())
 			text += ", ";
 
-		QString noun = format ("%1%2", typeName (objType), Plural (count));
+		QString noun = format ("%1%2", typeName (objType), plural (count));
 		text += format ("%1 %2", count, noun);
 	}
 
--- a/src/mainwindow.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/mainwindow.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -483,7 +483,7 @@
 
 	// Update the GL renderer
 	LDObjectList compound = priorSelection + Selection();
-	RemoveDuplicates (compound);
+	removeDuplicates (compound);
 
 	for (LDObject* obj : compound)
 		R()->compileObject (obj);
@@ -908,7 +908,7 @@
 	{
 		QIcon ico = MakeColorIcon (pair.first, 16);
 		box->addItem (ico, format ("[%1] %2 (%3 object%4)",
-			pair.first, pair.first.name(), pair.second, Plural (pair.second)));
+			pair.first, pair.first.name(), pair.second, plural (pair.second)));
 		box->setItemData (row, pair.first.index());
 
 		++row;
--- a/src/miscallenous.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/miscallenous.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -109,10 +109,10 @@
 double Grid::Snap (double value, const Grid::Config type)
 {
 	double snapvalue = (type == Coordinate) ? *CurrentGrid().coordinateSnap : *CurrentGrid().angleSnap;
-	double mult = floor (Abs<double> (value / snapvalue));
+	double mult = floor (qAbs<double> (value / snapvalue));
 	double out = mult * snapvalue;
 
-	if (Abs (value) - (mult * snapvalue) > snapvalue / 2)
+	if (qAbs (value) - (mult * snapvalue) > snapvalue / 2)
 		out += snapvalue;
 
 	if (value < 0)
--- a/src/partDownloader.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/partDownloader.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -162,12 +162,12 @@
 
 	// If the part starts with s\ or s/, then use parts/s/. Same goes with
 	// 48\ and p/48/.
-	if (Eq (dest.left (2), "s\\", "s/"))
+	if (isOneOf (dest.left (2), "s\\", "s/"))
 	{
 		dest.remove (0, 2);
 		dest.prepend ("parts/s/");
 	}
-	elif (Eq (dest.left (3), "48\\", "48/"))
+	elif (isOneOf (dest.left (3), "48\\", "48/"))
 	{
 		dest.remove (0, 3);
 		dest.prepend ("p/48/");
@@ -573,7 +573,7 @@
 //
 bool PartDownloadRequest::isFinished() const
 {
-    return Eq (state(), State::Finished, State::Failed);
+    return isOneOf (state(), State::Finished, State::Failed);
 }
 
 // =============================================================================
--- a/src/primitives.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/primitives.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -131,7 +131,7 @@
 //
 void PrimitiveScanner::work()
 {
-	int j = Min (m_i + 100, m_files.size());
+	int j = qMin (m_i + 100, m_files.size());
 
 	for (; m_i < j; ++m_i)
 	{
@@ -582,7 +582,7 @@
 	// Truncate the root if necessary (7-16rin4.dat for instance).
 	// However, always keep the root at least 2 characters.
 	int extra = (frac.length() + numstr.length() + root.length()) - 8;
-	root.chop (Clamp (extra, 0, 2));
+	root.chop (qBound (0, extra, 2));
 
 	// Stick them all together and return the result.
 	return prefix + frac + root + numstr + ".dat";
--- a/src/ringFinder.cpp	Sun Aug 30 05:30:17 2015 +0300
+++ b/src/ringFinder.cpp	Sun Aug 30 15:01:10 2015 +0300
@@ -36,7 +36,7 @@
 	double num = r0 / scale;
 
 	// If the ring number is integral, we have found a fitting ring to r0 -> r1!
-	if (IsIntegral (num))
+	if (isInteger (num))
 	{
 		Component cmp;
 		cmp.scale = scale;
@@ -54,7 +54,7 @@
 	else
 	{
 		// Try find solutions by splitting the ring in various positions.
-		if (IsZero (r1 - r0))
+		if (isZero (r1 - r0))
 			return false;
 
 		double interval;
@@ -129,18 +129,18 @@
 	// components.
 	double scale = 1.0;
 
-	if (not IsZero (scale = r0 - floor (r0)) or not IsZero (scale = r1 - floor (r1)))
+	if (not isZero (scale = r0 - floor (r0)) or not isZero (scale = r1 - floor (r1)))
 	{
 		double r0f = r0 / scale;
 		double r1f = r1 / scale;
 
-		if (IsIntegral (r0f) and IsIntegral (r1f))
+		if (isInteger (r0f) and isInteger (r1f))
 		{
 			r0 = r0f;
 			r1 = r1f;
 		}
 		// If the numbers are both at most one-decimal fractions, we can use a scale of 10
-		elif (IsIntegral (r0 * 10) and IsIntegral (r1 * 10))
+		elif (isInteger (r0 * 10) and isInteger (r1 * 10))
 		{
 			scale = 0.1;
 			r0 *= 10;
@@ -203,8 +203,8 @@
 
 	for (int i = 0; i < getComponents().size(); ++i)
 	{
-		maxA = Max (getComponents()[i].num, maxA);
-		maxB = Max (other->getComponents()[i].num, maxB);
+		maxA = qMax (getComponents()[i].num, maxA);
+		maxB = qMax (other->getComponents()[i].num, maxB);
 	}
 
 	if (maxA != maxB)

mercurial