Skip to content

Commit

Permalink
Add iterator remove support
Browse files Browse the repository at this point in the history
  • Loading branch information
nosoop committed Oct 31, 2018
1 parent dbb5ee1 commit d687467
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion smsdk_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/* Basic information exposed publicly */
#define SMEXT_CONF_NAME "More ADTs"
#define SMEXT_CONF_DESCRIPTION "Provides additional data structures to SourceMod."
#define SMEXT_CONF_VERSION "0.1.1"
#define SMEXT_CONF_VERSION "0.1.2"
#define SMEXT_CONF_AUTHOR "nosoop"
#define SMEXT_CONF_URL "http://github.com/nosoop/SMExt-MoreADTs"
#define SMEXT_CONF_LOGTAG "mADT"
Expand Down
17 changes: 12 additions & 5 deletions types/iterator_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@
template <typename T>
class IteratorContainer {
public:
IteratorContainer(typename T::iterator begin, typename T::iterator end):
_it(begin), _end(end) {};
IteratorContainer(T* source, typename T::iterator begin, typename T::iterator end):
_struct(source), _it(begin), _end(end), _removed(false) {};

bool Next() {
// TODO implement a way to mark the current entry as deleted
// if (_deleted) {
// _struct->erase(_current);
// }
if (_removed) {
_struct->erase(_current);
_removed = false;
}
_current = _it;
return _it++ != _end;
}

void MarkRemoved() {
_removed = true;
}

typename T::iterator Current() {
return _current;
}

private:
T* _struct;
typename T::iterator _current, _it, _end;
bool _removed;
};
19 changes: 17 additions & 2 deletions types/string_multimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ cell_t sm_BuildStringMultiMapIterator(IPluginContext *pContext, const cell_t *pa
return pContext->ThrowNativeError("Invalid StringMultiMap handle %x (error %d)", hndl, err);
}

StringMultiMapIterator *pIter = new StringMultiMapIterator(pMultiMap->begin(),
StringMultiMapIterator *pIter = new StringMultiMapIterator(pMultiMap, pMultiMap->begin(),
pMultiMap->end());

return g_pHandleSys->CreateHandle(g_StringMultiMapIteratorType, pIter,
Expand All @@ -259,7 +259,7 @@ cell_t sm_BuildStringMultiMapKeyIterator(IPluginContext *pContext, const cell_t
pContext->LocalToString(params[2], &key);

auto range = pMultiMap->equal_range(key);
StringMultiMapIterator *pIter = new StringMultiMapIterator(range.first, range.second);
StringMultiMapIterator *pIter = new StringMultiMapIterator(pMultiMap, range.first, range.second);

return g_pHandleSys->CreateHandle(g_StringMultiMapIteratorType, pIter,
pContext->GetIdentity(), myself->GetIdentity(), NULL);
Expand Down Expand Up @@ -431,3 +431,18 @@ cell_t sm_SetStringMultiMapIteratorArray(IPluginContext *pContext, const cell_t

return 0;
}

/* native void StringMultiMapIterator.Erase(); */
cell_t sm_RemoveOnStringMultiMapIterator(IPluginContext *pContext, const cell_t *params) {
Handle_t hndl = static_cast<Handle_t>(params[1]);

StringMultiMapIterator *pMultiMapIter;
HandleError err;
if ((err = ReadStringMultiMapIterHandle(hndl, &pMultiMapIter)) != HandleError_None) {
return pContext->ThrowNativeError("Invalid StringMultiMapIterator handle %x (error %d)", hndl, err);
}

pMultiMapIter->MarkRemoved();

return 0;
}
3 changes: 3 additions & 0 deletions types/string_multimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cell_t sm_GetStringMultiMapIteratorValue(IPluginContext *pContext, const cell_t
cell_t sm_SetStringMultiMapIteratorValue(IPluginContext *pContext, const cell_t *params);
cell_t sm_GetStringMultiMapIteratorArray(IPluginContext *pContext, const cell_t *params);
cell_t sm_SetStringMultiMapIteratorArray(IPluginContext *pContext, const cell_t *params);
cell_t sm_RemoveOnStringMultiMapIterator(IPluginContext *pContext, const cell_t *params);


const sp_nativeinfo_t g_StringMultiMapNatives[] = {
Expand Down Expand Up @@ -60,5 +61,7 @@ const sp_nativeinfo_t g_StringMultiMapNatives[] = {
{ "StringMultiMapIterator.GetArray", sm_GetStringMultiMapIteratorArray },
{ "StringMultiMapIterator.SetArray", sm_SetStringMultiMapIteratorArray },

{ "StringMultiMapIterator.Remove", sm_RemoveOnStringMultiMapIterator },

{NULL, NULL},
};

0 comments on commit d687467

Please sign in to comment.