HSP ver WS 企画第二弾です
前回はStackモジュールでしたが、
今回はHeapモジュールです。
Ruby版とかはハッシュ使ってやってるのですが、
C版は配列で実装しててそれに倣ってます
配列もどきな構造なのですが
配列のindexともども登録した順に代入しています
それではよろしくお願いします
#ifndef HSPWS_DEFINE
#define HSPWS_DEFINE
#define STACK_MEMBERS 1024
#define HEAP_MEMBERS 500
#define MAX_LABELS 200
#define MAX_LABEL_LENGTH 500
#define MAX_INSTRUCTIONS 30
#define MAX_INSTRUCTION_LENGTH 10
#define MAX_NESTED_SUBROUTINES 20
#endif
#module heap_model m_address,m_value,m_elements
#modinit
dim m_address,500
dim m_value,500
m_elements=0
return
// heap領域に値をSET
#modcfunc heap_put int p_val,int p_addr
// 最初にそのaddressが使われてるか調べる
for i,0,m_elements,1
if m_address.i == p_addr {
m_value.i = p_val
return 1
}
next
// そうじゃなければ領域が余ってるか調べて加える
if m_elements < HEAP_MEMBERS@ {
i = m_elements
m_address.i = p_addr
m_value.i = p_val
m_elements++
return 1
}
return 0
// heap領域から値をGET
// addressからデータを取得出来るか調べて
// 存在したら値を返し
// 存在しなければ0を返す
#modcfunc heap_get int p_addr
for i,0,HEAP_MEMBERS@,1
if m_address.i == p_addr:return m_value.i
next
return 0
#global
#if 1
newmod heap,heap_model
#endif
#if 1
er=heap_put(heap,10,1)
mes er
er=heap_put(heap,20,2)
mes er
er=heap_put(heap,30,3)
mes er
er=heap_put(heap,40,4)
er=heap_put(heap,50,5)
er=heap_put(heap,100,10)
er=heap_put(heap,90,9)
er=heap_put(heap,80,8)
er=heap_put(heap,70,7)
er=heap_put(heap,60,6)
er=heap_put(heap,100,1)
er=heap_put(heap,200,2)
er=heap_put(heap,300,3)
mes heap_get(heap,1)
mes heap_get(heap,2)
mes heap_get(heap,3)
mes heap_get(heap,4)
mes heap_get(heap,5)
mes heap_get(heap,6)
mes heap_get(heap,7)
mes heap_get(heap,8)
mes heap_get(heap,9)
mes heap_get(heap,10)
mes heap_get(heap,11)
mes heap_get(heap,12)
#endif