关于数据类型未知或许将会不断增加的解决方案
关于数据类型未知或者将会不断增加的解决方案
有时侯会面临未来不知数据会有多少变数的问题, 而把所以变数一次性全部写上, 或者在面临变数时,不断修改类源码以扩充新的数据记录显然不是一个好方法. 这时侯,可以采用索引+绑定方式解决: 类源码的数据只有一个用于区分事物的uid(索引用), 然后在后续的模块中,需要扩展新的数据,则对这个uid进行各类绑定. 这样,新增的数据需求不会修改到旧有的数据需求.
function get_t_set_by_i( _t ) if nil == _t then return _t end local t = {} for i,v in pairs(_t) do t[table.getn(t) + 1] = i end return t end function get_t_set_by_v( _t ) if nil == _t then return _t end local t = {} for i,v in pairs(_t) do t[table.getn(t) + 1] = v end return t end function myPrint(_x, _y) print(_x) end function get_valid_t( _t ) local t = {} local len = table.getn(_t) for i=1, len do if nil ~= _t[i] then t[ table.getn(t) + 1] = _t[i] end end return t end --------------------------------------------- 索引 -------------------------------------------- --索引管理 function new_index_manager() local t_index = {} local public = {} --创建索引 function public.create_index() for i=1, 9999999 do if nil == t_index[i] then t_index[i] = 1 return i end end myPrint("索引资源用尽", 1) return nil end --索引是否有效 function public.is_valid_index( _index ) if nil ~= t_index[_index] then return true end return false end --删除索引 function public.delete_index( _index ) t_index[_index] = nil end return public end G_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] then left_set[_left] = {} end local len = table.getn(left_set[_left]) for i=1, len do if left_set[_left][i] == _right then return end end left_set[_left][table.getn(left_set[_left])+1] = _right right_set[_right] = _left end --清除绑定 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 do right_set[ t_right[i] ] = nil end left_set[_left] = nil end --清除绑定 function public.clear_right( _left, _right ) right_set[_right] = nil local t_right = left_set[_left] local len = table.getn( t_right ) for i=1, len do if t_right[i] == _right then t_right[i] = nil end end end --通过left获得rigth表 function public.get_t_right_by_left( _left ) return get_valid_t( left_set[_left] ) end --通过right获得left function public.get_left_by_right( _right ) return right_set[ _right ] end return public end --buf绑定器 function new_map_for_index_to_buf() local index_set = {} local public = {} --绑定buf function public.bind_index_to_buf( _index, _buf ) index_set[ _index ] = _buf end --清除绑定 function public.clear_index_to_buf( _index ) index_set[_index] = nil end --通过left获得rigth表 function public.get_buf( _index ) return index_set[ _index ] end return public end ------------------------------------------------ 地图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 do fb_and_maps_binder.bind_left_and_right(index, map_uid) end local 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) do print(t[i]) end for role_uid=100, 110 do fb_and_roles_binder.bind_left_and_right(index, role_uid) roles_buf_binder.bind_index_to_buf(role_uid, {"name ".. role_uid}) end fb_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) do print(t[i]) local buf = roles_buf_binder.get_buf(t[i]) print( buf[1] ) end