Add rather inefficient type_identity wrapping for std::set.

Accessing an element is O(N), as if it was walking a list.
develop
Alexander Gavrilov 2012-08-18 14:34:20 +04:00
parent 24cc8b5c7a
commit c6694e386f
4 changed files with 45 additions and 1 deletions

@ -285,6 +285,9 @@ void container_identity::lua_item_read(lua_State *state, int fname_idx, void *pt
void container_identity::lua_item_write(lua_State *state, int fname_idx, void *ptr, int idx, int val_index) void container_identity::lua_item_write(lua_State *state, int fname_idx, void *ptr, int idx, int val_index)
{ {
if (is_readonly())
field_error(state, fname_idx, "container is read-only", "write");
auto id = (type_identity*)lua_touserdata(state, UPVAL_ITEM_ID); auto id = (type_identity*)lua_touserdata(state, UPVAL_ITEM_ID);
void *pitem = item_pointer(id, ptr, idx); void *pitem = item_pointer(id, ptr, idx);
id->lua_write(state, fname_idx, pitem, val_index); id->lua_write(state, fname_idx, pitem, val_index);

@ -28,6 +28,7 @@ distribution.
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <map> #include <map>
#include <set>
#include "Core.h" #include "Core.h"
#include "BitArray.h" #include "BitArray.h"

@ -115,6 +115,8 @@ namespace DFHack
virtual void lua_item_read(lua_State *state, int fname_idx, void *ptr, int idx); virtual void lua_item_read(lua_State *state, int fname_idx, void *ptr, int idx);
virtual void lua_item_write(lua_State *state, int fname_idx, void *ptr, int idx, int val_index); virtual void lua_item_write(lua_State *state, int fname_idx, void *ptr, int idx, int val_index);
virtual bool is_readonly() { return false; }
virtual bool resize(void *ptr, int size) { return false; } virtual bool resize(void *ptr, int size) { return false; }
virtual bool erase(void *ptr, int index) { return false; } virtual bool erase(void *ptr, int index) { return false; }
virtual bool insert(void *ptr, int index, void *pitem) { return false; } virtual bool insert(void *ptr, int index, void *pitem) { return false; }
@ -343,6 +345,33 @@ namespace df
} }
}; };
template<class T>
class ro_stl_container_identity : public container_identity {
const char *name;
public:
ro_stl_container_identity(const char *name, type_identity *item, enum_identity *ienum = NULL)
: container_identity(sizeof(T), &allocator_fn<T>, item, ienum), name(name)
{}
std::string getFullName(type_identity *item) {
return name + container_identity::getFullName(item);
}
virtual bool is_readonly() { return true; }
virtual bool resize(void *ptr, int size) { return false; }
virtual bool erase(void *ptr, int size) { return false; }
virtual bool insert(void *ptr, int idx, void *item) { return false; }
protected:
virtual int item_count(void *ptr, CountMode) { return ((T*)ptr)->size(); }
virtual void *item_pointer(type_identity *item, void *ptr, int idx) {
auto iter = (*(T*)ptr).begin();
for (; idx > 0; idx--) ++iter;
return (void*)&*iter;
}
};
class bit_array_identity : public bit_container_identity { class bit_array_identity : public bit_container_identity {
public: public:
/* /*
@ -517,6 +546,10 @@ namespace df
static container_identity *get(); static container_identity *get();
}; };
template<class T> struct identity_traits<std::set<T> > {
static container_identity *get();
};
template<> struct identity_traits<BitArray<int> > { template<> struct identity_traits<BitArray<int> > {
static bit_array_identity identity; static bit_array_identity identity;
static bit_container_identity *get() { return &identity; } static bit_container_identity *get() { return &identity; }
@ -579,6 +612,13 @@ namespace df
return &identity; return &identity;
} }
template<class T>
inline container_identity *identity_traits<std::set<T> >::get() {
typedef std::set<T> container;
static ro_stl_container_identity<container> identity("set", identity_traits<T>::get());
return &identity;
}
template<class T> template<class T>
inline bit_container_identity *identity_traits<BitArray<T> >::get() { inline bit_container_identity *identity_traits<BitArray<T> >::get() {
static bit_array_identity identity(identity_traits<T>::get()); static bit_array_identity identity(identity_traits<T>::get());

@ -1 +1 @@
Subproject commit 9f91e74767b4d583b580d46e16143216ba62ae66 Subproject commit 1eeaa08360c39a9a2d811544c2443309adc1a8f1