From 1a6a12a8b3310400a103e0048b7ed747fc479398 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 13 May 2015 20:59:09 +0300 Subject: Add initial support for custom stack class. In some tests it 3 times faster in push than std::stack. --- src/resources/mstack.h | 104 ++++++++++++++++++++++++ src/resources/mstack_unittest.cc | 165 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 src/resources/mstack.h create mode 100644 src/resources/mstack_unittest.cc (limited to 'src/resources') 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 . + */ + +#ifndef RESOURCES_MSTACK_H +#define RESOURCES_MSTACK_H + +#include "logger.h" + +#include "localconsts.h" + +template +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 . + */ + +#include "gui/cliprect.h" + +#include "resources/mstack.h" + +#include "gtest/gtest.h" + +#include "debug.h" + +TEST(mstack, push1) +{ + MStack 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 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 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 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 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 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 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 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); +} -- cgit v1.2.3-70-g09d2