summaryrefslogtreecommitdiff
path: root/src/resources/mstack.h
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/mstack.h
parent0984b817b1bfed9b8b475f7c79d1627e5037e914 (diff)
downloadplus-1a6a12a8b3310400a103e0048b7ed747fc479398.tar.gz
plus-1a6a12a8b3310400a103e0048b7ed747fc479398.tar.bz2
plus-1a6a12a8b3310400a103e0048b7ed747fc479398.tar.xz
plus-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/mstack.h')
-rw-r--r--src/resources/mstack.h104
1 files changed, 104 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