-registering same system multiple times only replace pointer for callback
-added export attributes (required by windows to make DLL library, not work at now due to DMD bugs) -interface D files to import
This commit is contained in:
parent
cb0f56b0fb
commit
d0fcdba6cd
20 changed files with 1322 additions and 105 deletions
|
|
@ -26,7 +26,7 @@ struct Entity
|
|||
}
|
||||
}
|
||||
|
||||
struct EntityTemplate
|
||||
export struct EntityTemplate
|
||||
{
|
||||
ubyte[] entity_data;
|
||||
EntityManager.EntityInfo* info;
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ private enum HASH_EMPTY = 0;
|
|||
private enum HASH_DELETED = 0x1;
|
||||
private enum HASH_FILLED_MARK = ulong(1) << 8 * ulong.sizeof - 1;
|
||||
|
||||
ulong defaultHashFunc(T)(auto ref T t) {
|
||||
export ulong defaultHashFunc(T)(auto ref T t) {
|
||||
static if (isIntegral!(T)) {
|
||||
return hashInt(t);
|
||||
} else {
|
||||
|
|
@ -20,7 +20,7 @@ ulong defaultHashFunc(T)(auto ref T t) {
|
|||
}
|
||||
|
||||
// Can turn bad hash function to good one
|
||||
ulong hashInt(ulong x) nothrow @nogc @safe {
|
||||
export ulong hashInt(ulong x) nothrow @nogc @safe {
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
x = x ^ (x >> 31);
|
||||
|
|
@ -48,27 +48,27 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
size_t length; // Used to compute loadFactor
|
||||
size_t markerdDeleted;
|
||||
|
||||
void clear() {
|
||||
export void clear() {
|
||||
elements.clear();
|
||||
length = 0;
|
||||
markerdDeleted = 0;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
export void reset() {
|
||||
elements.reset();
|
||||
length = 0;
|
||||
markerdDeleted = 0;
|
||||
}
|
||||
|
||||
bool isIn(ref Key el) {
|
||||
export bool isIn(ref Key el) {
|
||||
return getIndex(el) != getIndexEmptyValue;
|
||||
}
|
||||
|
||||
bool isIn(Key el) {
|
||||
export bool isIn(Key el) {
|
||||
return getIndex(el) != getIndexEmptyValue;
|
||||
}
|
||||
|
||||
Value* getPtr()(auto ref Key k) {
|
||||
export Value* getPtr()(auto ref Key k) {
|
||||
size_t index = getIndex(k);
|
||||
if (index == getIndexEmptyValue) {
|
||||
return null;
|
||||
|
|
@ -77,18 +77,18 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
}
|
||||
}
|
||||
|
||||
ref Value get()(auto ref Key k) {
|
||||
export ref Value get()(auto ref Key k) {
|
||||
size_t index = getIndex(k);
|
||||
assert(index != getIndexEmptyValue);
|
||||
return elements[index].keyValue.value;
|
||||
}
|
||||
|
||||
deprecated("Use get with second parameter.") auto ref Value getDefault()(
|
||||
deprecated("Use get with second parameter.") export auto ref Value getDefault()(
|
||||
auto ref Key k, auto ref Value defaultValue) {
|
||||
return get(k, defaultValue);
|
||||
}
|
||||
|
||||
auto ref Value get()(auto ref Key k, auto ref Value defaultValue) {
|
||||
export auto ref Value get()(auto ref Key k, auto ref Value defaultValue) {
|
||||
size_t index = getIndex(k);
|
||||
if (index == getIndexEmptyValue) {
|
||||
return defaultValue;
|
||||
|
|
@ -97,7 +97,7 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
}
|
||||
}
|
||||
|
||||
ref Value getInsertDefault()(auto ref Key k, auto ref Value defaultValue) {
|
||||
export ref Value getInsertDefault()(auto ref Key k, auto ref Value defaultValue) {
|
||||
size_t index = getIndex(k);
|
||||
if (index == getIndexEmptyValue) {
|
||||
add(k, defaultValue);
|
||||
|
|
@ -108,7 +108,7 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
|
||||
}
|
||||
|
||||
bool tryRemove(Key el) {
|
||||
export bool tryRemove(Key el) {
|
||||
size_t index = getIndex(el);
|
||||
if (index == getIndexEmptyValue) {
|
||||
return false;
|
||||
|
|
@ -119,20 +119,20 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
return true;
|
||||
}
|
||||
|
||||
void remove(Key el) {
|
||||
export void remove(Key el) {
|
||||
bool ok = tryRemove(el);
|
||||
assert(ok);
|
||||
}
|
||||
|
||||
ref Value opIndex()(auto ref Key key) {
|
||||
export ref Value opIndex()(auto ref Key key) {
|
||||
return get(key);
|
||||
}
|
||||
|
||||
void opIndexAssign()(auto ref Value value, auto ref Key key) {
|
||||
export void opIndexAssign()(auto ref Value value, auto ref Key key) {
|
||||
add(key, value);
|
||||
}
|
||||
|
||||
void add()(auto ref Key key, auto ref Value value) {
|
||||
export void add()(auto ref Key key, auto ref Value value) {
|
||||
size_t index = getIndex(key);
|
||||
if (index != getIndexEmptyValue) {
|
||||
elements[index].keyValue.value = value;
|
||||
|
|
@ -170,11 +170,11 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
//int numA;
|
||||
//int numB;
|
||||
|
||||
size_t getIndex(Key el) {
|
||||
export size_t getIndex(Key el) {
|
||||
return getIndex(el);
|
||||
}
|
||||
|
||||
size_t getIndex(ref Key el) {
|
||||
export size_t getIndex(ref Key el) {
|
||||
mixin(doNotInline);
|
||||
|
||||
immutable size_t groupsLength = elements.length;
|
||||
|
|
@ -203,14 +203,14 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
|
||||
}
|
||||
|
||||
float getLoadFactor(size_t forElementsNum) {
|
||||
export float getLoadFactor(size_t forElementsNum) {
|
||||
if (elements.length == 0) {
|
||||
return 1;
|
||||
}
|
||||
return cast(float) forElementsNum / (elements.length);
|
||||
}
|
||||
|
||||
void rehash() {
|
||||
export void rehash() {
|
||||
mixin(doNotInline);
|
||||
// Get all elements
|
||||
Vector!KeyVal allElements;
|
||||
|
|
@ -239,7 +239,7 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
}
|
||||
|
||||
// foreach support
|
||||
int opApply(DG)(scope DG dg) {
|
||||
export int opApply(DG)(scope DG dg) {
|
||||
int result;
|
||||
foreach (ref Bucket gr; elements) {
|
||||
if ((gr.hash & HASH_FILLED_MARK) == 0) {
|
||||
|
|
@ -262,7 +262,7 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
return result;
|
||||
}
|
||||
|
||||
int byKey(scope int delegate(Key k) dg) {
|
||||
export int byKey(scope int delegate(Key k) dg) {
|
||||
int result;
|
||||
foreach (ref Key k; this) {
|
||||
result = dg(k);
|
||||
|
|
@ -272,7 +272,7 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
return result;
|
||||
}
|
||||
|
||||
int byValue(scope int delegate(ref Value k) dg) {
|
||||
export int byValue(scope int delegate(ref Value k) dg) {
|
||||
int result;
|
||||
foreach (ref Value v; this) {
|
||||
result = dg(v);
|
||||
|
|
@ -282,7 +282,7 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
return result;
|
||||
}
|
||||
|
||||
int byKeyValue(scope int delegate(ref Key k, ref Value v) dg) {
|
||||
export int byKeyValue(scope int delegate(ref Key k, ref Value v) dg) {
|
||||
int result;
|
||||
foreach (ref Key k, ref Value v; this) {
|
||||
result = dg(k, v);
|
||||
|
|
@ -297,7 +297,7 @@ struct HashMap(KeyPar, ValuePar, alias hashFunc = defaultHashFunc) {
|
|||
/**
|
||||
* Preety print
|
||||
*/
|
||||
void toString(scope void delegate(const(char)[]) sink, FormatSpec!char fmt) {
|
||||
export void toString(scope void delegate(const(char)[]) sink, FormatSpec!char fmt) {
|
||||
formatValue(sink, '[', fmt);
|
||||
foreach (ref k, ref v; &byKeyValue) {
|
||||
formatValue(sink, k, fmt);
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ struct IDManager
|
|||
m_ids_array[entity.id.id].entity = &entity;
|
||||
}
|
||||
|
||||
Entity* getEntityPointer(EntityID id)
|
||||
export Entity* getEntityPointer(EntityID id)
|
||||
{
|
||||
Data* data = &m_ids_array[id.id];
|
||||
if (data.counter != id.counter)
|
||||
|
|
@ -42,7 +42,7 @@ struct IDManager
|
|||
return data.entity;
|
||||
}
|
||||
|
||||
bool isExist(EntityID id)
|
||||
export bool isExist(EntityID id)
|
||||
{
|
||||
Data* data = &m_ids_array[id.id];
|
||||
return data.counter == id.counter;
|
||||
|
|
|
|||
|
|
@ -22,12 +22,12 @@ alias SerializeVector = ecs.vector.Vector!ubyte;
|
|||
class EntityManager
|
||||
{
|
||||
|
||||
static void initialize()
|
||||
export static void initialize()
|
||||
{
|
||||
instance = Mallocator.instance.make!EntityManager;
|
||||
}
|
||||
|
||||
static void destroy()
|
||||
export static void destroy()
|
||||
{
|
||||
|
||||
foreach (ref system; instance.systems)
|
||||
|
|
@ -204,16 +204,27 @@ class EntityManager
|
|||
//system.m_components = Mallocator.instance.makeArray!uint(types.length - 1);
|
||||
mixin(genCompList());
|
||||
|
||||
systems.add(system);
|
||||
|
||||
if (system.m_create)
|
||||
system.m_create(system.m_system_pointer);
|
||||
|
||||
systems[$ - 1].enable();
|
||||
|
||||
foreach (info; &entities_infos.byValue)
|
||||
ushort sys_id = systems_map.get(Sys.stringof, ushort.max);
|
||||
if(sys_id < systems.length)
|
||||
{
|
||||
addEntityCaller(*info, cast(uint) systems.length - 1);
|
||||
system.enable();
|
||||
systems[sys_id] = system;
|
||||
}
|
||||
else
|
||||
{
|
||||
systems_map.add(Sys.stringof,cast(ushort)systems.length);
|
||||
|
||||
systems.add(system);
|
||||
|
||||
if (system.m_create)
|
||||
system.m_create(system.m_system_pointer);
|
||||
|
||||
systems[$ - 1].enable();
|
||||
|
||||
foreach (info; &entities_infos.byValue)
|
||||
{
|
||||
addEntityCaller(*info, cast(uint) systems.length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
updateEntityCallers();
|
||||
|
|
@ -246,12 +257,20 @@ class EntityManager
|
|||
info.init_data = Mallocator.instance.makeArray!ubyte(Comp.sizeof);
|
||||
*cast(Comp*) info.init_data.ptr = Comp.init; // = Comp();
|
||||
|
||||
components.add(info);
|
||||
Comp.component_id = cast(ushort)(components.length - 1);
|
||||
components_map.add(Comp.stringof, cast(ushort)(components.length - 1));
|
||||
ushort comp_id = components_map.get(Comp.stringof, ushort.max);
|
||||
if(comp_id < components.length)
|
||||
{
|
||||
Comp.component_id = comp_id;
|
||||
}
|
||||
else
|
||||
{
|
||||
components.add(info);
|
||||
Comp.component_id = cast(ushort)(components.length - 1);
|
||||
components_map.add(Comp.stringof, cast(ushort)(components.length - 1));
|
||||
}
|
||||
}
|
||||
|
||||
void update()
|
||||
export void update()
|
||||
{
|
||||
foreach (info; &entities_infos.byValue)
|
||||
{
|
||||
|
|
@ -280,7 +299,7 @@ class EntityManager
|
|||
return 1;
|
||||
}
|
||||
|
||||
EntityTemplate* allocateTemplate(ushort[] components_ids)
|
||||
export EntityTemplate* allocateTemplate(ushort[] components_ids)
|
||||
{
|
||||
|
||||
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * components_ids.length))[0
|
||||
|
|
@ -318,7 +337,7 @@ class EntityManager
|
|||
return temp;
|
||||
}
|
||||
|
||||
EntityInfo* getEntityInfo(ushort[] ids)
|
||||
export EntityInfo* getEntityInfo(ushort[] ids)
|
||||
{
|
||||
EntityInfo* info = entities_infos.get(ids, null);
|
||||
if (info is null)
|
||||
|
|
@ -354,7 +373,7 @@ class EntityManager
|
|||
return info;
|
||||
}
|
||||
|
||||
void updateEntityCallers()
|
||||
export void updateEntityCallers()
|
||||
{
|
||||
foreach (entity; &entities_infos.byValue)
|
||||
{
|
||||
|
|
@ -365,7 +384,7 @@ class EntityManager
|
|||
}
|
||||
}
|
||||
|
||||
void addEntityCaller(ref EntityInfo entity, uint system_id)
|
||||
export void addEntityCaller(ref EntityInfo entity, uint system_id)
|
||||
{
|
||||
System* system = &systems[system_id];
|
||||
CallData call_data = CallData(system_id, system, &entity, null);
|
||||
|
|
@ -412,12 +431,12 @@ class EntityManager
|
|||
entity.callers.add(call_data, index);
|
||||
}
|
||||
|
||||
Entity* getEntity(EntityID id)
|
||||
export Entity* getEntity(EntityID id)
|
||||
{
|
||||
return cast(Entity*) id_manager.getEntityPointer(id);
|
||||
}
|
||||
|
||||
void removeComponents(EntityID entity_id, ushort[] del_ids)
|
||||
export void removeComponents(EntityID entity_id, ushort[] del_ids)
|
||||
{
|
||||
uint num = cast(uint) del_ids.length;
|
||||
change_entities_list.add(0);
|
||||
|
|
@ -426,7 +445,7 @@ class EntityManager
|
|||
change_entities_list.add(cast(ubyte[]) del_ids);
|
||||
}
|
||||
|
||||
void __removeComponents(EntityID entity_id, ushort[] del_ids)
|
||||
private void __removeComponents(EntityID entity_id, ushort[] del_ids)
|
||||
{
|
||||
Entity* entity = id_manager.getEntityPointer(entity_id);
|
||||
EntitiesBlock* block = getMetaData(entity);
|
||||
|
|
@ -493,7 +512,7 @@ class EntityManager
|
|||
removeComponents(entity_id, del_ids);
|
||||
}
|
||||
|
||||
void __addComponents(EntityID entity_id, ushort[] new_ids, void*[] data_pointers)
|
||||
private void __addComponents(EntityID entity_id, ushort[] new_ids, void*[] data_pointers)
|
||||
{
|
||||
uint num = cast(uint) new_ids.length;
|
||||
Entity* entity = id_manager.getEntityPointer(entity_id);
|
||||
|
|
@ -644,13 +663,13 @@ class EntityManager
|
|||
//__addComponents(entity_id, new_ids, pointers);
|
||||
}
|
||||
|
||||
void freeTemplate(EntityTemplate* template_)
|
||||
export void freeTemplate(EntityTemplate* template_)
|
||||
{
|
||||
Mallocator.instance.dispose(template_.entity_data);
|
||||
Mallocator.instance.dispose(template_);
|
||||
}
|
||||
|
||||
ref Entity addEntity(EntityTemplate* tmpl)
|
||||
export ref Entity addEntity(EntityTemplate* tmpl)
|
||||
{
|
||||
EntitiesBlock* block = findBlockWithFreeSpace(tmpl.info);
|
||||
|
||||
|
|
@ -669,7 +688,7 @@ class EntityManager
|
|||
return *entity;
|
||||
}
|
||||
|
||||
EntitiesBlock* findBlockWithFreeSpace(EntityInfo* info)
|
||||
private EntitiesBlock* findBlockWithFreeSpace(EntityInfo* info)
|
||||
{
|
||||
EntitiesBlock* previous_block;
|
||||
EntitiesBlock* block = info.first_with_free_space;
|
||||
|
|
@ -709,12 +728,12 @@ class EntityManager
|
|||
return block;
|
||||
}
|
||||
|
||||
void removeEntity(EntityID id)
|
||||
export void removeEntity(EntityID id)
|
||||
{
|
||||
entities_to_remove.add(id);
|
||||
}
|
||||
|
||||
void __removeEntity(EntityID id)
|
||||
private void __removeEntity(EntityID id)
|
||||
{
|
||||
//get entity and block meta data pointers
|
||||
Entity* entity = id_manager.getEntityPointer(id);
|
||||
|
|
@ -771,12 +790,12 @@ class EntityManager
|
|||
*params:
|
||||
*pointer = pointer to any data of entity (i.e. component data pointer)
|
||||
*/
|
||||
EntitiesBlock* getMetaData(void* pointer)
|
||||
export EntitiesBlock* getMetaData(void* pointer)
|
||||
{
|
||||
return cast(EntitiesBlock*)(cast(size_t) pointer & (~cast(size_t)(page_size - 1)));
|
||||
}
|
||||
|
||||
void changeEntites()
|
||||
private void changeEntites()
|
||||
{
|
||||
uint index = 0;
|
||||
uint len = cast(uint) change_entities_list.length;
|
||||
|
|
@ -814,7 +833,7 @@ class EntityManager
|
|||
change_entities_list.clear();
|
||||
}
|
||||
|
||||
void updateBlocks()
|
||||
private void updateBlocks()
|
||||
{
|
||||
foreach (block; blocks_to_update)
|
||||
{
|
||||
|
|
@ -824,7 +843,7 @@ class EntityManager
|
|||
blocks_to_update.clear();
|
||||
}
|
||||
|
||||
void removeEntities()
|
||||
private void removeEntities()
|
||||
{
|
||||
foreach (id; entities_to_remove)
|
||||
{
|
||||
|
|
@ -833,7 +852,7 @@ class EntityManager
|
|||
entities_to_remove.clear();
|
||||
}
|
||||
|
||||
void begin()
|
||||
export void begin()
|
||||
{
|
||||
updateBlocks();
|
||||
changeEntites();
|
||||
|
|
@ -845,7 +864,7 @@ class EntityManager
|
|||
}
|
||||
}
|
||||
|
||||
void end()
|
||||
export void end()
|
||||
{
|
||||
foreach (ref system; instance.systems)
|
||||
{
|
||||
|
|
@ -900,7 +919,7 @@ class EntityManager
|
|||
}
|
||||
|
||||
///return pointer to first element in block
|
||||
void* dataBegin()
|
||||
export void* dataBegin()
|
||||
{
|
||||
ushort dif = EntitiesBlock.sizeof;
|
||||
alignNum(dif, type_data.alignment);
|
||||
|
|
@ -950,12 +969,39 @@ class EntityManager
|
|||
Vector!ubyte change_entities_list;
|
||||
|
||||
HashMap!(ushort[], EntityInfo*) entities_infos;
|
||||
HashMap!(string, ushort) systems_map;
|
||||
HashMap!(string, ushort) components_map;
|
||||
Vector!System systems;
|
||||
Vector!ComponentInfo components;
|
||||
__gshared EntityManager instance;
|
||||
|
||||
}
|
||||
|
||||
version(Windows)
|
||||
extern(Windows) bool DllMain(void* hInstance, uint ulReason, void*) {
|
||||
import core.sys.windows.windows;
|
||||
import core.sys.windows.dll;
|
||||
switch (ulReason)
|
||||
{
|
||||
default: assert(0);
|
||||
case DLL_PROCESS_ATTACH:
|
||||
dll_process_attach( hInstance, true );
|
||||
break;
|
||||
|
||||
case DLL_PROCESS_DETACH:
|
||||
dll_process_detach( hInstance, true );
|
||||
break;
|
||||
|
||||
case DLL_THREAD_ATTACH:
|
||||
dll_thread_attach( true, true );
|
||||
break;
|
||||
|
||||
case DLL_THREAD_DETACH:
|
||||
dll_thread_detach( true, true );
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
static ulong defaultHashFunc(T)(auto ref T t)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,26 +5,26 @@ import ecs.manager;
|
|||
|
||||
struct System
|
||||
{
|
||||
bool enabled()
|
||||
export bool enabled()
|
||||
{
|
||||
return m_enabled;
|
||||
}
|
||||
|
||||
void enable()
|
||||
export void enable()
|
||||
{
|
||||
if (!m_enabled && m_enable)
|
||||
m_enable(m_system_pointer);
|
||||
m_enabled = true;
|
||||
}
|
||||
|
||||
void disable()
|
||||
export void disable()
|
||||
{
|
||||
if (m_enabled && m_disable)
|
||||
m_disable(m_system_pointer);
|
||||
m_enabled = false;
|
||||
}
|
||||
|
||||
int priority()
|
||||
export int priority()
|
||||
{
|
||||
return m_priority;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import std.algorithm : swap;
|
|||
import std.conv : emplace;
|
||||
import std.traits : hasMember, isCopyable, TemplateOf, Unqual;
|
||||
|
||||
@nogc @safe nothrow pure size_t nextPow2(size_t num) {
|
||||
export @nogc @safe nothrow pure size_t nextPow2(size_t num) {
|
||||
return 1 << bsr(num) + 1;
|
||||
}
|
||||
|
||||
|
|
@ -19,17 +19,17 @@ struct Vector(T) {
|
|||
size_t used;
|
||||
public:
|
||||
|
||||
this()(T t) {
|
||||
export this()(T t) {
|
||||
add(t);
|
||||
}
|
||||
|
||||
this(X)(X[] t) if (is(Unqual!X == Unqual!T)) {
|
||||
export this(X)(X[] t) if (is(Unqual!X == Unqual!T)) {
|
||||
add(t);
|
||||
|
||||
}
|
||||
|
||||
static if (isCopyable!T) {
|
||||
this(this) {
|
||||
export this(this) {
|
||||
T[] tmp = array[0 .. used];
|
||||
array = null;
|
||||
used = 0;
|
||||
|
|
@ -39,15 +39,15 @@ public:
|
|||
@disable this(this);
|
||||
}
|
||||
|
||||
~this() {
|
||||
export ~this() {
|
||||
clear();
|
||||
}
|
||||
|
||||
void clear() {
|
||||
export void clear() {
|
||||
removeAll();
|
||||
}
|
||||
|
||||
void removeAll() {
|
||||
export void removeAll() {
|
||||
if (array !is null) {
|
||||
foreach (ref el; array[0 .. used]) {
|
||||
destroy(el);
|
||||
|
|
@ -59,15 +59,15 @@ public:
|
|||
used = 0;
|
||||
}
|
||||
|
||||
bool empty() {
|
||||
export bool empty() {
|
||||
return (used == 0);
|
||||
}
|
||||
|
||||
size_t length() {
|
||||
export size_t length() {
|
||||
return used;
|
||||
}
|
||||
|
||||
void length(size_t newLength) {
|
||||
export void length(size_t newLength) {
|
||||
if (newLength > used) {
|
||||
reserve(newLength);
|
||||
foreach (ref el; array[used .. newLength]) {
|
||||
|
|
@ -81,34 +81,34 @@ public:
|
|||
used = newLength;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
export void reset() {
|
||||
used = 0;
|
||||
}
|
||||
|
||||
void reserve(size_t numElements) {
|
||||
export void reserve(size_t numElements) {
|
||||
if (numElements > array.length) {
|
||||
extend(numElements);
|
||||
}
|
||||
}
|
||||
|
||||
size_t capacity() {
|
||||
export size_t capacity() {
|
||||
return array.length - used;
|
||||
}
|
||||
|
||||
void extend(size_t newNumOfElements) {
|
||||
export void extend(size_t newNumOfElements) {
|
||||
auto oldArray = manualExtend(array, newNumOfElements);
|
||||
if (oldArray !is null) {
|
||||
freeData(oldArray);
|
||||
}
|
||||
}
|
||||
|
||||
@nogc void freeData(void[] data) {
|
||||
export @nogc void freeData(void[] data) {
|
||||
// 0x0F probably invalid value for pointers and other types
|
||||
memset(data.ptr, 0x0F, data.length); // Makes bugs show up xD
|
||||
free(data.ptr);
|
||||
}
|
||||
|
||||
static void[] manualExtend(ref T[] array, size_t newNumOfElements = 0) {
|
||||
export static void[] manualExtend(ref T[] array, size_t newNumOfElements = 0) {
|
||||
if (newNumOfElements == 0)
|
||||
newNumOfElements = 2;
|
||||
if (array.length == 0)
|
||||
|
|
@ -122,18 +122,18 @@ public:
|
|||
return cast(void[]) oldArray;
|
||||
}
|
||||
|
||||
Vector!T copy()() {
|
||||
export Vector!T copy()() {
|
||||
Vector!T duplicate;
|
||||
duplicate.reserve(used);
|
||||
duplicate ~= array[0 .. used];
|
||||
return duplicate;
|
||||
}
|
||||
|
||||
bool canAddWithoutRealloc(uint elemNum = 1) {
|
||||
export bool canAddWithoutRealloc(uint elemNum = 1) {
|
||||
return used + elemNum <= array.length;
|
||||
}
|
||||
|
||||
void add()(T t) {
|
||||
export void add()(T t) {
|
||||
if (used >= array.length) {
|
||||
extend(nextPow2(used + 1));
|
||||
}
|
||||
|
|
@ -142,7 +142,7 @@ public:
|
|||
}
|
||||
|
||||
/// Add element at given position moving others
|
||||
void add()(T t, size_t pos) {
|
||||
export void add()(T t, size_t pos) {
|
||||
assert(pos <= used);
|
||||
if (used >= array.length) {
|
||||
extend(array.length * 2);
|
||||
|
|
@ -155,7 +155,7 @@ public:
|
|||
used++;
|
||||
}
|
||||
|
||||
void add(X)(X[] t) if (is(Unqual!X == Unqual!T)) {
|
||||
export void add(X)(X[] t) if (is(Unqual!X == Unqual!T)) {
|
||||
if (used + t.length > array.length) {
|
||||
extend(nextPow2(used + t.length));
|
||||
}
|
||||
|
|
@ -165,20 +165,20 @@ public:
|
|||
used += t.length;
|
||||
}
|
||||
|
||||
void remove(size_t elemNum) {
|
||||
export void remove(size_t elemNum) {
|
||||
destroy(array[elemNum]);
|
||||
swap(array[elemNum], array[used - 1]);
|
||||
used--;
|
||||
}
|
||||
|
||||
void removeStable()(size_t elemNum) {
|
||||
export void removeStable()(size_t elemNum) {
|
||||
used--;
|
||||
foreach (i; 0 .. used) {
|
||||
array[i] = array[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
bool tryRemoveElement()(T elem) {
|
||||
export bool tryRemoveElement()(T elem) {
|
||||
foreach (i, ref el; array[0 .. used]) {
|
||||
if (el == elem) {
|
||||
remove(i);
|
||||
|
|
@ -188,59 +188,59 @@ public:
|
|||
return false;
|
||||
}
|
||||
|
||||
void removeElement()(T elem) {
|
||||
export void removeElement()(T elem) {
|
||||
bool ok = tryRemoveElement(elem);
|
||||
assert(ok, "There is no such an element in vector");
|
||||
}
|
||||
|
||||
ref T opIndex(size_t elemNum) {
|
||||
export ref T opIndex(size_t elemNum) {
|
||||
assert(elemNum < used, "Range violation [index]");
|
||||
return array.ptr[elemNum];
|
||||
}
|
||||
|
||||
auto opSlice() {
|
||||
export auto opSlice() {
|
||||
return array.ptr[0 .. used];
|
||||
}
|
||||
|
||||
T[] opSlice(size_t x, size_t y) {
|
||||
export T[] opSlice(size_t x, size_t y) {
|
||||
assert(y <= used);
|
||||
return array.ptr[x .. y];
|
||||
}
|
||||
|
||||
size_t opDollar() {
|
||||
export size_t opDollar() {
|
||||
return used;
|
||||
}
|
||||
|
||||
void opAssign(X)(X[] slice) {
|
||||
export void opAssign(X)(X[] slice) {
|
||||
reset();
|
||||
this ~= slice;
|
||||
}
|
||||
|
||||
void opOpAssign(string op)(T obj) {
|
||||
export void opOpAssign(string op)(T obj) {
|
||||
static assert(op == "~");
|
||||
add(obj);
|
||||
}
|
||||
|
||||
void opOpAssign(string op, X)(X[] obj) {
|
||||
export void opOpAssign(string op, X)(X[] obj) {
|
||||
static assert(op == "~");
|
||||
add(obj);
|
||||
}
|
||||
|
||||
void opIndexAssign()(T obj, size_t elemNum) {
|
||||
export void opIndexAssign()(T obj, size_t elemNum) {
|
||||
assert(elemNum < used, "Range viloation");
|
||||
array[elemNum] = obj;
|
||||
}
|
||||
|
||||
void opSliceAssign()(T[] obj, size_t a, size_t b) {
|
||||
export void opSliceAssign()(T[] obj, size_t a, size_t b) {
|
||||
assert(b <= used && a <= b, "Range viloation");
|
||||
array.ptr[a .. b] = obj;
|
||||
}
|
||||
|
||||
bool opEquals()(auto ref const Vector!(T) r) const {
|
||||
export bool opEquals()(auto ref const Vector!(T) r) const {
|
||||
return used == r.used && array.ptr[0 .. used] == r.array.ptr[0 .. r.used];
|
||||
}
|
||||
|
||||
size_t toHash() const nothrow @trusted {
|
||||
export size_t toHash() const nothrow @trusted {
|
||||
return hashOf(cast(Unqual!(T)[]) array.ptr[0 .. used]);
|
||||
}
|
||||
|
||||
|
|
@ -249,7 +249,7 @@ public:
|
|||
/**
|
||||
* Preety print
|
||||
*/
|
||||
void toString(scope void delegate(const(char)[]) sink, FormatSpec!char fmt) {
|
||||
export void toString(scope void delegate(const(char)[]) sink, FormatSpec!char fmt) {
|
||||
static if (__traits(compiles, formatValue(sink, array[0 .. used], fmt))) {
|
||||
formatValue(sink, array[0 .. used], fmt);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue