关于数据类型未知或者将会不断增加的解决方案

来源:互联网 发布:淘宝网首页女装秋装 编辑:程序博客网 时间:2024/04/30 04:34

有时侯会面临未来不知数据会有多少变数的问题, 而把所以变数一次性全部写上, 或者在面临变数时,不断修改类源码以扩充新的数据记录显然不是一个好方法. 这时侯,可以采用索引+绑定方式解决: 类源码的数据只有一个用于区分事物的uid(索引用), 然后在后续的模块中,需要扩展新的数据,则对这个uid进行各类绑定. 这样,新增的数据需求不会修改到旧有的数据需求.

 

function get_t_set_by_i( _t )if nil == _t thenreturn _tendlocal t = {}for i,v in pairs(_t) dot[table.getn(t) + 1] = iendreturn tendfunction get_t_set_by_v( _t )if nil == _t thenreturn _tendlocal t = {}for i,v in pairs(_t) dot[table.getn(t) + 1] = vendreturn tendfunction myPrint(_x, _y)print(_x)endfunction get_valid_t( _t )local t = {}local len = table.getn(_t)for i=1, len doif nil ~= _t[i] thent[ table.getn(t) + 1] = _t[i]endendreturn tend--------------------------------------------- 索引 ----------------------------------------------索引管理function new_index_manager()local t_index = {}local public = {}--创建索引function public.create_index()for i=1, 9999999 doif nil == t_index[i] thent_index[i] = 1return iendendmyPrint("索引资源用尽", 1)return nilend--索引是否有效function public.is_valid_index( _index )if nil ~= t_index[_index] thenreturn trueendreturn falseend--删除索引function public.delete_index( _index )t_index[_index] = nilendreturn publicendG_FB_Buf_Index = new_index_manager()--1:N绑定器function new_map_for_1_and_N()local left_set = {}local right_set = {}local public = {}--绑定索引和UID( 1:N )function public.bind_left_and_right( _left, _right )if nil == left_set[_left] thenleft_set[_left] = {}endlocal len = table.getn(left_set[_left])for i=1, len doif left_set[_left][i] == _right thenreturnendendleft_set[_left][table.getn(left_set[_left])+1] = _rightright_set[_right] = _leftend--清除绑定function public.clear_left_and_right( _left )local t_right = public.get_t_map_by_fb_buf_index( _left )local len = table.getn( t_right )for i=1, len doright_set[ t_right[i] ] = nilendleft_set[_left] = nilend--清除绑定function public.clear_right( _left, _right )right_set[_right] = nillocal t_right = left_set[_left]local len = table.getn( t_right )for i=1, len doif t_right[i] == _right thent_right[i] = nilendendend--通过left获得rigth表function public.get_t_right_by_left( _left )return get_valid_t( left_set[_left] )end--通过right获得leftfunction public.get_left_by_right( _right )return right_set[ _right ]endreturn publicend--buf绑定器function new_map_for_index_to_buf()local index_set = {}local public = {}--绑定buffunction public.bind_index_to_buf( _index, _buf )index_set[ _index ] = _bufend--清除绑定function public.clear_index_to_buf( _index )index_set[_index] = nilend--通过left获得rigth表function public.get_buf( _index )return index_set[ _index ]endreturn publicend------------------------------------------------ 地图buf --------------------------------------------------------------------------------------- 人员buf ------------------------------------------------local index = G_FB_Buf_Index.create_index()local fb_and_maps_binder = new_map_for_1_and_N()local fb_and_roles_binder = new_map_for_1_and_N()local roles_buf_binder = new_map_for_index_to_buf()for map_uid=1, 10 dofb_and_maps_binder.bind_left_and_right(index, map_uid)endlocal t = fb_and_maps_binder.get_t_right_by_left( fb_and_maps_binder.get_left_by_right(7) )for i=1, table.getn(t) doprint(t[i])endfor role_uid=100, 110 dofb_and_roles_binder.bind_left_and_right(index, role_uid)roles_buf_binder.bind_index_to_buf(role_uid, {"name ".. role_uid})endfb_and_roles_binder.clear_right(index, 102)local t = fb_and_roles_binder.get_t_right_by_left( fb_and_roles_binder.get_left_by_right(105) )for i=1, table.getn(t) doprint(t[i])local buf = roles_buf_binder.get_buf(t[i])print( buf[1] )end


 

原创粉丝点击