Skip to content

Commit

Permalink
refactor: use std::unique_ptr for memory management
Browse files Browse the repository at this point in the history
  • Loading branch information
william-silversmith committed Feb 18, 2024
1 parent e9d3214 commit 5dff147
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions python/edt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
#ifndef EDT_H
#define EDT_H

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <algorithm>
#include <limits>
#include <memory>
#include "threadpool.h"

// The pyedt namespace contains the primary implementation,
Expand Down Expand Up @@ -173,13 +174,13 @@ void squared_edt_1d_parabolic(
const float w2 = anisotropy * anisotropy;

int k = 0;
int* v = new int[n]();
float* ff = new float[n]();
std::unique_ptr<int[]> v(new int[n]());
std::unique_ptr<float[]> ff(new float[n]());
for (long int i = 0; i < n; i++) {
ff[i] = f[i * stride];
}

float* ranges = new float[n + 1]();
std::unique_ptr<float[]> ranges(new float[n + 1]());

ranges[0] = -INFINITY;
ranges[1] = +INFINITY;
Expand Down Expand Up @@ -231,10 +232,6 @@ void squared_edt_1d_parabolic(
f[i * stride] = std::fminf(w2 * sq(n - i), f[i * stride]);
}
}

delete [] v;
delete [] ff;
delete [] ranges;
}

// about 5% faster
Expand All @@ -252,13 +249,13 @@ void squared_edt_1d_parabolic(
const float w2 = anisotropy * anisotropy;

int k = 0;
int* v = new int[n]();
float* ff = new float[n]();
std::unique_ptr<int[]> v(new int[n]());
std::unique_ptr<float[]> ff(new float[n]());
for (long int i = 0; i < n; i++) {
ff[i] = f[i * stride];
}

float* ranges = new float[n + 1]();
std::unique_ptr<float[]> ranges(new float[n + 1]());

ranges[0] = -INFINITY;
ranges[1] = +INFINITY;
Expand Down Expand Up @@ -302,10 +299,6 @@ void squared_edt_1d_parabolic(
envelope = std::fminf(w2 * sq(i + 1), w2 * sq(n - i));
f[i * stride] = std::fminf(envelope, f[i * stride]);
}

delete [] v;
delete [] ff;
delete [] ranges;
}

void _squared_edt_1d_parabolic(
Expand Down

0 comments on commit 5dff147

Please sign in to comment.