ULTIMA4: Further implementation of std replacement classes

This commit is contained in:
Paul Gilbert 2020-03-10 22:27:20 -07:00 committed by Paul Gilbert
parent 914fad839d
commit eead5589ee
2 changed files with 95 additions and 11 deletions

View File

@ -26,6 +26,7 @@
#include "common/algorithm.h"
#include "common/array.h"
#include "common/hashmap.h"
#include "common/hash-str.h"
#include "common/list.h"
#include "common/queue.h"
#include "common/stack.h"
@ -33,6 +34,17 @@
namespace Ultima {
namespace Std {
template<class T1, class T2>
struct pair {
T1 first;
T2 second;
pair() {
}
pair(T1 first_, T2 second_) : first(first_), second(second_) {
}
};
template<class T>
class vector : public Common::Array<T> {
public:
@ -44,7 +56,7 @@ public:
reverse_iterator(vector<T> *owner, int index) : _owner(owner), _index(index) {}
reverse_iterator() : _owner(0), _index(-1) {}
T operator*() const { return (*_owner)[_index]; }
T &operator*() { return (*_owner)[_index]; }
reverse_iterator &operator++() {
--_index;
@ -58,6 +70,33 @@ public:
return !operator==(rhs);
}
};
struct const_reverse_iterator {
private:
const vector<T> *_owner;
int _index;
public:
const_reverse_iterator(const vector<T> *owner, int index) : _owner(owner), _index(index) {
}
const_reverse_iterator() : _owner(0), _index(-1) {
}
const T operator*() const {
return (*_owner)[_index];
}
const_reverse_iterator &operator++() {
--_index;
return *this;
}
bool operator==(const const_reverse_iterator &rhs) {
return _owner == rhs._owner && _index == rhs._index;
}
bool operator!=(const const_reverse_iterator &rhs) {
return !operator==(rhs);
}
};
public:
typedef T reference;
typedef const T const_reference;
@ -66,6 +105,9 @@ public:
vector(size_t newSize) : Common::Array<T>() {
Common::Array<T>::reserve(newSize);
}
vector(size_t newSize, const T elem) {
resize(newSize, elem);
}
typename Common::Array<T>::iterator erase(typename Common::Array<T>::iterator pos) {
return Common::Array<T>::erase(pos);
@ -97,10 +139,30 @@ public:
reverse_iterator rend() {
return reverse_iterator(this, -1);
}
const_reverse_iterator rbegin() const {
return const_reverse_iterator(this, (int)Common::Array<T>::size() - 1);
}
const_reverse_iterator rend() const {
return const_reverse_iterator(this, -1);
}
void pop_front() {
Common::Array<T>::remove_at(0);
}
void resize(size_t newSize) {
Common::Array<T>::resize(newSize);
}
void resize(size_t newSize, const T elem) {
size_t oldSize = Common::Array<T>::size();
resize(newSize);
for (size_t idx = oldSize; idx < newSize; ++idx)
this->operator[](idx) = elem;
}
T at(size_t index) {
return (*this)[index];
}
};
template<class T>
@ -128,6 +190,8 @@ public:
iterator begin() { return _items.begin(); }
iterator end() { return _items.end(); }
const_iterator begin() const { return _items.begin(); }
const_iterator end() const { return _items.end(); }
/**
* Clear the set
@ -163,20 +227,49 @@ public:
/**
* Find an item
*/
iterator find(T item) {
iterator find(const T item) {
iterator it = begin();
for (; it != end() && *it != item; ++it) {}
return it;
}
const_iterator find(const T item) const {
const_iterator it = begin();
for (; it != end() && *it != item; ++it) {
}
return it;
}
};
struct PointerHash {
Common::Hash<const char *> hash;
uint operator()(const void *ptr) const {
Common::String str = Common::String::format("%p", ptr);
return hash.operator()(str.c_str());
}
};
template<class Key, class Val, class HashFunc = Common::Hash<Key>,
class EqualFunc = Common::EqualTo<Key> >
class map : public Common::HashMap<Key, Val, HashFunc, EqualFunc> {
public:
void insert(Std::pair<Key, Val> elem) {
this->operator[](elem.first) = elem.second;
}
};
template<class VAL>
class deque : public Common::List<VAL> {
public:
VAL operator[](uint index) {
for (typename Common::List<VAL>::iterator it = this->begin();
it != this->end() && index > 0; ++it, --index) {
if (index == 0)
return *it;
}
error("Invalid index");
}
};
template<class T>

View File

@ -30,15 +30,6 @@ namespace Std {
enum Precision { hex = 16, dec = 10 };
template<class T1, class T2>
struct pair {
T1 first;
T2 second;
pair() {}
pair(T1 first_, T2 second_) : first(first_), second(second_) {}
};
extern void memset(void *data, byte val, size_t count);
extern int memcmp(const void *ptr1, const void *ptr2, size_t num);
extern void *memcpy(void *destination, const void *source, size_t num);