summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2015-05-13 20:59:09 +0300
committerAndrei Karas <akaras@inbox.ru>2015-05-13 20:59:45 +0300
commit1a6a12a8b3310400a103e0048b7ed747fc479398 (patch)
treead969a9b12bb65e8072e5b2937f9d5c41a559841 /src/resources
parent0984b817b1bfed9b8b475f7c79d1627e5037e914 (diff)
downloadmv-1a6a12a8b3310400a103e0048b7ed747fc479398.tar.gz
mv-1a6a12a8b3310400a103e0048b7ed747fc479398.tar.bz2
mv-1a6a12a8b3310400a103e0048b7ed747fc479398.tar.xz
mv-1a6a12a8b3310400a103e0048b7ed747fc479398.zip
Add initial support for custom stack class.
In some tests it 3 times faster in push than std::stack.
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/mstack.h104
-rw-r--r--src/resources/mstack_unittest.cc165
2 files changed, 269 insertions, 0 deletions
diff --git a/src/resources/mstack.h b/src/resources/mstack.h
new file mode 100644
index 000000000..ba1a58eb2
--- /dev/null
+++ b/src/resources/mstack.h
@@ -0,0 +1,104 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2015 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef RESOURCES_MSTACK_H
+#define RESOURCES_MSTACK_H
+
+#include "logger.h"
+
+#include "localconsts.h"
+
+template<typename T>
+struct MStack final
+{
+ MStack(const size_t maxSize) :
+ mStack(new T[maxSize]),
+ mMaxSize(maxSize),
+ mPointer(mStack - 1),
+ mStartPointer(mStack - 1),
+ mEndPointer(mStack + maxSize - 1)
+ {
+ }
+
+ ~MStack()
+ {
+ delete [] mStack;
+ }
+
+ T &push()
+ {
+ if (mPointer == mEndPointer)
+ {
+ logger->log("error: max stack size reached");
+ return *mPointer;
+ }
+ return *(++mPointer);
+ }
+
+ T &getPop()
+ {
+ if (mPointer == mStartPointer)
+ logger->log("error: minimal stack size reached");
+ else
+ mPointer --;
+ return *mPointer;
+ }
+
+ const T &getPopConst()
+ {
+ if (mPointer == mStartPointer)
+ logger->log("error: minimal stack size reached");
+ else
+ mPointer --;
+ return *mPointer;
+ }
+
+ void pop()
+ {
+ if (mPointer == mStartPointer)
+ logger->log("error: minimal stack size reached");
+ else
+ mPointer --;
+ }
+
+ T &top()
+ {
+ return *mPointer;
+ }
+
+ const T &topConst() const
+ {
+ return *mPointer;
+ }
+
+ void clear()
+ {
+ mPointer = mStack - 1;
+ }
+
+ T *mStack;
+
+ size_t mMaxSize;
+ T *mPointer;
+ const T *const mStartPointer;
+ const T *const mEndPointer;
+};
+
+#endif // RESOURCES_MSTACK_H
diff --git a/src/resources/mstack_unittest.cc b/src/resources/mstack_unittest.cc
new file mode 100644
index 000000000..6146f28e5
--- /dev/null
+++ b/src/resources/mstack_unittest.cc
@@ -0,0 +1,165 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2015 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gui/cliprect.h"
+
+#include "resources/mstack.h"
+
+#include "gtest/gtest.h"
+
+#include "debug.h"
+
+TEST(mstack, push1)
+{
+ MStack<ClipRect> stack(10);
+ EXPECT_EQ(-1, stack.mPointer - stack.mStack);
+ EXPECT_EQ(0, stack.mStack[0].xOffset);
+
+ ClipRect &val1 = stack.push();
+ val1.xOffset = 10;
+ ClipRect &val2 = stack.top();
+ EXPECT_EQ(0, stack.mPointer - stack.mStack);
+ EXPECT_EQ(10, val2.xOffset);
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+ EXPECT_EQ(0, stack.mStack[1].xOffset);
+
+ val2.yOffset = 2;
+ EXPECT_EQ(2, val1.yOffset);
+}
+
+TEST(mstack, push2)
+{
+ MStack<ClipRect> stack(10);
+ ClipRect &val1 = stack.push();
+ val1.xOffset = 10;
+ const ClipRect &val2 = stack.top();
+ EXPECT_EQ(10, val2.xOffset);
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+
+ val1.yOffset = 2;
+ EXPECT_EQ(2, val2.yOffset);
+ EXPECT_EQ(2, stack.mStack[0].yOffset);
+}
+
+TEST(mstack, push3)
+{
+ MStack<ClipRect> stack(10);
+ ClipRect &val1 = stack.push();
+ val1.xOffset = 10;
+ val1 = stack.top();
+ EXPECT_EQ(10, val1.xOffset);
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+}
+
+TEST(mstack, push4)
+{
+ MStack<ClipRect> stack(10);
+ ClipRect &val1 = stack.push();
+ val1.xOffset = 10;
+ EXPECT_EQ(10, val1.xOffset);
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+ EXPECT_EQ(0, stack.mStack[1].xOffset);
+ EXPECT_EQ(0, stack.mStack[2].xOffset);
+
+ ClipRect &val2 = stack.push();
+ val2.xOffset = 20;
+ EXPECT_EQ(20, val2.xOffset);
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+ EXPECT_EQ(20, stack.mStack[1].xOffset);
+ EXPECT_EQ(0, stack.mStack[2].xOffset);
+
+ ClipRect &val3 = stack.push();
+ val3.xOffset = 30;
+ EXPECT_EQ(30, val3.xOffset);
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+ EXPECT_EQ(20, stack.mStack[1].xOffset);
+ EXPECT_EQ(30, stack.mStack[2].xOffset);
+}
+
+TEST(mstack, pop1)
+{
+ MStack<ClipRect> stack(10);
+ ClipRect &val1 = stack.push();
+ val1.xOffset = 10;
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+
+ stack.pop();
+ EXPECT_EQ(-1, stack.mPointer - stack.mStack);
+}
+
+TEST(mstack, pop2)
+{
+ MStack<ClipRect> stack(10);
+ ClipRect &val1 = stack.push();
+ EXPECT_EQ(0, stack.mPointer - stack.mStack);
+
+ val1.xOffset = 10;
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+
+ ClipRect &val2 = stack.push();
+ EXPECT_EQ(1, stack.mPointer - stack.mStack);
+
+ val2.xOffset = 20;
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+ EXPECT_EQ(20, stack.mStack[1].xOffset);
+
+ stack.pop();
+ EXPECT_EQ(0, stack.mPointer - stack.mStack);
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+ EXPECT_EQ(20, stack.mStack[1].xOffset);
+
+ ClipRect &val3 = stack.top();
+ EXPECT_EQ(0, stack.mPointer - stack.mStack);
+ EXPECT_EQ(10, val1.xOffset);
+ EXPECT_EQ(20, val2.xOffset);
+ EXPECT_EQ(10, val3.xOffset);
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+ EXPECT_EQ(20, stack.mStack[1].xOffset);
+ EXPECT_EQ(0, stack.mStack[2].xOffset);
+}
+
+TEST(mstack, clear1)
+{
+ MStack<ClipRect> stack(10);
+ EXPECT_EQ(-1, stack.mPointer - stack.mStack);
+ EXPECT_EQ(0, stack.mStack[0].xOffset);
+
+ ClipRect &val1 = stack.push();
+ val1.xOffset = 10;
+
+ stack.clear();
+ EXPECT_EQ(-1, stack.mPointer - stack.mStack);
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+ EXPECT_EQ(0, stack.mStack[1].xOffset);
+}
+
+TEST(mstack, getpop1)
+{
+ MStack<ClipRect> stack(10);
+ ClipRect &val1 = stack.push();
+ val1.xOffset = 10;
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+ EXPECT_EQ(10, val1.xOffset);
+
+ ClipRect &val2 = stack.getPop();
+ EXPECT_EQ(-1, stack.mPointer - stack.mStack);
+ EXPECT_EQ(10, stack.mStack[0].xOffset);
+ EXPECT_EQ(10, val2.xOffset);
+}