--利用lua中的table是哈希表这一点进行计算
function lua_distinct_union (union_t1,union_t2)
if(union_t2==nil) then
union_t2={};
end;
local result={};
for k,v in pairs(union_t1) do
result[v]=v;
end;
for k,v in pairs(union_t2) do
if result[v] ==nil then
result[v]=v;
end;
end;
union_t1=nil;
union_t2=nil;
return result;
end; function lua_distinct (distinct_t)
local result={};
for k,v in pairs(distinct_t) do
if(result[v]==nil) then
result[v]=v;
end;
end;
distinct_t=nil;
return result;
end; function lua_distinct_diff(diff_t1,diff_t2) local result={};
for k,v in pairs(diff_t1) do
if(result[v]==nil) then
result[v]=v;
end;
end; for k,v in pairs(diff_t2) do
if(result[v]~=nil) then
table.remove(result,v);
end;
end;
diff_t1=nil;
diff_t2=nil;
return result;
end; function lua_distinct_inter (inter_t1,inter_t2)
if inter_t1==nil and inter_t2~=nil then
return lua_distinct(inter_t2);
end;
if inter_t1~=nil and inter_t2==nil then
return lua_distinct(inter_t1);
end;
local temp1={};
local temp2={};
local result={};
local num1=;
local num2=;
for k,v in pairs(inter_t1) do
if(temp1[v]==nil) then
temp1[v]=v;
num1=num1+;
end;
end;
for k,v in pairs(inter_t2) do
if(temp2[v]==nil) then
temp2[v]=v;
num2=num2+;
end;
end;
inter_t1=nil;
inter_t2=nil;
if num1<num2 then
for k,v in pairs(temp1) do
if temp2[k]~=nil then
result[k]=temp1[k];
end;
end;
else
for k,v in pairs(temp2) do
if temp2[k]~=nil then
result[k]=temp1[k];
end;
end;
end;
temp1=nil;
temp2=nil;
return result;
end; function lua_distinct_union_multiply(tablesneedunion)
if(tablesneedunion==nil or #(tablesneedunion)==) then
error('lua_distinct_diff_Multiply传入参数为空');
end;
local result=tablesneedunion[];
if #(tablesneedunion)== then
return lua_distinct_union(result,nil);
end;
for i=,#(tablesneedunion) do
local tmp=tablesneedunion[i];
result=lua_distinct_union(tmp,result);
end;
tablesneedunion=nil;
return result;
end; function lua_distinct_inter_multiply(tablesneedinter)
if(tablesneedinter==nil or #(tablesneedinter)==) then
error('lua_distinct_inter_multiply传入参数为空');
end;
local result=tablesneedinter[];
if #(tablesneedinter)== then
return lua_distinct_inter(result,nil);
end;
for i=,#(tablesneedinter) do
local tmp=tablesneedinter[i];
result=lua_distinct_inter(tmp,result);
if #(result)== then
break;
end;
end;
tablesneedinter=nil;
return result;
end; function getidsfromkeyvaluepair(keyvaluepair)
local result={};
if(keyvaluepair==nil) then
return result;
end;
local num=;
for k,v in pairs(keyvaluepair) do
result[num]=v;
num=num+;
end;
return result;
end; function lua_getfinalids(structs)
if #(structs)== then
return getidsfromkeyvaluepair(temptable);
end;
local temptable=structs[].temptable;
for i=,#(structs) do
local tmp2=structs[i].temptable;
if structs[i-].flag== then
temptable=lua_distinct_union(temptable,tmp2);
else
temptable=lua_distinct_inter(temptable,tmp2);
end;
end;
return getidsfromkeyvaluepair(temptable);
end; function lua_sortandtake(tableneedsort,takecount,isasc)
if isasc==true then
table.sort(tableneedsort);
else
table.sort(tableneedsort,function(a,b) return a>b end );
end;
local result={};
for i= ,takecount do
result[i]=tableneedsort[i];
end;
return result;
end;