格ゲーコマンド定義をぶち込むコンテナ
相変わらずキー同時押しには対応していません。
いわいるタメという概念にも対応していません。
簡単な辞書です。一つのコマンドで1024byte強使います。メモリの無駄です。(泣)
とりあえず、一つのアプローチとして以下に載せときます。
ライセンスはNYSLです。自由に使ってください。
/** licence:NYSL */ #include <stdio.h> #include <windows.h> #include <dkutil_c/dkc.h> //#include <dkutil/macro.hpp> #include <math.h> #include <vector> template<class T,size_t __N_,class A=std::allocator<T> > class dictionary{ public: typedef typename A allocator; //BOOST_STATIC_CONSTANT(size_t,node_num = __N_); ///sizeof -> 4 * __N_ + sizeof(bool) + alpha struct node_type{ T data; bool data_stop; node_type *next[__N_]; node_type(){ data_stop = false; for(size_t i=0;i<__N_;i++) next[i] = NULL; } }; typedef typename std::vector<node_type **> free_node_vector; //node_type *root[__N_]; typedef node_type *root_type[__N_]; root_type root; struct default_offset_calc_functor{ size_t operator()(const T &x){ return x; } }; dictionary(){ for(size_t i=0;i<__N_;i++) root[i] = NULL; } ~dictionary(){ clear(); } bool reset(){ clear(); } void clear_reflective(free_node_vector &v,node_type *pt[__N_]){ for(size_t i=0;i<__N_;i++) { if(NULL != pt[i]) { v.push_back(&pt[i]); //if(pt[i]->next[j]) clear_reflective(v,&(pt[i]->next[0])); } } } void node_free(node_type *p){ delete p; } node_type *node_alloc(){ return new node_type; } void clear() { //stack free_node_vector v; clear_reflective(v,&root[0]); free_node_vector::reverse_iterator it = v.rbegin(); for(;it != v.rend();it++) { node_type **tpp = (*it); node_free(*tpp); tpp = NULL; } v.clear(); } template<class FUNCTOR> bool insert(T *pobj,size_t num,FUNCTOR calc=default_offset_calc_functor()){ node_type **pt = root; size_t offset = 0; size_t nt = calc(pobj[offset]); if(NULL==pt[nt]){ pt[nt] = node_alloc(); } pt[nt]->data = pobj[offset]; offset++; if(offset == num){ pt[nt]->data_stop = true; return true; } return insert_reflective(pt[nt]->next,offset,pobj,num,calc); } template<class FUNCTOR> bool insert_reflective(node_type **pt,size_t offset,T *pobj,size_t num,FUNCTOR calc=default_offset_calc_functor()) { size_t nt = calc(pobj[offset]); if(NULL==pt[nt]){ pt[nt] = node_alloc(); } offset++; if(offset == num){ pt[nt]->data_stop = true; return true; } return insert_reflective(pt[nt]->next,offset,pobj,num,calc); } template<class FUNCTOR> bool find(T *pobj,size_t num,FUNCTOR calc=default_offset_calc_functor()) { node_type **pt = root; size_t offset = 0; size_t nt = calc(pobj[offset]); if(NULL==pt[nt]){ return false; } offset++; if(offset == num){ return true; } return find_reflective(pt[nt]->next,offset,pobj,num,calc); } template<class FUNCTOR> bool find_reflective(root_type pt,size_t offset,T *pobj,size_t num,FUNCTOR calc=default_offset_calc_functor()) { size_t nt = calc(pobj[offset]); if(NULL==pt[nt]){ return false; } offset++; if(offset == num){ if(true==pt[nt]->data_stop) { return true; } return false; } return find_reflective(pt[nt]->next,offset,pobj,num,calc); } }; int main(){ dkcCheckMemoryLeak(TRUE); typedef dictionary<uint8,256> dic_t; dic_t dic; uint8 data[]={4,2,6,4,9,2}; dic_t::default_offset_calc_functor func; bool r = dic.insert(data,sizeof(data),func); r = dic.find(data,sizeof(data),func); return 0; }