private static void intersectLists(ArrayList A, ArrayList B, ArrayList result) {
// The optimization is done according to the following truth
// (A|B|C) intersect (B|C|E|D)) == B|C|(A inter E)|(A inter D)
//
// We also check on any duplicates in the result
bool[] aDone=new bool[A.Count]; //used to avoid duplicates in result
bool[] bDone=new bool[B.Count];
int ia=0;
int ib=0;
// Round 1st
// Getting rid of same permissons in the input arrays (assuming X /\ X = X)
foreach (EndpointPermission a in A) {
ib = 0;
foreach (EndpointPermission b in B) {
// check to see if b is in the result already
if (!bDone[ib]) {
//if both elements are the same, copy it into result
if (a.Equals(b)) {
result.Add(a);
aDone[ia]=bDone[ib]=true;
//since permissions are ORed we can break and go to the next A
break;
}
}
++ib;
} //foreach b in B
++ia;
} //foreach a in A
ia = 0;
// Round second
// Grab only intersections of objects not found in both A and B
foreach (EndpointPermission a in A) {
if (!aDone[ia]) {
ib = 0;
foreach(EndpointPermission b in B) {
if (!bDone[ib]) {
EndpointPermission intesection = a.Intersect(b);
if (intesection != null) {
bool found = false;
// check to see if we already have the same result
foreach (EndpointPermission res in result) {
if (res.Equals(intesection)) {
found = true;
break;
}
}
if (!found) {
result.Add(intesection);
}
}
} //!Done[ib]
++ib;
} //foreach b in B
} //!Done[ia]
++ia;
} //foreach a in A
}