function new_index_manager()
local res = {{1,99999999}}
local public = {}
function public.take_away_index()
local t = res[1]
if table.getn(t) >= 1 then
local ret = t[1]
t[1] = t[1] + 1
if t[1] > t[2] then
res[1] = res[ table.getn( res ) ]
res[ table.getn( res ) ] = nil
end
return ret
end
return nil
end
function public.take_back_index( _index )
local t = {_index, _index}
res[ table.getn( res ) + 1] = t
if table.getn( res ) > 50 then
public.tidy()
end
end
function public.tidy()
local reflash = true
--有新合并发生则继续进行合并检测
while reflash do
reflash = false
--进行合并
local len = table.getn( res )
for i=1, len do
for j=i+1, len do
if table.getn(res[i]) > 1 and table.getn(res[j]) > 1 then
local be_mixed = true
--判断交集情况
if res[i][2]+1 < res[j][1] or res[j][2]+1 < res[i][1] then
be_mixed = false
end
--合并
if be_mixed then
local t = {}
if res[i][1] < res[j][1] then
t[1] = res[i][1]
else
t[1] = res[j][1]
end
if res[i][2] > res[j][2] then
t[2] = res[i][2]
else
t[2] = res[j][2]
end
res[i] = t
res[j] = {}
reflash = true
end
end
end
end
--刷新数据
local t = {}
local len = table.getn( res )
for i=1, len do
if table.getn(res[i]) > 1 then
t[ table.getn(t) + 1 ] = res[i]
end
end
res = t
end
end
function public.show()
local len = table.getn( res )
for i=1, len do
print(res[i][1], res[i][2])
end
end
return public
end
local index_manager = new_index_manager()
local t_record = {}
for i=1, 1000 do
local r = math.random(1,10)
if r < 6 then
t_record[ table.getn(t_record) + 1 ] = index_manager.take_away_index()
else
if table.getn(t_record) > 1 then
index_manager.take_back_index(t_record[1])
t_record[1] = t_record[ table.getn(t_record) ]
t_record[ table.getn(t_record) ] = nil
end
end
end
index_manager.show()
print("---------")
index_manager.show()
>lua -e "io.stdout:setvbuf 'no'" "123.lua"
471 99999999
1 451
454 456
458 462
464 464
---------
471 99999999
1 451
454 456
458 462
464 464
>Exit code: 0
#include<vector>
using namespace std;
class UID_Factory
{
public:
typedef pair<long, long> ITIM;
UID_Factory(){ res.push_back( make_pair(1, 99999999) ); }
long create_uid()
{
if( res.size() > 0 )
{
long ret = res[0].first;
res[0].first++;
if( res[0].first > res[0].second)
{
res[0] = res[ res.size() - 1];
res.pop_back();
}
return ret;
}
return 0;
}
bool create_need_uid(long _uid)
{
if( res.size() > 0 )
{
long len = res.size();
for(long i=0; i<len; ++i)
{
if( _uid >= res[i].first && _uid <= res[i].second)
{
long s = res[i].first;
long e = res[i].second;
res[i] = res[res.size()-1];
res.pop_back();
if( _uid > s)
{
res.push_back( make_pair(s, _uid-1) );
}
if( _uid < e)
{
res.push_back( make_pair(_uid+1, e) );
}
return true;
}
}
}
return false;
}
void delete_uid(long _uid)
{
long len = res.size();
for(long i=0; i<len; ++i)
{
if( _uid >= res[i].first && _uid <= res[i].second)
return;
}
res.push_back( make_pair(_uid, _uid) );
if( res.size() > 50 )
tidy_res();
}
void tidy_res()
{
bool is_reflash = false;
do
{
is_reflash = false;
for(long i=0; i<res.size()-1; ++i)
{
for(long j=i+1; j<res.size(); ++j)
{
bool is_mixed = true;
if( (res[i].second + 1 < res[j].first) || (res[j].second + 1 < res[i].first) )
is_mixed = false;
if( is_mixed )
{
long s = min( res[i].first, res[j].first );
long e = max( res[i].second, res[j].second );
res[i] = make_pair(s,e);
res[j] = res[res.size()-1];
res.pop_back();
is_reflash = true;
}
}
}
} while ( is_reflash );
}
private:
friend void show_uid_res(UID_Factory *_p_uid_factory);
vector< ITIM > res;
};