Skip to content

Commit ec2555a

Browse files
committed
destruct weighted sampler
1 parent 90f30ce commit ec2555a

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

paddle/fluid/distributed/table/weighted_sampler.cc

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,58 @@ std::vector<WeightedObject *> RandomSampler::sample_k(int k) {
3434
std::unordered_map<int, int> replace_map;
3535
while(k--){
3636
int rand_int = rand() % n;
37-
auto tmp = replace_map.find(rand_int);
38-
if(tmp == replace_map.end()){
37+
auto iter = replace_map.find(rand_int);
38+
if(iter == replace_map.end()){
3939
sample_result.push_back(edges->at(rand_int));
4040
}else{
41-
sample_result.push_back(edges->at(tmp->second));
41+
sample_result.push_back(edges->at(iter->second));
4242
}
4343

44-
tmp = replace_map.find(n - 1);
45-
if(tmp == replace_map.end()){
44+
iter = replace_map.find(n - 1);
45+
if(iter == replace_map.end()){
4646
replace_map[rand_int] = n - 1;
4747
}else{
48-
replace_map[rand_int] = tmp->second;
48+
replace_map[rand_int] = iter->second;
4949
}
5050
--n;
5151
}
5252
return sample_result;
5353
}
5454

55+
WeightedSampler::WeightedSampler(){
56+
left = nullptr;
57+
right = nullptr;
58+
object = nullptr;
59+
}
60+
61+
WeightedSampler::~WeightedSampler() {
62+
if(left != nullptr){
63+
delete left;
64+
left = nullptr;
65+
}
66+
if(right != nullptr){
67+
delete right;
68+
right = nullptr;
69+
}
70+
}
71+
5572
void WeightedSampler::build(std::vector<WeightedObject*>* edges) {
73+
if(left != nullptr){
74+
delete left;
75+
left = nullptr;
76+
}
77+
if(right != nullptr){
78+
delete right;
79+
right = nullptr;
80+
}
5681
WeightedObject** v = edges->data();
5782
return build_one(v, 0, edges->size());
5883
}
5984

6085
void WeightedSampler::build_one(WeightedObject **v, int start, int end) {
6186
count = 0;
6287
if (start + 1 == end) {
63-
left = right = NULL;
88+
left = right = nullptr;
6489
weight = v[start]->get_weight();
6590
object = v[start];
6691
count = 1;
@@ -98,7 +123,7 @@ WeightedObject *WeightedSampler::sample(
98123
std::unordered_map<WeightedSampler *, float> &subtract_weight_map,
99124
std::unordered_map<WeightedSampler *, int> &subtract_count_map,
100125
float &subtract) {
101-
if (left == NULL) {
126+
if (left == nullptr) {
102127
subtract_weight_map[this] = weight;
103128
subtract = weight;
104129
subtract_count_map[this] = 1;

paddle/fluid/distributed/table/weighted_sampler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class RandomSampler: public Sampler {
4444

4545
class WeightedSampler: public Sampler {
4646
public:
47-
virtual ~WeightedSampler() {}
47+
WeightedSampler();
48+
virtual ~WeightedSampler();
4849
WeightedSampler *left, *right;
4950
WeightedObject *object;
5051
int count;

0 commit comments

Comments
 (0)