vector<vector<int> > pathSum(TreeNode *root, int sum) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
            
        vector<vector<int> > result; 
        vector<int> oneSolution;
        
        if(root == NULL)
            return result;
        int tempS = 0;
        
        tempS = root->val;
        
        oneSolution.push_back(root->val);
        
        if(root->left == NULL && root->right == NULL && tempS == sum)
            result.push_back(oneSolution);
        
        
        if(root->left != NULL)
            calculateNext(root->left, tempS+root->left->val, sum, oneSolution, result);

        
        if(root->right!=NULL)
            calculateNext(root->right, tempS+root->right->val, sum, oneSolution, result);
        
        return result; 
    }
Example #2
0
 void calculateNext(TreeNode* node, int tempS, int sum, bool &result)
 {
     if(node->left == NULL && node->right == NULL && tempS == sum)
     {
         result = true;
         return;
     }
         
     if(node->left != NULL)        
         calculateNext(node->left, tempS+node->left->val, sum, result);
     
     if(result == false && node->right!=NULL)
         calculateNext(node->right, tempS+node->right->val, sum, result); 
 }
 void calculateNext(TreeNode* node, int tempS, int sum, vector<int> onsS, vector<vector<int> >  &result)
 {
     onsS.push_back(node->val);
     if(node->left == NULL && node->right == NULL && tempS == sum)
     {
         result.push_back(onsS);
         return;
     }
         
     if(node->left != NULL)
         calculateNext(node->left, tempS+node->left->val, sum, onsS, result);
     
     if(node->right!=NULL)
         calculateNext(node->right, tempS+node->right->val, sum, onsS, result);
 }
Example #4
0
File: main.c Project: olivo/BP
int PseudoRandomUsingAtomic_nextInt(int n) {
	int read, nexts, casret, nextInt_return;
#ifdef HPRED
	__CPROVER_predicate(n==10);
#endif

	while(1) {
		read = seed;
		nexts = calculateNext(read);
		assert(nexts != read); 
		casret = PseudoRandomUsingAtomic_compareAndSet(read, nexts);
		if(casret == 1){
#ifdef USE_BRANCHING_ASSUMES
			__CPROVER_assume(casret == 1);
#endif
			nextInt_return = nexts % n;
			break;
		}else{
#ifdef USE_BRANCHING_ASSUMES
			__CPROVER_assume(!(casret == 1));
#endif
		}
	}

	return nextInt_return;
}
Example #5
0
int main(int argc, char **argv) {
  hashset hash;
  vector keys;
  printf("Usage [Order] [AmountWords] [FileName]\n");
  calculateNext(&hash, &keys, atoi(argv[1]), argv[3], atoi(argv[2]));
  return 0;
}
inline int PseudoRandomUsingAtomic_nextInt() {
	int read, nexts, nextInt_return;

	assert(seed != 0);

	__VERIFIER_atomic_acquire();
	read = seed;
	nexts = calculateNext(read);
	seed = nexts;
	__VERIFIER_atomic_release();
	nextInt_return = min(nexts,NUM);
	return nextInt_return;
}
Example #7
0
 bool hasPathSum(TreeNode *root, int sum) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     bool result = false;
     
     if(root == NULL)
         return false;
     
     int tempS = 0;
     
     tempS = root->val;
     
     if(root->left == NULL && root->right == NULL && tempS == sum)
         result = true;
     
     if(!result && root->left != NULL)        
         calculateNext(root->left, tempS+root->left->val, sum, result);
     
     if(result == false && root->right!=NULL)
         calculateNext(root->right, tempS+root->right->val, sum, result);        
     
     return result;        
 }
