[Scummvm-cvs-logs] SF.net SVN: scummvm:[47071] scummvm/trunk

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Wed Jan 6 13:16:00 CET 2010


Revision: 47071
          http://scummvm.svn.sourceforge.net/scummvm/?rev=47071&view=rev
Author:   fingolfin
Date:     2010-01-06 12:15:05 +0000 (Wed, 06 Jan 2010)

Log Message:
-----------
Add more comparision operators to Timestamp

Modified Paths:
--------------
    scummvm/trunk/sound/timestamp.cpp
    scummvm/trunk/sound/timestamp.h
    scummvm/trunk/test/sound/timestamp.h

Modified: scummvm/trunk/sound/timestamp.cpp
===================================================================
--- scummvm/trunk/sound/timestamp.cpp	2010-01-06 12:09:14 UTC (rev 47070)
+++ scummvm/trunk/sound/timestamp.cpp	2010-01-06 12:15:05 UTC (rev 47071)
@@ -72,15 +72,43 @@
 }
 
 bool Timestamp::operator==(const Timestamp &ts) const {
-	return (ts._secs == _secs) &&
-			(ts._numberOfFrames * _framerate == _numberOfFrames * ts._framerate);
+	return cmp(ts) == 0;
 }
 
 bool Timestamp::operator!=(const Timestamp &ts) const {
-	return !(*this == ts);
+	return cmp(ts) != 0;
 }
 
+bool Timestamp::operator<(const Timestamp &ts) const {
+	return cmp(ts) < 0;
+}
 
+bool Timestamp::operator<=(const Timestamp &ts) const {
+	return cmp(ts) <= 0;
+}
+
+bool Timestamp::operator>(const Timestamp &ts) const {
+	return cmp(ts) > 0;
+}
+
+bool Timestamp::operator>=(const Timestamp &ts) const {
+	return cmp(ts) >= 0;
+}
+
+int Timestamp::cmp(const Timestamp &ts) const {
+	int delta = _secs - ts._secs;
+	if (!delta) {
+		const uint g = gcd(_framerate, ts._framerate);
+		const uint p = _framerate / g;
+		const uint q = ts._framerate / g;
+
+		delta = (_numberOfFrames * q - ts._numberOfFrames * p);
+	}
+
+	return delta;
+}
+
+
 Timestamp Timestamp::addFrames(int frames) const {
 	Timestamp ts(*this);
 

Modified: scummvm/trunk/sound/timestamp.h
===================================================================
--- scummvm/trunk/sound/timestamp.h	2010-01-06 12:09:14 UTC (rev 47070)
+++ scummvm/trunk/sound/timestamp.h	2010-01-06 12:15:05 UTC (rev 47071)
@@ -90,12 +90,11 @@
 	 * as equal even if they use different framerates.
 	 */
 	bool operator==(const Timestamp &ts) const;
-
 	bool operator!=(const Timestamp &ts) const;
-// 	bool operator<(const Timestamp &ts) const;
-// 	bool operator<=(const Timestamp &ts) const;
-// 	bool operator>(const Timestamp &ts) const;
-// 	bool operator>=(const Timestamp &ts) const;
+	bool operator<(const Timestamp &ts) const;
+	bool operator<=(const Timestamp &ts) const;
+	bool operator>(const Timestamp &ts) const;
+	bool operator>=(const Timestamp &ts) const;
 
 	/**
 	 * Returns a new timestamp, which corresponds to the time encoded
@@ -129,6 +128,10 @@
 
 	/** Return the framerate used by this timestamp. */
 	int getFramerate() const { return _framerate / _framerateFactor; }
+
+protected:
+
+	int cmp(const Timestamp &ts) const;
 };
 
 

Modified: scummvm/trunk/test/sound/timestamp.h
===================================================================
--- scummvm/trunk/test/sound/timestamp.h	2010-01-06 12:09:14 UTC (rev 47070)
+++ scummvm/trunk/test/sound/timestamp.h	2010-01-06 12:15:05 UTC (rev 47071)
@@ -103,6 +103,40 @@
 	}
 
 
+	void test_compare() {
+		const Audio::Timestamp a = Audio::Timestamp(60, 1000);
+		Audio::Timestamp b = Audio::Timestamp(60, 60);
+		Audio::Timestamp c = Audio::Timestamp(60, 44100);
+
+		TS_ASSERT(a <= b);
+		TS_ASSERT(b <= c);
+		TS_ASSERT(a <= c);
+
+		TS_ASSERT(b >= a);
+		TS_ASSERT(c >= b);
+		TS_ASSERT(c >= a);
+
+		b = b.addFrames(60 / 12);
+		c = c.addFrames(44100 / 10);
+
+		TS_ASSERT(a < b);
+		TS_ASSERT(b < c);
+		TS_ASSERT(a < c);
+
+		TS_ASSERT(b > a);
+		TS_ASSERT(c > b);
+		TS_ASSERT(c > a);
+
+		TS_ASSERT(a <= b);
+		TS_ASSERT(b <= c);
+		TS_ASSERT(a <= c);
+
+		TS_ASSERT(b >= a);
+		TS_ASSERT(c >= b);
+		TS_ASSERT(c >= a);
+	}
+
+
 	void test_framerate() {
 		const Audio::Timestamp a = Audio::Timestamp(500, 1000);
 		const Audio::Timestamp b = Audio::Timestamp(500, 67);


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list