R implementation of all pairs class