@@ -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+
5572void 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
6085void 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 ;
0 commit comments