Skip to content

Commit

Permalink
Add SelCollection:iData()
Browse files Browse the repository at this point in the history
  • Loading branch information
destroyedlolo committed Oct 3, 2015
1 parent a429115 commit 763fdd7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Selenites/Collection.sel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!./Selene --dfb:quiet
#!./Selene

-- Collection example

Expand All @@ -19,6 +19,6 @@ for i=1,5 do
end
col:dump()

print( col:MinMax() )
print( "MinMax", col:MinMax() )

print( col:Data() )
for d in col:iData() do print(d) end
27 changes: 27 additions & 0 deletions src/SelCollection.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ static int scol_data(lua_State *L){
if(!col->last && !col->full)
return 0;

#ifdef DEBUG
printf("%d : %s\n", col->full ? col->size : col->last, lua_checkstack(L, col->full ? col->size : col->last) ? "ok" : "nonok" );
#endif
for(unsigned int i=col->full ? col->last - col->size : 0; i < col->last; i++)
lua_pushnumber(L, col->data[ i % col->size ]);
return col->full ? col->size : col->last;
Expand All @@ -83,6 +86,29 @@ static int scol_getsize(lua_State *L){
return 1;
}

static int scol_inter(lua_State *L){
struct SelCollection *col = (struct SelCollection *)lua_touserdata(L, lua_upvalueindex(1));

if(col->cidx < col->last) {
lua_pushnumber(L, col->data[ col->cidx % col->size ]);
col->cidx++;
return 1;
} else
return 0;
}

static int scol_idata(lua_State *L){
struct SelCollection *col = checkSelCollection(L);

if(!col->last && !col->full)
return 0;

col->cidx = col->full ? col->last - col->size : 0;
lua_pushcclosure(L, scol_inter, 1);

return 1;
}

static int scol_create(lua_State *L){
struct SelCollection *col = (struct SelCollection *)lua_newuserdata(L, sizeof(struct SelCollection));
luaL_getmetatable(L, "SelCollection");
Expand All @@ -107,6 +133,7 @@ static const struct luaL_reg SelColM [] = {
{"Push", scol_push},
{"MinMax", scol_minmax},
{"Data", scol_data},
{"iData", scol_idata},
{"GetSize", scol_getsize},
{"dump", scol_dump},
{NULL, NULL}
Expand Down
1 change: 1 addition & 0 deletions src/SelCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct SelCollection {
unsigned int size; /* Length of the data collection */
unsigned int last; /* Last value pointer */
char full; /* the collection is full */
unsigned int cidx; /* Current index for iData() */
};

#endif

0 comments on commit 763fdd7

Please sign in to comment.