Browse Source

Removed Iterator.h.
Removed unnecessary checks from String & Vector concatenations.

Lasse Öörni 14 years ago
parent
commit
2952b2f5bf

+ 0 - 122
Engine/Container/Iterator.h

@@ -1,122 +0,0 @@
-//
-// Urho3D Engine
-// Copyright (c) 2008-2011 Lasse Öörni
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
-//
-
-#pragma once
-
-/// Random access iterator
-template <class T> class RandomAccessIterator
-{
-public:
-    /// Construct
-    explicit RandomAccessIterator(T* ptr) :
-        ptr_(ptr)
-    {
-    }
-    
-    /// Point to the object
-    T* operator -> () const { return ptr_; }
-    /// Dereference the object
-    T& operator * () const { return *ptr_; }
-    /// Preincrement the pointer
-    RandomAccessIterator<T>& operator ++ () { ++ptr_; return *this; }
-    /// Postincrement the pointer
-    RandomAccessIterator<T> operator ++ (int) { RandomAccessIterator<T> it = *this; ++ptr_; return it; }
-    /// Predecrement the pointer
-    RandomAccessIterator<T>& operator -- () { --ptr_; return *this; }
-    /// Postdecrement the pointer
-    RandomAccessIterator<T> operator -- (int) { RandomAccessIterator<T> it = *this; --ptr_; return it; }
-    /// Add an offset to the pointer
-    RandomAccessIterator<T>& operator += (int value) { ptr_ += value; return *this; }
-    /// Subtract an offset from the pointer
-    RandomAccessIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
-    /// Add an offset to the pointer
-    RandomAccessIterator<T> operator + (int value) const { return RandomAccessIterator<T>(ptr_ + value); }
-    /// Subtract an offset from the pointer
-    RandomAccessIterator<T> operator - (int value) const { return RandomAccessIterator<T>(ptr_ - value); }
-    /// Calculate offset to another iterator
-    int operator - (const RandomAccessIterator& rhs) const { return ptr_ - rhs.ptr_; }
-    /// Test for equality with another iterator
-    bool operator == (const RandomAccessIterator& rhs) const { return ptr_ == rhs.ptr_; }
-    /// Test for inequality with another iterator
-    bool operator != (const RandomAccessIterator& rhs) const { return ptr_ != rhs.ptr_; }
-    /// Test for less than with another iterator
-    bool operator < (const RandomAccessIterator& rhs) const { return ptr_ < rhs.ptr_; }
-    /// Test for greater than with another iterator
-    bool operator > (const RandomAccessIterator& rhs) const { return ptr_ > rhs.ptr_; }
-    
-    /// Pointer
-    T* ptr_;
-};
-
-/// Random access const iterator
-template <class T> class RandomAccessConstIterator
-{
-public:
-    /// Construct
-    explicit RandomAccessConstIterator(T* ptr) :
-        ptr_(ptr)
-    {
-    }
-    
-    /// Construct from a non-const iterator
-    RandomAccessConstIterator(const RandomAccessIterator<T>& rhs) :
-        ptr_(rhs.ptr_)
-    {
-    }
-    
-    /// Assign from a non-const iterator
-    RandomAccessConstIterator<T>& operator = (const RandomAccessIterator<T>& rhs) { ptr_ = rhs.ptr_; return *this; }
-    /// Point to the object
-    const T* operator -> () const { return ptr_; }
-    /// Dereference the object
-    const T& operator * () const { return *ptr_; }
-    /// Preincrement the pointer
-    RandomAccessConstIterator<T>& operator ++ () { ++ptr_; return *this; }
-    /// Postincrement the pointer
-    RandomAccessConstIterator<T> operator ++ (int) { RandomAccessConstIterator<T> it = *this; ++ptr_; return it; }
-    /// Predecrement the pointer
-    RandomAccessConstIterator<T>& operator -- () { --ptr_; return *this; }
-    /// Postdecrement the pointer
-    RandomAccessConstIterator<T> operator -- (int) { RandomAccessConstIterator<T> it = *this; --ptr_; return it; }
-    /// Add an offset to the pointer
-    RandomAccessConstIterator<T>& operator += (int value) { ptr_ += value; return *this; }
-    /// Subtract an offset from the pointer
-    RandomAccessConstIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
-    /// Add an offset to the pointer
-    RandomAccessConstIterator<T> operator + (int value) const { return RandomAccessConstIterator<T>(ptr_ + value); }
-    /// Subtract an offset from the pointer
-    RandomAccessConstIterator<T> operator - (int value) const { return RandomAccessConstIterator<T>(ptr_ - value); }
-    /// Calculate offset to another iterator
-    int operator - (const RandomAccessConstIterator& rhs) const { return ptr_ - rhs.ptr_; }
-    /// Test for equality with another iterator
-    bool operator == (const RandomAccessConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
-    /// Test for inequality with another iterator
-    bool operator != (const RandomAccessConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
-    /// Test for less than with another iterator
-    bool operator < (const RandomAccessConstIterator& rhs) const { return ptr_ < rhs.ptr_; }
-    /// Test for greater than with another iterator
-    bool operator > (const RandomAccessConstIterator& rhs) const { return ptr_ > rhs.ptr_; }
-    
-    /// Pointer
-    T* ptr_;
-};

+ 1 - 26
Engine/Container/PODVector.h

@@ -23,7 +23,6 @@
 
 #pragma once
 
-#include "Iterator.h"
 #include "VectorBase.h"
 
 #include <cstring>
@@ -155,9 +154,6 @@ public:
     /// Add another vector at the end
     void Push(const PODVector<T>& vector)
     {
-        if (!vector.size_)
-            return;
-        
         unsigned oldSize = size_;
         Resize(size_ + vector.size_);
         CopyElements(GetBuffer() + oldSize, vector.GetBuffer(), vector.size_);
@@ -173,12 +169,6 @@ public:
     /// Insert an element at position
     void Insert(unsigned pos, const T& value)
     {
-        if (!size_)
-        {
-            Push(value);
-            return;
-        }
-        
         if (pos > size_)
             pos = size_;
         
@@ -191,15 +181,6 @@ public:
     /// Insert another vector at position
     void Insert(unsigned pos, const PODVector<T>& vector)
     {
-        if (!vector.size_)
-            return;
-            
-        if (!size_)
-        {
-            *this = vector;
-            return;
-        }
-        
         if (pos > size_)
             pos = size_;
         
@@ -240,10 +221,7 @@ public:
         unsigned length = end - start;
         Resize(size_ + length);
         MoveRange(pos + length, pos, size_ - pos - length);
-        
-        T* destPtr = GetBuffer() + pos;
-        for (Iterator i = start; i != end; ++i)
-            *destPtr++ = *i;
+        CopyElements(GetBuffer() + pos, &(*start), length);
         
         return Begin() + pos;
     }
@@ -291,9 +269,6 @@ public:
     /// Resize the vector
     void Resize(unsigned newSize)
     {
-        if (newSize == size_)
-            return;
-        
         if (newSize > capacity_)
         {
             if (!capacity_)

+ 1 - 1
Engine/Container/Sort.h

@@ -23,8 +23,8 @@
 
 #pragma once
 
-#include "Iterator.h"
 #include "Swap.h"
+#include "VectorBase.h"
 
 static const int QUICKSORT_THRESHOLD = 16;
 

+ 18 - 18
Engine/Container/String.cpp

@@ -297,9 +297,6 @@ String::Iterator String::Erase(const String::Iterator& start, const String::Iter
 
 void String::Resize(unsigned newLength)
 {
-    if (newLength == length_)
-        return;
-    
     if (!capacity_)
     {
         // Calculate initial capacity
@@ -311,7 +308,7 @@ void String::Resize(unsigned newLength)
     }
     else
     {
-        if (capacity_ < newLength + 1)
+        if ((newLength) && (capacity_ < newLength + 1))
         {
             // Increase the capacity with half each time it is exceeded
             while (capacity_ < newLength + 1)
@@ -350,10 +347,8 @@ void String::Reserve(unsigned newCapacity)
 
 void String::Compact()
 {
-    if (!capacity_)
-        return;
-    
-    Reserve(length_ + 1);
+    if (capacity_)
+        Reserve(length_ + 1);
 }
 
 void String::Clear()
@@ -394,9 +389,7 @@ String String::Replace(unsigned pos, unsigned length, const String& str) const
 
 String String::Substring(unsigned pos) const
 {
-    if (pos >= length_)
-        return String();
-    else
+    if (pos < length_)
     {
         String ret;
         ret.Resize(length_ - pos);
@@ -404,13 +397,13 @@ String String::Substring(unsigned pos) const
         
         return ret;
     }
+    else
+        return String();
 }
 
 String String::Substring(unsigned pos, unsigned length) const
 {
-    if (pos >= length_)
-        return String();
-    else
+    if (pos < length_)
     {
         String ret;
         if (pos + length > length_)
@@ -420,6 +413,8 @@ String String::Substring(unsigned pos, unsigned length) const
         
         return ret;
     }
+    else
+        return String();
 }
 
 String String::Trim() const
@@ -548,9 +543,12 @@ unsigned String::Find(const String& str, unsigned startPos) const
     return NPOS;
 }
 
-unsigned String::FindLast(char c) const
+unsigned String::FindLast(char c, unsigned startPos) const
 {
-    for (unsigned i = length_ - 1; i < length_; --i)
+    if (startPos >= length_)
+        startPos = length_ - 1;
+    
+    for (unsigned i = startPos; i < length_; --i)
     {
         if (buffer_[i] == c)
             return i;
@@ -559,14 +557,16 @@ unsigned String::FindLast(char c) const
     return NPOS;
 }
 
-unsigned String::FindLast(const String& str) const
+unsigned String::FindLast(const String& str, unsigned startPos) const
 {
     if ((!str.length_) || (str.length_ > length_))
         return NPOS;
+    if (startPos > length_ - str.length_)
+        startPos = length_ - str.length_;
     
     char first = str.buffer_[0];
     
-    for (unsigned i = length_ - str.length_; i < length_; --i)
+    for (unsigned i = startPos; i < length_; --i)
     {
         if (buffer_[i] == first)
         {

+ 9 - 21
Engine/Container/StringBase.h

@@ -139,12 +139,9 @@ public:
     /// Add-assign a string
     String& operator += (const String& rhs)
     {
-        if (rhs.length_)
-        {
-            unsigned oldLength = length_;
-            Resize(length_ + rhs.length_);
-            CopyChars(buffer_ + oldLength, rhs.buffer_, rhs.length_);
-        }
+        unsigned oldLength = length_;
+        Resize(length_ + rhs.length_);
+        CopyChars(buffer_ + oldLength, rhs.buffer_, rhs.length_);
         
         return *this;
     }
@@ -153,12 +150,9 @@ public:
     String& operator += (const char* rhs)
     {
         unsigned rhsLength = GetCStringLength(rhs);
-        if (rhsLength)
-        {
-            unsigned oldLength = length_;
-            Resize(length_ + rhsLength);
-            CopyChars(buffer_ + oldLength, rhs, rhsLength);
-        }
+        unsigned oldLength = length_;
+        Resize(length_ + rhsLength);
+        CopyChars(buffer_ + oldLength, rhs, rhsLength);
         
         return *this;
     }
@@ -202,13 +196,7 @@ public:
     /// Add a C string
     String operator + (const char* rhs) const
     {
-        if (!rhs)
-            return String(*this);
-        
-        unsigned rhsLength = strlen(rhs);
-        if (!rhsLength)
-            return String(*this);
-        
+        unsigned rhsLength = GetCStringLength(rhs);
         String ret;
         ret.Resize(length_ + rhsLength);
         CopyChars(ret.buffer_, buffer_, length_);
@@ -360,9 +348,9 @@ public:
     /// Find the first occurrence of a character, or NPOS if not found
     unsigned Find(char c, unsigned startPos = 0) const;
     /// Find the last occurrence of a string, or NPOS if not found
-    unsigned FindLast(const String& str) const;
+    unsigned FindLast(const String& str, unsigned startPos = NPOS) const;
     /// Find the last occurrence of a character, or NPOS if not found
-    unsigned FindLast(char c) const;
+    unsigned FindLast(char c, unsigned startPos = NPOS) const;
     /// Return the C string
     const char* CString() const { return buffer_; }
     /// Return length

+ 0 - 22
Engine/Container/Vector.h

@@ -23,7 +23,6 @@
 
 #pragma once
 
-#include "Iterator.h"
 #include "VectorBase.h"
 
 #include <cstring>
@@ -153,9 +152,6 @@ public:
     /// Add another vector at the end
     void Push(const Vector<T>& vector)
     {
-        if (!vector.size_)
-            return;
-        
         Resize(size_ + vector.size_, vector.GetBuffer());
     }
     
@@ -169,12 +165,6 @@ public:
     /// Insert an element at position
     void Insert(unsigned pos, const T& value)
     {
-        if (!size_)
-        {
-            Push(value);
-            return;
-        }
-        
         if (pos > size_)
             pos = size_;
         
@@ -187,15 +177,6 @@ public:
     /// Insert another vector at position
     void Insert(unsigned pos, const Vector<T>& vector)
     {
-        if (!vector.size_)
-            return;
-            
-        if (!size_)
-        {
-            *this = vector;
-            return;
-        }
-        
         if (pos > size_)
             pos = size_;
         
@@ -351,9 +332,6 @@ private:
    /// Resize the vector and create/remove new elements as necessary
     void Resize(unsigned newSize, const T* src)
     {
-        if (newSize == size_)
-            return;
-        
         // If size shrinks, destruct the removed elements
         if (newSize < size_)
             DestructElements(GetBuffer() + newSize, size_ - newSize);

+ 98 - 3
Engine/Container/VectorBase.h

@@ -25,6 +25,104 @@
 
 #include "Swap.h"
 
+/// Random access iterator
+template <class T> class RandomAccessIterator
+{
+public:
+    /// Construct
+    explicit RandomAccessIterator(T* ptr) :
+        ptr_(ptr)
+    {
+    }
+    
+    /// Point to the object
+    T* operator -> () const { return ptr_; }
+    /// Dereference the object
+    T& operator * () const { return *ptr_; }
+    /// Preincrement the pointer
+    RandomAccessIterator<T>& operator ++ () { ++ptr_; return *this; }
+    /// Postincrement the pointer
+    RandomAccessIterator<T> operator ++ (int) { RandomAccessIterator<T> it = *this; ++ptr_; return it; }
+    /// Predecrement the pointer
+    RandomAccessIterator<T>& operator -- () { --ptr_; return *this; }
+    /// Postdecrement the pointer
+    RandomAccessIterator<T> operator -- (int) { RandomAccessIterator<T> it = *this; --ptr_; return it; }
+    /// Add an offset to the pointer
+    RandomAccessIterator<T>& operator += (int value) { ptr_ += value; return *this; }
+    /// Subtract an offset from the pointer
+    RandomAccessIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
+    /// Add an offset to the pointer
+    RandomAccessIterator<T> operator + (int value) const { return RandomAccessIterator<T>(ptr_ + value); }
+    /// Subtract an offset from the pointer
+    RandomAccessIterator<T> operator - (int value) const { return RandomAccessIterator<T>(ptr_ - value); }
+    /// Calculate offset to another iterator
+    int operator - (const RandomAccessIterator& rhs) const { return ptr_ - rhs.ptr_; }
+    /// Test for equality with another iterator
+    bool operator == (const RandomAccessIterator& rhs) const { return ptr_ == rhs.ptr_; }
+    /// Test for inequality with another iterator
+    bool operator != (const RandomAccessIterator& rhs) const { return ptr_ != rhs.ptr_; }
+    /// Test for less than with another iterator
+    bool operator < (const RandomAccessIterator& rhs) const { return ptr_ < rhs.ptr_; }
+    /// Test for greater than with another iterator
+    bool operator > (const RandomAccessIterator& rhs) const { return ptr_ > rhs.ptr_; }
+    
+    /// Pointer
+    T* ptr_;
+};
+
+/// Random access const iterator
+template <class T> class RandomAccessConstIterator
+{
+public:
+    /// Construct
+    explicit RandomAccessConstIterator(T* ptr) :
+        ptr_(ptr)
+    {
+    }
+    
+    /// Construct from a non-const iterator
+    RandomAccessConstIterator(const RandomAccessIterator<T>& rhs) :
+        ptr_(rhs.ptr_)
+    {
+    }
+    
+    /// Assign from a non-const iterator
+    RandomAccessConstIterator<T>& operator = (const RandomAccessIterator<T>& rhs) { ptr_ = rhs.ptr_; return *this; }
+    /// Point to the object
+    const T* operator -> () const { return ptr_; }
+    /// Dereference the object
+    const T& operator * () const { return *ptr_; }
+    /// Preincrement the pointer
+    RandomAccessConstIterator<T>& operator ++ () { ++ptr_; return *this; }
+    /// Postincrement the pointer
+    RandomAccessConstIterator<T> operator ++ (int) { RandomAccessConstIterator<T> it = *this; ++ptr_; return it; }
+    /// Predecrement the pointer
+    RandomAccessConstIterator<T>& operator -- () { --ptr_; return *this; }
+    /// Postdecrement the pointer
+    RandomAccessConstIterator<T> operator -- (int) { RandomAccessConstIterator<T> it = *this; --ptr_; return it; }
+    /// Add an offset to the pointer
+    RandomAccessConstIterator<T>& operator += (int value) { ptr_ += value; return *this; }
+    /// Subtract an offset from the pointer
+    RandomAccessConstIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
+    /// Add an offset to the pointer
+    RandomAccessConstIterator<T> operator + (int value) const { return RandomAccessConstIterator<T>(ptr_ + value); }
+    /// Subtract an offset from the pointer
+    RandomAccessConstIterator<T> operator - (int value) const { return RandomAccessConstIterator<T>(ptr_ - value); }
+    /// Calculate offset to another iterator
+    int operator - (const RandomAccessConstIterator& rhs) const { return ptr_ - rhs.ptr_; }
+    /// Test for equality with another iterator
+    bool operator == (const RandomAccessConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
+    /// Test for inequality with another iterator
+    bool operator != (const RandomAccessConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
+    /// Test for less than with another iterator
+    bool operator < (const RandomAccessConstIterator& rhs) const { return ptr_ < rhs.ptr_; }
+    /// Test for greater than with another iterator
+    bool operator > (const RandomAccessConstIterator& rhs) const { return ptr_ > rhs.ptr_; }
+    
+    /// Pointer
+    T* ptr_;
+};
+
 /// Vector base class
 class VectorBase
 {
@@ -45,9 +143,6 @@ public:
         ::Swap(buffer_, rhs.buffer_);
     }
     
-    /// Minimum dynamic allocation size
-    static const unsigned MIN_CAPACITY = 2;
-    
 protected:
     /// Size of vector
     unsigned size_;

+ 2 - 2
Engine/Script/Addons.cpp

@@ -747,8 +747,8 @@ void RegisterString(asIScriptEngine *engine)
     engine->RegisterObjectMethod("String", "void Resize(uint)", asFUNCTION(StringResize), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("String", "int Find(const String& in, uint start = 0) const", asMETHODPR(String, Find, (const String&, unsigned) const, unsigned), asCALL_THISCALL);
     engine->RegisterObjectMethod("String", "int Find(uint8, uint start = 0) const", asMETHODPR(String, Find, (char, unsigned) const, unsigned), asCALL_THISCALL);
-    engine->RegisterObjectMethod("String", "int FindLast(const String& in) const", asMETHODPR(String, FindLast, (const String&) const, unsigned), asCALL_THISCALL);
-    engine->RegisterObjectMethod("String", "int FindLast(uint8) const", asMETHODPR(String, FindLast, (char) const, unsigned), asCALL_THISCALL);
+    engine->RegisterObjectMethod("String", "int FindLast(const String& in, uint start = 0xffffffff) const", asMETHODPR(String, FindLast, (const String&, unsigned) const, unsigned), asCALL_THISCALL);
+    engine->RegisterObjectMethod("String", "int FindLast(uint8, uint start = 0xffffffff) const", asMETHODPR(String, FindLast, (char, unsigned) const, unsigned), asCALL_THISCALL);
     engine->RegisterObjectMethod("String", "String Replace(uint8, uint8) const", asMETHODPR(String, Replace, (char, char) const, String), asCALL_THISCALL);
     engine->RegisterObjectMethod("String", "String Replace(const String&in, const String&in) const", asMETHODPR(String, Replace, (const String&, const String&) const, String), asCALL_THISCALL);
     engine->RegisterObjectMethod("String", "String Substring(uint) const", asMETHODPR(String, Substring, (unsigned) const, String), asCALL_THISCALL);