using
System;
using
System.Collections.Generic;
public
class
ProtoVanEmdeBoas
{
public
int
universeSize;
public
ProtoVanEmdeBoas summary;
public
List<ProtoVanEmdeBoas> clusters;
public
ProtoVanEmdeBoas(
int
size)
{
universeSize = size;
if
(size <= 2)
{
summary =
null
;
clusters =
new
List<ProtoVanEmdeBoas>(size);
for
(
int
i = 0; i < size; i++)
{
clusters.Add(
null
);
}
}
else
{
summary =
new
ProtoVanEmdeBoas(root(size));
clusters =
new
List<ProtoVanEmdeBoas>(root(size));
for
(
int
i = 0; i < root(size); i++)
{
clusters.Add(
null
);
}
for
(
int
i = 0; i < root(size); i++)
{
clusters[i] =
new
ProtoVanEmdeBoas(root(size));
}
}
}
public
int
root(
int
u) {
return
(
int
)Math.Sqrt(u); }
public
int
high(
int
x) {
return
x / root(universeSize); }
public
int
low(
int
x) {
return
x % root(universeSize); }
public
int
generateIndex(
int
cluster,
int
position)
{
return
cluster * root(universeSize) + position;
}
}
class
Program
{
static
bool
isMember(ProtoVanEmdeBoas helper,
int
key)
{
if
(key >= helper.universeSize)
{
return
false
;
}
if
(helper.universeSize == 2)
{
return
helper.clusters[key] !=
null
;
}
else
{
return
isMember(
helper.clusters[helper.high(key)],
helper.low(key));
}
}
static
void
insert(ProtoVanEmdeBoas helper,
int
key)
{
if
(helper.universeSize == 2)
{
helper.clusters[key] =
new
ProtoVanEmdeBoas(1);
}
else
{
insert(helper.clusters[helper.high(key)],
helper.low(key));
insert(helper.summary, helper.high(key));
}
}
static
void
Main()
{
ProtoVanEmdeBoas hello =
new
ProtoVanEmdeBoas(4);
Console.WriteLine(isMember(hello, 3));
insert(hello, 3);
Console.WriteLine(isMember(hello, 3));
}}