有時侯會面臨未來不知數據會有多少變數的問題, 而把所以變數一次性全部寫上, 或者在面臨變數時,不斷修改類源碼以擴充新的數據記錄顯然不是一個好方法. 這時侯,可以采用索引+綁定方式解決: 類源碼的數據只有一個用於區分事物的uid(索引用), 然後在後續的模塊中,需要擴展新的數據,則對這個uid進行各類綁定. 這樣,新增的數據需求不會修改到舊有的數據需求.
[cpp]
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