Browse Source

Make RandomAccessIterator std compatible | Replace LowerBound & UpperBound by std functions [cache clear]

https://github.com/urho3d/Urho3D/pull/2939
1vanK 3 years ago
parent
commit
e529edea21

+ 0 - 57
Source/Urho3D/Base/Algorithm.h

@@ -1,57 +0,0 @@
-// Copyright (c) 2008-2022 the Urho3D project
-// License: MIT
-
-#pragma once
-
-#include "../Urho3DConfig.h"
-
-namespace Urho3D
-{
-
-/// Returns an iterator pointing to the first element in the range [first, last) that is not less than value.
-template <class TRandomAccessIterator, class T>
-TRandomAccessIterator LowerBound(TRandomAccessIterator first, TRandomAccessIterator last, const T& value)
-{
-    unsigned count = last - first;
-
-    while (count > 0)
-    {
-        const unsigned step = count / 2;
-        const TRandomAccessIterator it = first + step;
-        if (*it < value)
-        {
-            first = it + 1;
-            count -= step + 1;
-        }
-        else
-        {
-            count = step;
-        }
-    }
-    return first;
-}
-
-/// Returns an iterator pointing to the first element in the range [first, last) that is greater than value.
-template <class TRandomAccessIterator, class T>
-TRandomAccessIterator UpperBound(TRandomAccessIterator first, TRandomAccessIterator last, const T& value)
-{
-    unsigned count = last - first;
-
-    while (count > 0)
-    {
-        const unsigned step = count / 2;
-        const TRandomAccessIterator it = first + step;
-        if (!(value < *it))
-        {
-            first = it + 1;
-            count -= step + 1;
-        }
-        else
-        {
-            count = step;
-        };
-    }
-    return first;
-}
-
-}

+ 15 - 1
Source/Urho3D/Base/Iter.h → Source/Urho3D/Container/Iter.h

@@ -3,7 +3,9 @@
 
 #pragma once
 
-#include "../Urho3DConfig.h"
+#include "../Base/PrimitiveTypes.h"
+
+#include <iterator>
 
 namespace Urho3D
 {
@@ -11,6 +13,12 @@ namespace Urho3D
 /// Random access iterator.
 template <class T> struct RandomAccessIterator
 {
+    using iterator_category = std::random_access_iterator_tag;
+    using difference_type = i32;
+    using value_type = T;
+    using pointer = T*;
+    using reference = T&;
+
     /// Construct.
     constexpr RandomAccessIterator() :
         ptr_(nullptr)
@@ -107,6 +115,12 @@ template <class T> struct RandomAccessIterator
 /// Random access const iterator.
 template <class T> struct RandomAccessConstIterator
 {
+    using iterator_category = std::random_access_iterator_tag;
+    using difference_type = i32;
+    using value_type = T;
+    using pointer = T*;
+    using reference = T&;
+
     /// Construct.
     constexpr RandomAccessConstIterator() :
         ptr_(0)

+ 1 - 2
Source/Urho3D/Container/VectorBase.h

@@ -9,9 +9,8 @@
 #include <Urho3D/Urho3D.h>
 #endif
 
-#include "../Base/Iter.h"
-#include "../Base/PrimitiveTypes.h"
 #include "../Container/Swap.h"
+#include "Iter.h"
 
 namespace Urho3D
 {

+ 4 - 3
Source/Urho3D/Engine/Console.cpp

@@ -4,7 +4,6 @@
 #include "../Precompiled.h"
 
 #include "../Core/Context.h"
-#include "../Base/Algorithm.h"
 #include "../Core/CoreEvents.h"
 #include "../Engine/Console.h"
 #include "../Engine/EngineEvents.h"
@@ -22,6 +21,8 @@
 #include "../UI/UI.h"
 #include "../UI/UIEvents.h"
 
+#include <algorithm>
+
 #include "../DebugNew.h"
 
 namespace Urho3D
@@ -234,7 +235,7 @@ void Console::SetFocusOnShow(bool enable)
 void Console::AddAutoComplete(const String& option)
 {
     // Sorted insertion
-    Vector<String>::Iterator iter = UpperBound(autoComplete_.Begin(), autoComplete_.End(), option);
+    Vector<String>::Iterator iter = std::upper_bound(autoComplete_.Begin(), autoComplete_.End(), option);
     if (!iter.ptr_)
         autoComplete_.Push(option);
     // Make sure it isn't a duplicate
@@ -245,7 +246,7 @@ void Console::AddAutoComplete(const String& option)
 void Console::RemoveAutoComplete(const String& option)
 {
     // Erase and keep ordered
-    autoComplete_.Erase(LowerBound(autoComplete_.Begin(), autoComplete_.End(), option));
+    autoComplete_.Erase(std::lower_bound(autoComplete_.Begin(), autoComplete_.End(), option));
     if (autoCompletePosition_ > autoComplete_.Size())
         autoCompletePosition_ = autoComplete_.Size();
 }