From 1e23be81a0ac264de731b9432cfccf174e695e76 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 18 Jun 2014 11:26:44 +0000 Subject: [PATCH] base: remove Array::PushFirst() because Array::Insert(..., 0) works just as well, remove PushUniqueFirst in favour of InsertUnique(), and optimise Array::Swap(). --- src/image/image.cpp | 2 ++ src/lol/base/array.h | 53 +++++++++++++++++--------------------------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/src/image/image.cpp b/src/image/image.cpp index dc498a98..21ed86b3 100644 --- a/src/image/image.cpp +++ b/src/image/image.cpp @@ -15,6 +15,8 @@ #include "core.h" #include "image-private.h" +#include /* for std::swap */ + namespace lol { diff --git a/src/lol/base/array.h b/src/lol/base/array.h index c0acef18..d1f3155f 100644 --- a/src/lol/base/array.h +++ b/src/lol/base/array.h @@ -13,7 +13,7 @@ // The Array class // --------------- // A very simple Array class not unlike the std::vector, with some nice -// additional features, eg. Array for automatic arrays of structs. +// additional features, eg. Array for automatic arrays of tuples. // #if !defined __LOL_BASE_ARRAY_H__ @@ -21,7 +21,8 @@ #include -#include +#include /* for placement new */ +#include /* for std::swap */ #include namespace lol @@ -216,33 +217,11 @@ public: inline bool PushUnique(T const &x) { - int idx = Find(x); - if (idx == INDEX_NONE) - { - Push(x); - return true; - } - return false; - } + if (Find(x) != INDEX_NONE) + return false; - //PushFirst, insert, etc : CRUDE VERSION - inline void PushFirst(T const &x) - { - ArrayBase tmp; - tmp.Push(x); - tmp += *this; - *this = tmp; - } - - inline bool PushUniqueFirst(T const &x) - { - int idx = Find(x); - if (idx == INDEX_NONE) - { - PushFirst(x); - return true; - } - return false; + Push(x); + return true; } inline void Insert(T const &x, int pos) @@ -262,6 +241,18 @@ public: ++m_count; } + inline bool InsertUnique(T const &x, int pos) + { + ASSERT(pos >= 0); + ASSERT(pos <= m_count); + + if (Find(x) != INDEX_NONE) + return false; + + Insert(x, pos); + return true; + } + inline int Find(T const &x) { for (int i = 0; i < m_count; ++i) @@ -320,11 +311,7 @@ public: ASSERT(pos2 < m_count); if (pos1 != pos2) - { - Element tmp = (*this)[pos1]; - (*this)[pos1] = (*this)[pos2]; - (*this)[pos2] = tmp; - } + std::swap((*this)[pos1], (*this)[pos2]); } void Remove(int pos, int todelete = 1)