implementation of case-control matching algorithm