Sometimes, you may face the issue of not knowing how many variables there will be in the data in the future, and write all the variables at once, or when you are faced with variables, it is obviously not a good method to constantly modify the class source code to expand new data records. at this time, you can use the index + binding method to solve the problem: the data of the class source code only has one uid (used for indexing) used to differentiate transactions, and new data needs to be extended in subsequent modules, this uid is bound to various types. in this way, the new data requirements will not be changed to the old data requirements.
[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
--------------------------------------------- Index --------------------------------------------
-- Index management
Function new_index_manager ()
Local t_index = {}
Local public = {}
-- Create an index
Function public. create_index ()
For I = 1, 9999999 do
If nil = t_index [I] then
T_index [I] = 1
Return I
End
End
MyPrint ("exhausted index resources", 1)
Return nil
End
-- Whether the index is valid
Function public. is_valid_index (_ index)
If nil ~ = T_index [_ index] then
Return true
End
Return false
End
-- Delete an index
Function public. delete_index (_ index)
T_index [_ index] = nil
End
Return public
End
G_FB_Buf_Index = new_index_manager ()
-- 1: N binder
Function new_map_for_1_and_N ()
Local left_set = {}
Local right_set = {}
Local public = {}
-- Bind the index and 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
-- Clear binding
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
-- Clear binding
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
-- Obtain the rigoal table through left
Function public. get_t_right_by_left (_ left)
Return get_valid_t (left_set [_ left])
End
-- Obtain left by right
Function public. get_left_by_right (_ right)
Return right_set [_ right]
End
Return public
End
-- Buf Binder
Function new_map_for_index_to_buf ()
Local index_set = {}
Local public = {}
-- Bind buf
Function public. bind_index_to_buf (_ index, _ buf)
Index_set [_ index] = _ buf
End
-- Clear binding
Function public. clear_index_to_buf (_ index)
Index_set [_ index] = nil
End
-- Obtain the rigoal table through left
Function public. get_buf (_ index)
Return index_set [_ index]
End
Return public
End
------------------------------------------------ Map buf ----------------------------------------
----------------------------------------------- Personnel 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_assistand_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