Skip to content

Instantly share code, notes, and snippets.

@jtrupiano
Created May 4, 2009 14:12
Show Gist options
  • Save jtrupiano/106477 to your computer and use it in GitHub Desktop.
Save jtrupiano/106477 to your computer and use it in GitHub Desktop.
// My common scenario is that I need to compare a single Patient to a set of Donors, basically performing the equiavalent to Enumerable#all? in Ruby.
// To fully understand this example, realize that the NodeData object contains a single Patient and one or more Donors. When looping, we're comparing one NodeData's Patient with a separate NodeData's list of Donors.
// Using the following iterator
bool KpdDriver::donorIterator(NodeData& recip_nd, NodeData& donor_nd, bool (kpd::drivers::KpdDriver::* pt2func)(Patient, Donor)) {
Patient p = recip_nd.patient;
std::list<Donor>::const_iterator donor_iter;
bool ret = false;
for (donor_iter = donor_nd.donors.begin(); donor_iter != donor_nd.donors.end(); ++donor_iter) {
if ((this->*pt2func)(p, *donor_iter)) {
ret = true;
break;
}
}
return ret;
}
// I can then write dead simple comparator functions, e.g.
// Simple comparator
bool KpdDriver::hepBTest(Patient p, Donor d) {
return p.hep_b_core_ab_positive_acceptable || !d.hep_b;
}
// Complex comparator
bool KpdDriver::bloodTypeTest(Patient p, Donor d) {
if (d.A <= p.A && d.B <= p.B) {
return true;
}
// Let's check the rare exceptions, first A2 --> B or O
bool patient_is_O_or_B = (p.getBloodType() == BT_O) || (p.getBloodType() == BT_B);
bool donor_is_A2 = (d.getBloodType() == BT_A) && (d.abo_subtype == 2);
if (patient_is_O_or_B && donor_is_A2) {
return true;
}
// Now A2B --> B
bool patient_is_B = p.getBloodType() == BT_B;
bool donor_is_A2B = d.getBloodType() == BT_AB && d.abo_subtype == 2;
if (patient_is_B && donor_is_A2B) {
return true;
}
return false;
}
// And then I can invoke these with a single line of code over all Donors in the list..
donorIterator(recip_nd, donor_nd, &KpdDriver::bloodTypeTest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment