Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rename SparseMap to IntervalMap and add a unit test for this class.

  • Loading branch information...
commit 4a76a340bc3dce87f09195ed4ace70d59f2d5e6a 1 parent c3d0b16
@ajclinto authored
View
2  SparseMap.C → IntervalMap.C
@@ -22,5 +22,5 @@
The GNU General Public License is contained in the file COPYING.
*/
-#include "SparseMap.h"
+#include "IntervalMap.h"
View
12 SparseMap.h → IntervalMap.h
@@ -22,8 +22,8 @@
The GNU General Public License is contained in the file COPYING.
*/
-#ifndef SparseMap_H
-#define SparseMap_H
+#ifndef IntervalMap_H
+#define IntervalMap_H
#include <QMutex>
#include "Math.h"
@@ -31,8 +31,9 @@
#include <map>
#include <string>
+// This class stores a map of intervals [start, end)
template <typename T>
-class SparseMap {
+class IntervalMap {
private:
struct Entry {
uint64 start;
@@ -53,6 +54,7 @@ class SparseMap {
QMutexLocker lock(&myLock);
myMap.erase(end);
}
+ size_t size() const { return myMap.size(); }
//
// Note that these methods return elements by value. This is to ensure
@@ -113,7 +115,7 @@ class SparseMap {
mutable QMutex myLock;
};
-typedef SparseMap<std::string> StackTraceMap;
-typedef SparseMap<std::string> MMapMap;
+typedef IntervalMap<std::string> StackTraceMap;
+typedef IntervalMap<std::string> MMapMap;
#endif
View
2  Loader.h
@@ -28,7 +28,7 @@
#include <QtGui>
#include "valgrind/memview/mv_ipc.h"
#include "Math.h"
-#include "SparseMap.h"
+#include "IntervalMap.h"
#include <memory>
#include <sys/types.h>
#include <signal.h>
View
2  MemoryState.h
@@ -28,7 +28,7 @@
#include <QtGui>
#include "Math.h"
#include "GLImage.h"
-#include "SparseMap.h"
+#include "IntervalMap.h"
#include "valgrind/memview/mv_ipc.h"
// Storage for the entire memory state. This is specifically designed to
View
2  Window.h
@@ -33,7 +33,7 @@
#include "StopWatch.h"
#include "MemoryState.h"
#include "DisplayLayout.h"
-#include "SparseMap.h"
+#include "IntervalMap.h"
#include <queue>
class MemViewWidget;
View
4 memview.pro
@@ -15,5 +15,5 @@ QMAKE_CFLAGS_RELEASE = -DGL_GLEXT_PROTOTYPES -g -O3
QMAKE_CXXFLAGS_RELEASE = -DGL_GLEXT_PROTOTYPES -g -O3 -std=c++0x
# Input
-HEADERS += Window.h MemoryState.h Loader.h DisplayLayout.h SparseMap.h
-SOURCES += main.C window.C MemoryState.C Loader.C DisplayLayout.C SparseMap.C
+HEADERS += Window.h MemoryState.h Loader.h DisplayLayout.h IntervalMap.h
+SOURCES += main.C window.C MemoryState.C Loader.C DisplayLayout.C IntervalMap.C
View
13 test/Makefile
@@ -0,0 +1,13 @@
+APP = test
+SRC = test.C
+
+# Copied from the compile line in the parent directory
+CXXFLAGS = -g -O3 -std=c++0x -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_CORE_LIB -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4
+
+LDFLAGS = -lQtCore
+
+$(APP): $(SRC)
+ g++ $(CXXFLAGS) $(SRC) -o $@ $(LDFLAGS)
+
+clean:
+ rm -f test
View
52 test/test.C
@@ -0,0 +1,52 @@
+#include "../IntervalMap.h"
+
+#define FIND(IDX, STR) \
+ if (map.find(IDX) != STR) \
+ { \
+ fprintf(stderr, "find: %d %s\n", IDX, STR); \
+ return 1; \
+ }
+
+#define CLOSEST(IDX, STR) \
+ if (map.findClosest(IDX) != STR) \
+ { \
+ fprintf(stderr, "findClosest: %d %s\n", IDX, STR); \
+ return 1; \
+ }
+
+int main()
+{
+ StackTraceMap map;
+
+ map.insert(1, 2, "test1");
+ map.insert(10, 20, "test2");
+
+ FIND(0, "")
+ FIND(1, "test1")
+ FIND(2, "")
+ FIND(15, "test2")
+ FIND(20, "")
+ FIND(100, "")
+
+ CLOSEST(0, "test1")
+ CLOSEST(1, "test1")
+ CLOSEST(8, "test2")
+ CLOSEST(100, "test2")
+
+ map.erase(1, 2);
+
+ FIND(1, "")
+
+ map.erase(10, 20);
+
+ if (map.size())
+ {
+ fprintf(stderr, "map should be empty\n");
+ return 1;
+ }
+
+ FIND(15, "")
+ CLOSEST(15, "")
+
+ return 0;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.