فقط یک میلیون؟من روی میلیارد حساب کرده بودم.این مساله نزدیک ۱۰ مگ حافظه میخواد.
sealed class node
{
public node(int id, node[] Collection)
{
this.Collection = collection;
this.id = id;
}
node[] Collection;
int id; public ID { get { return id; } }
list<node> refs = new List<node>();
void verify(node noderef)
{
if(noderef.Collection != Collection)
throw new InvalidOperationException("cross-collection reference detected.");
}
void verify (int nodeid)
{
if(nodeid >= Collection.Length)
throw new InvalidOperationException("cross-collection reference detected.there may be false negatives.");
}
public void AddRefSafe(node noderef)
{
lock(refs)
AddRef(noderef);
}
public void AddRef(node noderef)
{
if(noderef == this)
throw new Exception();
verify(noderef);
refs.Add(noderef);
}
public void ContainsRefSafe(node noderef)
{
lock(refs)
return ContainsRef(noderef);
}
public void ContainsRef(node noderef)
{
verify(noderef);
return refs.Contains(noderef);
}
public void ContainsSafe(int nodeid)
{
lock(refs)
return Contains(nodeid);
}
public void Contains(int nodeid)
{
verify(noderef);
for(int n = 0;n != refs.Count;n++)
{
if(refs[n].id == nodeid)
return true;
}
return false;
}
//todo:implement bloom-filter based CanSee for memory-cheaper lookups.
public bool CanSee(node noderef, bool Safe)
{
return CanSee(noderef, Safe, new byte[(Collection.Length + 7) >> 3]);
}
private bool CanSee(node noderef, bool Safe, byte[] bitmap)
{
if(Contains(noderef))
return true;
bitmap[id >> 3] |= 1 << (id & 7);
if(Safe)
System.Threading.Monitor.Enter(refs);
try
{
for(int n = 0;n != refs.Count;n++)
{
int rid = refs[n].id;
if(bitmap[rid >> 3] & (1 << (rid & 7)) == 0)
{
if(CanSee(noderef, Safe, bitmap))
return true;
}
}
}
finally
{
if(Safe)
System.Threading.Monitor.Exit(refs);
}
return false;
}
}
void main()
{
node[] nodes = new node[262111];
for(int n = 0;n < nodes.Length;n++)
{
nodes[n] = new node(n, nodes);
}
//fill data here
//test data here
}
edit:
CanSee یک مشکل داره که اول گره های مربوط درجه اول رو تست میکنه بعد علامت میزنه.توی حالت های ستاره ای یک مقدار سرعتش کم میشه.اگر خیلی سرعت کمه میتونی تغییرش بدی.احتمالا میشه تا ۱۰ ۲۰ درصد سریعترش کرد.