jtrupiano (owner)

Revisions

gist: 106477 Download_button fork
public
Public Clone URL: git://gist.github.com/106477.git
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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)