// CIS534 homework
// Author: Prof. Milo Martin
// University of Pennsylvania
// Jan 2010

#include "compute.h"
#include <math.h>

static int64 distance(int64 a, int64 b) 
{
  if (a > b) {
    return a-b;
  } else {
    return b-a;
  }
}

void compute_one(int64 num_particles, 
                 int64 *location, 
                 int64 *weight, 
                 int64 *radius, 
                 int64 *answer)
{
  // Computation 1
  for (int64 i = 0; i < num_particles; i++) {
    for (int64 j = 0; j < num_particles; j++) {
      if (distance(location[i], location[j]) < radius[i]) {
        answer[i] += weight[j];
      }
    }
  }
}

void compute_two(int64 num_particles, 
                 int64 *location, 
                 int64 *weight, 
                 int64 *radius, 
                 int64 *answer)
{
  // Computation 2
  for (int64 i = 0; i < num_particles; i++) {
    for (int64 j = 0; j < num_particles; j++) {
      if (distance(location[i], location[j]) < radius[i]) {
        answer[i] += (weight[i] + weight[j]) | distance(location[i], location[j]);
      }
    }
  }
}

void compute_three(int64 num_particles, 
                   int64 *xlocation, 
                   int64 *ylocation, 
                   int64 *weight, 
                   int64 radius, 
                   int64 *answer)
{
  // Computation 3
  for (int64 i = 0; i < num_particles; i++) {
    for (int64 j = 0; j < num_particles; j++) {
      int64 dist = distance(xlocation[i], xlocation[j]) + distance(ylocation[i], ylocation[j]);
      if (dist < radius) {
        answer[i] += (weight[i] + weight[j]) * dist;
      }
    }
  }
}
