NO.1006:indexを使って配列にアクセスするモジュール

配列の全要素にアクセスする時は
大体repeatとcntを使ってアクセスします
でもcntは動く値なので
静的な動作に感じられません
そこでLISTのようにadd、getできるモジュールを作成してみました
cntの指定はしなくてよくなったのですが
インスタンスを書く必要があるのが不満

array_exモジュール

FILE名:FILE_array_push_pop.hsp
//	"FILE_array_ex.hsp"が必要!

//	モジュール変数の説明
//	array_dat:array_exのインスタンス
//	size:array_exのデータを確保する単位
//	index4push:add時のindex
//	index4pop:get_add_index時のindex
#module array_push_pop array_dat,size,index4push,index4pop
#modinit str type
flag=0
size=16
index4push=0
index4pop=0
if type=="S"{size=16:flag=1}
if type=="M"{size=64:flag=1}
if type=="L"{size=256:flag=1}
if flag!=1{
logmes "array_push_pop内modinitのERROR"
logmes "modinitの引数は\"S\"か\"M\"か\"L\"を指定してください"
return
}
newmod array_dat,array_ex,size
return

//	index4pushクリア
#modfunc reset_push_at_array_push_pop
index4push=0
return
//	index4pushをset
#modfunc set_index_push_at_array_push_pop int p
index4push=p
return
//	index4pushをget
#modfunc get_index_push_at_array_push_pop var r
r=index4push
return

//	index4popクリア
#modfunc reset_pop_at_array_push_pop
index4pop=0
return
//	index4popをset
#modfunc set_index_pop_at_array_push_pop int p
index4pop=p
return
//	index4popをget
#modfunc get_index_pop_at_array_push_pop var r
r=index4pop
return

//	インスタンスに数値をadd
#modfunc add_at_array_push_pop int p
set_ex_at_array_ex array_dat,p,index4push
index4push++
return
//	インスタンスから数値を取り出す
#modfunc get_add_index_at_array_push_pop var r
get_at_array_ex array_dat,r,index4pop
index4pop++
return
#global

・以下テストコード
#include "FILE_array_ex.hsp"
#include "FILE_array_push_pop.hsp"
newmod o,array_push_pop,"M"
//	add_at_array_push_popのテスト
add_at_array_push_pop o,123
add_at_array_push_pop o,456
add_at_array_push_pop o,789
//	get_index_push_at_array_push_popのテスト
r=0
get_index_push_at_array_push_pop o,r
logmes "push_index : "+str(r)
//	get_add_index_at_array_push_popのテスト。兼dump
r=0
get_add_index_at_array_push_pop o,r
logmes str(r)
r=0
get_add_index_at_array_push_pop o,r
logmes str(r)
r=0
get_add_index_at_array_push_pop o,r
logmes str(r)

//	get_index_pop_at_array_push_popのテスト
r=0
get_index_pop_at_array_push_pop o,r
logmes "pop_index : "+str(r)
//	reset_pop_at_array_push_popのテスト
reset_pop_at_array_push_pop o
r=0
get_index_pop_at_array_push_pop o,r
logmes "pop_index : "+str(r)
//	set_index_pop_at_array_push_popのテスト
set_index_pop_at_array_push_pop o,2
r=0
get_index_pop_at_array_push_pop o,r
logmes "pop_index : "+str(r)
r=0
get_add_index_at_array_push_pop o,r
logmes str(r)

//	reset_push_at_array_push_popのテスト
reset_push_at_array_push_pop o
r=0
get_index_push_at_array_push_pop o,r
logmes "push_index : "+str(r)
add_at_array_push_pop o,256
//	set_index_push_at_array_push_popのテスト
set_index_push_at_array_push_pop o,2
add_at_array_push_pop o,512
reset_pop_at_array_push_pop o
//	dump
r=0
get_add_index_at_array_push_pop o,r
logmes str(r)
r=0
get_add_index_at_array_push_pop o,r
logmes str(r)
r=0
get_add_index_at_array_push_pop o,r
logmes str(r)

こんなかんじで使います
#include "FILE_array_ex.hsp"
#include "FILE_array_push_pop.hsp"

newmod o,array_push_pop,"M"
repeat 32,1
add_at_array_push_pop o,cnt
loop

repeat 32
r=0
get_add_index_at_array_push_pop o,r
logmes "var : "+str(r)
loop

logmes ""

newmod o2,array_push_pop,"M"
repeat 32,1
add_at_array_push_pop o2,(cnt*2)
loop

repeat 32
r=0
get_add_index_at_array_push_pop o2,r
logmes "var : "+str(r)
loop

stop