フレームバッファ描画ライブラリが一先ずの目標までもう少しなところで習作プログラムでも作ろうかと思い立ち、
一部の動作に FIFO データ構造が欲しいということになって、
作るなら後々の再利用まで考えた作りに出来ないものか、
といった辺りからの寄り道です。そもそもこれ LIFO ですし(笑)。
寄り道から戻るのを止めて凍結なんてはそろそろ卒業しようと思う次第です。
キューテンプレートライブラリまで作ったら戻ります。
キューを作るならスタックも作らなくては!という流れで
まずは基本動作確認できるものを作ってみました。
後はライブラリを継承して利用する為の構造を組んでマルチプルインスタンス化でしょうか。
実装はオリジナルですが関数内訳は C++ の STL を参考にしてます。
main.c
/*--------------------------------------------------------------------------------- Stack template library test version 0.01 test Jun 20, 2010 By REGEKATSU ---------------------------------------------------------------------------------*/ #include <stdio.h> #include <nds.h> #include "libmy/Stack.h" void drawStack(void); //--------------------------------------------------------------------------------- int main(void){ //--------------------------------------------------------------------------------- int value = 0; consoleDemoInit(); printf("Stack template library test\n\nversion 0.01 test\nJun 20, 2010\n\nBy REGEKATSU\n\n\n"); printf("\x01b[22;0HStack Act :null"); Stack_Init(); while(1) { swiWaitForVBlank(); scanKeys(); drawStack(); //push if(keysDown() & KEY_A){ if(Stack_Size() != STACK_SIZE){ Stack_Push(++value); printf("\x01b[22;12HPush"); } //pop }else if(keysDown() & KEY_B){ if(Stack_Empty() == false){ Stack_Pop(); printf("\x01b[22;12HPop "); } } } } //--------------------------------------------------------------------------------- void drawStack(void){ //--------------------------------------------------------------------------------- printf("\x01b[8;0HStack:Value\n"); int i; for(i = 0;i < STACK_SIZE;i++){ printf( "\x01b[%d;0H\x01b[2K\r %02d:%d\n", 9 + i, STACK_SIZE - (i + 1), *Stack_Request(STACK_SIZE - (i + 1)) ); } printf("\n"); printf("Stack Empty:%s\n", (Stack_Empty() == true) ? "true ":"false"); printf("Stack Size :%02d\n", Stack_Size()); } //---------------------------------------------------------------------------------
Stack.h
/*--------------------------------------------------------------------------------- Stack template library header version 0.01 test Jun 20, 2010 By REGEKATSU ---------------------------------------------------------------------------------*/ #ifndef _STACK_H_ #define _STACK_H_ #include <nds/ndstypes.h>//bool #define STACK_SIZE (10) #ifdef __cplusplus extern "C" { #endif //スタックデータ構造を初期化します。 void Stack_Init(void); //現在のスタックが空の時にtrueを返し、そうでないときはfalseを返します。 bool Stack_Empty(void); //現在のスタックの先頭の要素を削除します。 void Stack_Pop(void); //値を現在のスタックの先頭に追加します。 void Stack_Push(int value); //現在のスタックの任意の要素への参照を返します。 int *Stack_Request(int number); //現在のスタックの要素数を返します。 int Stack_Size(void); //現在のスタックの先頭の要素への参照を返します。 int *Stack_Top(void); #ifdef __cplusplus } #endif #endif //_STACK_H_
Stack.c
/*--------------------------------------------------------------------------------- Stack template library routine version 0.01 test Jun 20, 2010 By REGEKATSU ---------------------------------------------------------------------------------*/ #include <string.h> #include "Stack.h" typedef struct _Stack{ int value[STACK_SIZE]; int sp; }_Stack; static _Stack st_stack; //--------------------------------------------------------------------------------- void Stack_Init(void){ //--------------------------------------------------------------------------------- //スタックデータ構造を初期化します。 memset(&st_stack, 0, sizeof(st_stack)); } //--------------------------------------------------------------------------------- bool Stack_Empty(void){ //--------------------------------------------------------------------------------- //現在のスタックが空の時にtrueを返し、そうでないときはfalseを返します。 return (st_stack.sp == 0) ? true:false; } //--------------------------------------------------------------------------------- void Stack_Pop(void){ //--------------------------------------------------------------------------------- //現在のスタックの先頭の要素を削除します。 if(st_stack.sp > 0) st_stack.value[--st_stack.sp] = 0; } //--------------------------------------------------------------------------------- void Stack_Push(int value){ //--------------------------------------------------------------------------------- //値を現在のスタックの先頭に追加します。 if(st_stack.sp < STACK_SIZE) st_stack.value[st_stack.sp++] = value; } //--------------------------------------------------------------------------------- int *Stack_Request(int number){ //--------------------------------------------------------------------------------- //現在のスタックの任意の要素への参照を返します。 if(number < 0 || number >= STACK_SIZE) return NULL; if(st_stack.sp == 0) return NULL; return &st_stack.value[number]; } //--------------------------------------------------------------------------------- int Stack_Size(void){ //--------------------------------------------------------------------------------- //現在のスタックの要素数を返します。 return st_stack.sp; } //--------------------------------------------------------------------------------- int *Stack_Top(void){ //--------------------------------------------------------------------------------- //現在のスタックの先頭の要素への参照を返します。 if(st_stack.sp == 0) return NULL; return &st_stack.value[st_stack.sp - 1]; } //---------------------------------------------------------------------------------