Add rstl::binary_find

Including CAnimData::GetBoundingBox


Former-commit-id: e4a864880b
This commit is contained in:
Henrique Gemignani Passos Lima
2022-12-05 23:35:31 +02:00
parent b29e5337fc
commit d86009a79d
19 changed files with 232 additions and 87 deletions

View File

@@ -1,6 +1,8 @@
#ifndef _RSTL_ALGORITHM
#define _RSTL_ALGORITHM
#include "rstl/functional.hpp"
#include "rstl/pair.hpp"
#include "rstl/pointer_iterator.hpp"
namespace rstl {
@@ -67,7 +69,7 @@ void __sort3(T& a, T& b, T& c, Cmp comp) {
template < typename It, class Cmp >
void sort(It first, It last, Cmp cmp) {
int count = last - first; // use distance?
int count = last - first; // use distance?
if (count > 1) {
if (count <= 20) {
__insertion_sort(first, last, cmp);
@@ -93,6 +95,87 @@ void sort(It first, It last, Cmp cmp) {
}
}
template < typename It, typename T, typename Cmp >
It lower_bound(It start, It end, const T& value, Cmp cmp) {
int dist = distance(start, end);
It it = start;
while (dist > 0) {
it = start;
int halfDist = dist / 2;
advance(it, halfDist);
if (cmp(*it, value)) {
start = it;
++start;
dist = (dist - halfDist) - 1;
} else {
dist = halfDist;
}
}
return start;
}
template < typename It, typename T, typename Cmp >
It binary_find(It start, It end, const T& value, Cmp cmp) {
It lower = lower_bound(start, end, value, cmp);
bool found = false;
if (lower != end && !cmp(value, *lower)) {
found = true;
}
if (found) {
return lower;
} else {
return end;
}
}
template < typename T, typename Cmp >
class pair_sorter_finder;
template < typename K, typename V, typename Cmp >
class pair_sorter_finder< pair< K, V >, Cmp > {
public:
typedef K key_type;
Cmp cmp;
pair_sorter_finder(const Cmp& cmp) : cmp(cmp) {}
bool operator()(const K& a, const pair< K, V >& b) const;
/* {
return cmp(a, b.first);
}*/
bool operator()(const pair< K, V >& a, const K& b) const;
/* {
return cmp(a.first, b);
}*/
};
template < typename K, typename V, typename Cmp >
bool pair_sorter_finder< pair< K, V >, Cmp >::operator()(const K& a, const pair< K, V >& b) const {
return cmp(a, b.first);
}
template < typename K, typename V, typename Cmp >
bool pair_sorter_finder< pair< K, V >, Cmp >::operator()(const pair< K, V >& a, const K& b) const {
return cmp(a.first, b);
}
template < typename T >
typename T::const_iterator
find_by_key(const T& container,
const typename select1st< typename T::value_type >::value_type& key);
template < typename T >
typename T::const_iterator
find_by_key(const T& container,
const typename select1st< typename T::value_type >::value_type& key) {
typedef typename select1st< typename T::value_type >::value_type K;
return binary_find(container.begin(), container.end(), key,
pair_sorter_finder< typename T::value_type, less< K > >(rstl::less< K >()));
}
} // namespace rstl
#endif // _RSTL_ALGORITHM

View File

@@ -0,0 +1,18 @@
#ifndef _RSTL_FUNCTIONAL
#define _RSTL_FUNCTIONAL
namespace rstl {
template < typename P >
struct identity {
const P& operator()(const P& it) const { return it; }
};
template < typename T >
struct less {
bool operator()(const T& a, const T& b) const { return a < b; }
};
} // namespace rstl
#endif // _RSTL_FUNCTIONAL

View File

@@ -25,6 +25,18 @@ inline typename It::difference_type distance(It first, It last) {
return __distance(first, last, typename It::iterator_category());
}
template < typename It, typename S >
void __advance(It& it, S count, forward_iterator_tag) {
while (count > 0) {
++it;
}
}
template < typename It, typename S >
inline void advance(It& it, S count) {
return __advance(it, count, typename It::iterator_category());
}
} // namespace rstl
#endif // _RSTL_ITERATOR

View File

@@ -15,6 +15,20 @@ public:
L first;
R second;
};
template < typename P >
struct select1st {
const P& operator()(const P& it) const { return it; }
};
template < typename K, typename V >
struct select1st< pair< K, V > > {
typedef K value_type;
const K& operator()(const pair< K, V >& it) const { return it.first; }
};
} // namespace rstl
#endif // _RSTL_PAIR

