关于数据类型未知或者将会不断增加的解决方案
来源:互联网 发布:淘宝网首页女装秋装 编辑:程序博客网 时间: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
- 关于数据类型未知或者将会不断增加的解决方案
- 传统金融不断改革,互联网金融的红利将会消失
- 面向对象的经典语录:(不断增加)
- [整理]有用的SQL语句(不断增加)
- 内存使用率不断增加的原因之一
- Ycnd 2D新版将会增加的扩展组件
- 关于本人将会搬家博客的声明
- 关于 vs2005 添加类或者变量 出错问题的解决方案
- 关于出现僵尸信号SIGBAT或者EXC_BAD_ACCESS的解决方案
- 关于出现僵尸信号SIGBAT或者EXC_BAD_ACCESS的解决方案
- 关于出现僵尸信号SIGBAT或者EXC_BAD_ACCESS的解决方案
- 关于目前SDK更新慢,或者无法更新的解决方案
- 关于Eclipse无法识别手机或者模拟器的解决方案
- 关于iReport5.6.0无法正常启动或者闪退或者JDK8不兼容的解决方案
- 关于未知
- 不断增加点
- 严重的生产事故--不断增加的list
- 一些学习flash的不错的资源(不断增加)
- js,py,ru
- oracle网络基础
- listView.addFooterView时出现java.lang.ClassCastException:
- curl使用简介---转载
- ORA-02143: invalid STORAGE option --DSG oracle 11g 复制数据到oracle 10g
- 关于数据类型未知或者将会不断增加的解决方案
- ORA-600 [504], ..., [Transaction Branch Allocation] Error When Using XA [ID 868527.1]
- 不使用随机数的洗牌算法/Shuffle sort and other fallacies of randomization
- 简明 Vim 练级攻略
- 读一位作者的微软面试心得想到的
- RMAN的"rman: can't open target"错误
- 初试WCF
- 中兴U880刷机
- C++ 变长参数列表函数的用法