Example #8
0
ListNode * _002_AddTwoNumbers::addTwoNumbers(ListNode *l1, ListNode *l2)
{
    ListNode *first = new ListNode(-1);
    first->next = new ListNode(0);
    ListNode *current = first;

    while (l1 != nullptr || l2 != nullptr)
    {
        current = calculateNext(&(current->next), &l1, &l2);
    }

    if (current->next->val == 0) {
        current->next = nullptr;
    }

    return first->next;
}
inline int PseudoRandomUsingAtomic_nextInt(int n)
{
	int read, nexts, casret, nextInt_return;

	while(1) {
		read = seed;
		nexts = calculateNext(read);
		assert(nexts != read); 
		__VERIFIER_atomic_CAS(&seed,read,nexts,&casret);

		if(casret == 1){
			nextInt_return = nexts % n;
			//assume(nexts < n);
			//nextInt_return = nexts;
			break;
		}
	}

	return nextInt_return;
}
Example #10
0
File: main.c Project: olivo/BP
int PseudoRandomUsingAtomic_nextInt(int n) {
	int read, nexts, nextInt_return;

#ifdef HPRED
	__CPROVER_predicate(n==10);
#endif
#if (APRED >= 3)
	__CPROVER_predicate(nextInt_return <= 10);
	__CPROVER_predicate(nexts % n <= 10);
	//__CPROVER_predicate(calculateNext_return % n <= 10); //cannot be specified here
#endif

	atomic_begin();
	read = seed;
	nexts = calculateNext(read);
	assert(nexts != read); 
	seed = nexts;
	atomic_end();
	nextInt_return = nexts % n;

	return nextInt_return;
}
Example #11
0
File: Mesh.cpp Project: dicta/ray
bool Mesh::hit(const Ray& ray, double& tmin, ShadeRecord& sr) const {
   // Check the mesh.
   if(textureCoords.size() && textureCoords.size() != points.size()) {
      printf("%lu %lu\n", textureCoords.size(), points.size());
      exit(1);
   }

   // the following code includes modifications from Shirley and Morley (2003)
   double tx_min = (bbox.x0 - ray.origin.x) / ray.direction.x;
   double tx_max = (bbox.x1 - ray.origin.x) / ray.direction.x;
   if(ray.direction.x < 0) swap(tx_min, tx_max);

   double ty_min = (bbox.y0 - ray.origin.y) / ray.direction.y;
   double ty_max = (bbox.y1 - ray.origin.y) / ray.direction.y;
   if(ray.direction.y < 0) swap(ty_min, ty_max);

   double tz_min = (bbox.z0 - ray.origin.z) / ray.direction.z;
   double tz_max = (bbox.z1 - ray.origin.z) / ray.direction.z;
   if(ray.direction.z < 0) swap(tz_min, tz_max);

   double t0 = max(max(tx_min, ty_min), tz_min);
   double t1 = min(min(tx_max, ty_max), tz_max);

   if (t0 > t1) return(false);

   const Point3D& p = bbox.contains(ray.origin) ? ray.origin : ray(t0);

   int ix = (int) clamp((p.x - bbox.x0) * nx / (bbox.wx), 0, nx - 1);
   int iy = (int) clamp((p.y - bbox.y0) * ny / (bbox.wy), 0, ny - 1);
   int iz = (int) clamp((p.z - bbox.z0) * nz / (bbox.wz), 0, nz - 1);

   // ray parameter increments per cell in the x, y, and z directions

   double dtx = (tx_max - tx_min) / nx;
   double dty = (ty_max - ty_min) / ny;
   double dtz = (tz_max - tz_min) / nz;

   int ix_step, iy_step, iz_step;
   int ix_stop, iy_stop, iz_stop;

   double tx_next = calculateNext(ray.direction.x, tx_min, ix, dtx, nx, ix_step, ix_stop);
   double ty_next = calculateNext(ray.direction.y, ty_min, iy, dty, ny, iy_step, iy_stop);
   double tz_next = calculateNext(ray.direction.z, tz_min, iz, dtz, nz, iz_step, iz_stop);

   double t = 0;
   // Traverse the grid
   while(true) {
      int idx = ix + nx * iy + nx * ny * iz;
//      assert(idx < numCells);
      Voxel* cell = voxels[idx];

      if (tx_next < ty_next && tx_next < tz_next) {
         t = tx_next;
         if(checkCell(ray, cell, t, sr)) { tmin = t; return true; }

         tx_next += dtx;
         ix += ix_step;
         if (ix == ix_stop) return false;
      }
      else if (ty_next < tz_next) {
         t = ty_next;
         if(checkCell(ray, cell, t, sr)) { tmin = t; return true; }

         ty_next += dty;
         iy += iy_step;
         if (iy == iy_stop) return false;
      }
      else {
         t = tz_next;
         if(checkCell(ray, cell, t, sr)) { tmin = t; return true; }

         tz_next += dtz;
         iz += iz_step;
         if (iz == iz_stop) return false;
      }
   }
}
Example #12
0
File: Grid.cpp Project: dicta/ray
bool Grid::hit(const Ray& ray, double& tmin, ShadeRecord& sr) const {
	// the following code includes modifications from Shirley and Morley (2003)

   double tx_min = (bbox.x0 - ray.origin.x) / ray.direction.x;
   double tx_max = (bbox.x1 - ray.origin.x) / ray.direction.x;
   if(ray.direction.x < 0) swap(tx_min, tx_max);

   double ty_min = (bbox.y0 - ray.origin.y) / ray.direction.y;
   double ty_max = (bbox.y1 - ray.origin.y) / ray.direction.y;
   if(ray.direction.y < 0) swap(ty_min, ty_max);

   double tz_min = (bbox.z0 - ray.origin.z) / ray.direction.z;
   double tz_max = (bbox.z1 - ray.origin.z) / ray.direction.z;
   if(ray.direction.z < 0) swap(tz_min, tz_max);

	double t0 = max(max(tx_min, ty_min), tz_min);
	double t1 = min(min(tx_max, ty_max), tz_max);

	if (t0 > t1) return(false);

   Point3D p = ray.origin;
   if(!bbox.contains(ray.origin)) {
      p = ray(t0);
   }

   int ix = (int) clamp((p.x - bbox.x0) * nx / (bbox.wx), 0, nx - 1);
   int iy = (int) clamp((p.y - bbox.y0) * ny / (bbox.wy), 0, ny - 1);
   int iz = (int) clamp((p.z - bbox.z0) * nz / (bbox.wz), 0, nz - 1);

   // ray parameter increments per cell in the x, y, and z directions

   double dtx = (tx_max - tx_min) / nx;
   double dty = (ty_max - ty_min) / ny;
   double dtz = (tz_max - tz_min) / nz;

   int ix_step, iy_step, iz_step;
   int ix_stop, iy_stop, iz_stop;

   double tx_next = calculateNext(ray.direction.x, tx_min, ix, dtx, nx, ix_step, ix_stop);
   double ty_next = calculateNext(ray.direction.y, ty_min, iy, dty, ny, iy_step, iy_stop);
   double tz_next = calculateNext(ray.direction.z, tz_min, iz, dtz, nz, iz_step, iz_stop);

   // Traverse the grid
   while(true) {
      GridVoxel* cell = voxels[ix + nx * iy + nx * ny * iz];

      if (tx_next < ty_next && tx_next < tz_next) {
//         tmin = tx_next;
         if(checkCell(ray, cell, tmin, sr)) return true;

         tx_next += dtx;
         ix += ix_step;
         if (ix == ix_stop) return false;
      }
      else if (ty_next < tz_next) {
//         tmin = ty_next;
         if(checkCell(ray, cell, tmin, sr)) return true;

         ty_next += dty;
         iy += iy_step;
         if (iy == iy_stop) return false;
      }
      else {
//         tmin = tz_next;
         if(checkCell(ray, cell, tmin, sr)) return true;

         tz_next += dtz;
         iz += iz_step;
         if (iz == iz_stop) return false;
      }
   }
}
Example #13
0
File: Grid.cpp Project: dicta/ray
bool Grid::shadowHit(const Ray& ray, double& tmin) const {
   performance_counter.increment_num_shadow_rays();

   double tx_min = (bbox.x0 - ray.origin.x) / ray.direction.x;
   double tx_max = (bbox.x1 - ray.origin.x) / ray.direction.x;
   if(ray.direction.x < 0) swap(tx_min, tx_max);

   double ty_min = (bbox.y0 - ray.origin.y) / ray.direction.y;
   double ty_max = (bbox.y1 - ray.origin.y) / ray.direction.y;
   if(ray.direction.y < 0) swap(ty_min, ty_max);

   double tz_min = (bbox.z0 - ray.origin.z) / ray.direction.z;
   double tz_max = (bbox.z1 - ray.origin.z) / ray.direction.z;
   if(ray.direction.z < 0) swap(tz_min, tz_max);

   double t0 = max(max(tx_min, ty_min), tz_min);
   double t1 = min(min(tx_max, ty_max), tz_max);

   if (t0 > t1) return(false);

   Point3D p = ray.origin;
   if(!bbox.contains(ray.origin)) {
      p = ray(t0);
   }

   int ix = (int) clamp((p.x - bbox.x0) * nx / (bbox.wx), 0, nx - 1);
   int iy = (int) clamp((p.y - bbox.y0) * ny / (bbox.wy), 0, ny - 1);
   int iz = (int) clamp((p.z - bbox.z0) * nz / (bbox.wz), 0, nz - 1);

   // ray parameter increments per cell in the x, y, and z directions

   double dtx = (tx_max - tx_min) / nx;
   double dty = (ty_max - ty_min) / ny;
   double dtz = (tz_max - tz_min) / nz;

   int ix_step, iy_step, iz_step;
   int ix_stop, iy_stop, iz_stop;

   double tx_next = calculateNext(ray.direction.x, tx_min, ix, dtx, nx, ix_step, ix_stop);
   double ty_next = calculateNext(ray.direction.y, ty_min, iy, dty, ny, iy_step, iy_stop);
   double tz_next = calculateNext(ray.direction.z, tz_min, iz, dtz, nz, iz_step, iz_stop);

   // Traverse the grid
   while(true) {
      GridVoxel* cell = voxels[ix + nx * iy + nx * ny * iz];

      if (tx_next < ty_next && tx_next < tz_next) {
         if(checkCellShadow(ray, cell, tmin)) return true;

         tx_next += dtx;
         ix += ix_step;
         if (ix == ix_stop) return false;
      }
      else if (ty_next < tz_next) {
         if(checkCellShadow(ray, cell, tmin)) return true;

         ty_next += dty;
         iy += iy_step;
         if (iy == iy_stop) return false;
      }
      else {
         if(checkCellShadow(ray, cell, tmin)) return true;

         tz_next += dtz;
         iz += iz_step;
         if (iz == iz_stop) return false;
      }
   }
}