View File

@@ -132,6 +132,10 @@ inline typename It::difference_type __distance(It first, It last, random_access_
return last - first;
}
template < typename It, typename S >
inline void __advance(It& it, S count, random_access_iterator_tag) {
it += count;
}
template < typename T >
struct iterator_traits {};

View File

@@ -3,30 +3,12 @@
#include "types.h"
#include "rstl/functional.hpp"
#include "rstl/iterator.hpp"
#include "rstl/pair.hpp"
#include "rstl/rmemory_allocator.hpp"
#include "rstl/iterator.hpp"
namespace rstl {
template < typename P >
struct select1st {
const P& operator()(const P& it) const { return it; }
};
template < typename K, typename V >
struct select1st< pair< K, V > > {
const K& operator()(const pair< K, V >& it) const { return it.first; }
};
template < typename P >
struct identity {
const P& operator()(const P& it) const { return it; }
};
template < typename T >
struct less {
bool operator()(const T& a, const T& b) const { return a < b; }
};
enum node_color {
kNC_Red,
@@ -52,9 +34,7 @@ private:
: mLeft(left), mRight(right), mParent(parent), mColor(color) {
construct(get_value(), value);
}
~node() {
get_value()->~P();
}
~node() { get_value()->~P(); }
P* get_value() { return reinterpret_cast< P* >(&mValue); }
const P* get_value() const { return reinterpret_cast< const P* >(&mValue); }
@@ -93,7 +73,7 @@ public:
// TODO why is this bool here?
const_iterator(node* node, const header* header, bool b)
: mNode(node), mHeader(header)/*, x8_(b)*/ {}
: mNode(node), mHeader(header) /*, x8_(b)*/ {}
const P* operator->() const { return mNode->get_value(); }
const P* operator*() const { return mNode->get_value(); }
@@ -191,7 +171,7 @@ public:
}
return iterator(needle, &x8_header, false);
}
iterator erase(iterator it) {
node* node = it.get_node();
++it;
@@ -245,7 +225,9 @@ private:
void rebalance(node* n) { rbtree_rebalance(&x8_header, n); }
node* rebalance_for_erase(node* n) { return static_cast<node*>(rbtree_rebalance_for_erase(&x8_header, n)); }
node* rebalance_for_erase(node* n) {
return static_cast< node* >(rbtree_rebalance_for_erase(&x8_header, n));
}
};
static bool kUnknownValueNewRoot = true;

View File

@@ -107,6 +107,7 @@ public:
int _eq_helper(const basic_string& other) const;
bool operator==(const basic_string& other) const;
bool operator<(const basic_string& other) const;
const char* data() const { return x0_ptr; }
};
@@ -116,6 +117,11 @@ bool basic_string< _CharTp, Traits, Alloc >::operator==(const basic_string& othe
return _eq_helper(other) == 0;
}
template < typename _CharTp, typename Traits, typename Alloc >
bool basic_string< _CharTp, Traits, Alloc >::operator<(const basic_string& other) const {
return _eq_helper(other) < 0;
}
// template <>
// const char basic_string<char>::mNull = 0;
// template <>

View File

@@ -36,6 +36,7 @@ protected:
public:
typedef pointer_iterator< T, vector< T, Alloc >, Alloc > iterator;
typedef const_pointer_iterator< T, vector< T, Alloc >, Alloc > const_iterator;
typedef T value_type;
iterator begin() { return iterator(xc_items); }
const_iterator begin() const { return const_iterator(xc_items); }