Skip to content

Commit

Permalink
Make -c opt mode build without warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasColthurst committed Aug 12, 2024
1 parent 9938136 commit 7f1c537
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 35 deletions.
6 changes: 5 additions & 1 deletion cxx/clean_relation.hh
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,11 @@ class CleanRelation : public Relation<T> {
void set_cluster_assignment_gibbs(const Domain& domain, const T_item& item,
int table, std::mt19937* prng) {
int table_current = domain.get_cluster_assignment(item);
assert(table != table_current);
if (table == table_current) {
printf("table %d == table_current in set_cluster_assignment_gibbs\n",
table);
assert(false);
}
for (const T_items& items : data_r.at(domain.name).at(item)) {
ValueType x = data.at(items);
// Remove from current cluster.
Expand Down
6 changes: 4 additions & 2 deletions cxx/distributions/get_distribution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ DistributionSpec::DistributionSpec(
distribution = DistributionEnum::string_skellam;
observation_type = ObservationEnum::string_type;
} else {
assert(false && "Unsupported distribution name.");
printf("Unknown distribution name %s\n", dist_name.c_str());
std::exit(1);
}
}

Expand Down Expand Up @@ -104,6 +105,7 @@ DistributionVariant get_prior(const DistributionSpec& spec,
return new DistributionAdapter<int>(s);
}
default:
assert(false && "Unsupported distribution enum value.");
printf("Unknown distribution enum value %d.\n", (int)(spec.distribution));
std::exit(1);
}
}
4 changes: 3 additions & 1 deletion cxx/emissions/base.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cassert>
#include <cstdlib>
#include <random>

#include "distributions/base.hh"
Expand All @@ -9,7 +10,8 @@ template <typename SampleType = double>
class Emission : public Distribution<std::pair<SampleType, SampleType>> {
public:
virtual std::pair<SampleType, SampleType> sample(std::mt19937* prng) {
assert(false && "sample() should never be called on an Emission\n");
printf("sample() should never be called on an Emission\n");
std::exit(1);
}

// Return a stochastically corrupted version of clean.
Expand Down
2 changes: 1 addition & 1 deletion cxx/emissions/categorical.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CategoricalEmission : public Emission<int> {
int propose_clean(const std::vector<int>& corrupted,
std::mt19937* unused_prng) {
// Brute force; compute log prob over all possible clean states.
int best_clean;
int best_clean = 0;
double best_clean_logp = std::numeric_limits<double>::lowest();
for (size_t i = 0; i < emission_dists.size(); ++i) {
double lp = 0.0;
Expand Down
7 changes: 5 additions & 2 deletions cxx/emissions/get_emission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "emissions/get_emission.hh"

#include <cassert>
#include <cstdlib>
#include <random>
#include <string>

Expand Down Expand Up @@ -37,7 +38,8 @@ EmissionSpec::EmissionSpec(
emission = EmissionEnum::sometimes_gaussian;
observation_type = ObservationEnum::double_type;
} else {
assert(false && "Unsupported emission name.");
printf("Unknown Emission name %s\n", emission_str.c_str());
std::exit(1);
}
}

Expand All @@ -59,6 +61,7 @@ EmissionVariant get_prior(const EmissionSpec& spec, std::mt19937* prng) {
case EmissionEnum::sometimes_gaussian:
return new Sometimes<double>(new GaussianEmission());
default:
assert(false && "Unsupported emission enum value.");
printf("Unknown Emission enum value %d.\n", (int)(spec.emission));
std::exit(1);
}
}
2 changes: 1 addition & 1 deletion cxx/emissions/simple_string.hh
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class SimpleStringEmission : public Emission<std::string> {
while (true) {
std::unordered_map<char, int> counts;
int max_count = 0;
char mode;
char mode = '\0';
for (const std::string& s : corrupted) {
char c;
if (i < s.length()) {
Expand Down
2 changes: 1 addition & 1 deletion cxx/emissions/sometimes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Sometimes : public Emission<SampleType> {
// BetaBernoulli instances for each choice of clean and picking the
// clean with the highest combined logp_score().
std::unordered_map<SampleType, int> counts;
SampleType mode;
SampleType mode = corrupted[0];
int max_count = 0;
for (const SampleType& c : corrupted) {
++counts[c];
Expand Down
8 changes: 6 additions & 2 deletions cxx/hirm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ void HIRM::transition_cluster_assignment_relation(std::mt19937* prng,
crp.incorporate(rc, choice);
assert(irms.size() == crp.tables.size());
for (const auto& [table, irm] : irms) {
assert(crp.tables.contains(table));
if (!crp.tables.contains(table)) {
assert(false);
}
}

// Update any parent relations to point to the new relation as a base
Expand Down Expand Up @@ -204,7 +206,9 @@ void HIRM::set_cluster_assignment_gibbs(std::mt19937* prng,
update_base_relation(r);
assert(irms.size() == crp.tables.size());
for (const auto& [table, irm] : irms) {
assert(crp.tables.contains(table));
if (!crp.tables.contains(table)) {
assert(false);
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion cxx/irm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "irm.hh"

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <variant>
Expand All @@ -21,7 +22,9 @@ RelationVariant clean_relation_from_spec(const std::string& name,
case ObservationEnum::string_type:
return new CleanRelation<std::string>(name, spec, doms);
default:
assert(false && "Unsupported observation type.");
printf("Unknown observation type %d for relation %s\n",
(int)(spec.observation_type), name.c_str());
std::exit(1);
}
}

Expand Down
2 changes: 1 addition & 1 deletion cxx/pclean/pclean.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Apache License, Version 2.0, refer to LICENSE.txt

// Example usage:
// ./pclean --schema=assets/flights.schema --obs=assets/flights_dirty.csv \
// ./pclean --schema=assets/flights.schema --obs=assets/flights_dirty.csv
// --iters=5

#include <iostream>
Expand Down
2 changes: 2 additions & 0 deletions cxx/pclean/schema_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ PCleanVariable PCleanSchemaHelper::get_scalarvar_from_path(
printf("Error: could not find name %s in class %s\n",
s.c_str(), base_class.name.c_str());
assert(false);
PCleanVariable pcv;
return pcv;
}

std::vector<std::string> reorder_domains(
Expand Down
16 changes: 9 additions & 7 deletions cxx/tests/test_hirm_animals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ int main(int argc, char** argv) {

// Marginally normalized.
int persiancat = enc["animal"]["persiancat"];
auto p0_black_persiancat = hirm.logp({{"black", {persiancat}, false}}, &prng);
auto p1_black_persiancat = hirm.logp({{"black", {persiancat}, true}}, &prng);
[[maybe_unused]] auto p0_black_persiancat = hirm.logp({{"black", {persiancat}, false}}, &prng);
[[maybe_unused]] auto p1_black_persiancat = hirm.logp({{"black", {persiancat}, true}}, &prng);
assert(abs(logsumexp({p0_black_persiancat, p1_black_persiancat})) < 1e-10);

// Marginally normalized.
int sheep = enc["animal"]["sheep"];
auto p0_solitary_sheep = hirm.logp({{"solitary", {sheep}, false}}, &prng);
auto p1_solitary_sheep = hirm.logp({{"solitary", {sheep}, true}}, &prng);
[[maybe_unused]] auto p0_solitary_sheep = hirm.logp(
{{"solitary", {sheep}, false}}, &prng);
[[maybe_unused]] auto p1_solitary_sheep = hirm.logp(
{{"solitary", {sheep}, true}}, &prng);
assert(abs(logsumexp({p0_solitary_sheep, p1_solitary_sheep})) < 1e-10);

// Jointly normalized.
Expand All @@ -99,7 +101,7 @@ int main(int argc, char** argv) {
{{"black", {persiancat}, true}, {"solitary", {sheep}, false}}, &prng);
auto p11_black_persiancat_solitary_sheep = hirm.logp(
{{"black", {persiancat}, true}, {"solitary", {sheep}, true}}, &prng);
auto Z = logsumexp({
[[maybe_unused]] auto Z = logsumexp({
p00_black_persiancat_solitary_sheep,
p01_black_persiancat_solitary_sheep,
p10_black_persiancat_solitary_sheep,
Expand Down Expand Up @@ -133,7 +135,7 @@ int main(int argc, char** argv) {
assert(abs(irx->logp_score() - irm->logp_score()) < 1e-8);
// Check domains agree.
for (const auto& [d, dm] : irm->domains) {
auto dx = irx->domains.at(d);
[[maybe_unused]] auto dx = irx->domains.at(d);
assert(dm->items == dx->items);
assert(dm->crp.assignments == dx->crp.assignments);
assert(dm->crp.tables == dx->crp.tables);
Expand All @@ -150,7 +152,7 @@ int main(int argc, char** argv) {
assert(rm->data_r == rx->data_r);
assert(rm->clusters.size() == rx->clusters.size());
for (const auto& [z, clusterm] : rm->clusters) {
auto clusterx = rx->clusters.at(z);
[[maybe_unused]] auto clusterx = rx->clusters.at(z);
assert(clusterm->N == clusterx->N);
}
}
Expand Down
12 changes: 6 additions & 6 deletions cxx/tests/test_irm_two_relations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ int main(int argc, char** argv) {
auto p0 =
reinterpret_cast<T_r>(std::get<Relation<bool>*>(irm.relations.at("R1")))
->logp({x1, x2}, false, &prng);
auto p0_irm = irm.logp({{"R1", {x1, x2}, false}}, &prng);
[[maybe_unused]] auto p0_irm = irm.logp({{"R1", {x1, x2}, false}}, &prng);
assert(abs(p0 - p0_irm) < 1e-10);
auto p1 =
reinterpret_cast<T_r>(std::get<Relation<bool>*>(irm.relations.at("R1")))
->logp({x1, x2}, true, &prng);
auto Z = logsumexp({p0, p1});
[[maybe_unused]] auto Z = logsumexp({p0, p1});
assert(abs(Z) < 1e-10);
assert(abs(exp(p0) - expected_p0[x1].at(x2)) < .1);
}
Expand All @@ -104,7 +104,7 @@ int main(int argc, char** argv) {
irm.logp({{"R1", {x1, x2}, true}, {"R1", {x1, x3}, false}}, &prng);
auto p11 =
irm.logp({{"R1", {x1, x2}, true}, {"R1", {x1, x3}, true}}, &prng);
auto Z = logsumexp({p00, p01, p10, p11});
[[maybe_unused]] auto Z = logsumexp({p00, p01, p10, p11});
assert(abs(Z) < 1e-10);
}

Expand Down Expand Up @@ -134,8 +134,8 @@ int main(int argc, char** argv) {
assert(abs(irx.logp_score() - irm.logp_score()) < 1e-8);
// Check domains agree.
for (const auto& d : {"D1", "D2"}) {
auto dm = irm.domains.at(d);
auto dx = irx.domains.at(d);
[[maybe_unused]] auto dm = irm.domains.at(d);
[[maybe_unused]] auto dx = irx.domains.at(d);
assert(dm->items == dx->items);
assert(dm->crp.assignments == dx->crp.assignments);
assert(dm->crp.tables == dx->crp.tables);
Expand All @@ -152,7 +152,7 @@ int main(int argc, char** argv) {
assert(rm->data_r == rx->data_r);
assert(rm->clusters.size() == rx->clusters.size());
for (const auto& [z, clusterm] : rm->clusters) {
auto clusterx = rx->clusters.at(z);
[[maybe_unused]] auto clusterx = rx->clusters.at(z);
assert(clusterm->N == clusterx->N);
}
}
Expand Down
6 changes: 3 additions & 3 deletions cxx/tests/test_misc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ int main(int argc, char** argv) {
irm4.domains.at("feature")->crp.alpha = irm3.domains.at("feature")->crp.alpha;
assert(abs(irm3.logp_score() - irm4.logp_score()) < 1e-8);
for (const auto& d : {"animal", "feature"}) {
auto d3 = irm3.domains.at(d);
auto d4 = irm4.domains.at(d);
[[maybe_unused]] auto d3 = irm3.domains.at(d);
[[maybe_unused]] auto d4 = irm4.domains.at(d);
assert(d3->items == d4->items);
assert(d3->crp.assignments == d4->crp.assignments);
assert(d3->crp.tables == d4->crp.tables);
Expand All @@ -143,7 +143,7 @@ int main(int argc, char** argv) {
assert(r3->data_r == r4->data_r);
assert(r3->clusters.size() == r4->clusters.size());
for (const auto& [z, cluster3] : r3->clusters) {
auto cluster4 = r4->clusters.at(z);
[[maybe_unused]] auto cluster4 = r4->clusters.at(z);
assert(cluster3->N == cluster4->N);
}
}
Expand Down
12 changes: 7 additions & 5 deletions cxx/util_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ T_schema load_schema(const std::string& path) {
printf("Error parsing schema line %s: expected ',' or ']' after parameter definition\n", line.c_str());
printf("Got %s of type %d instead\n",
tokens[i].val.c_str(), (int)(tokens[i].type));
assert(false);
std::exit(1);
}

} while (tokens[i].val == ",");
Expand Down Expand Up @@ -134,7 +134,7 @@ T_schema load_schema(const std::string& path) {
printf("Error parsing schema line %s: expected ',' or ')' after domain\n", line.c_str());
printf("Got %s of type %d instead\n",
tokens[i].val.c_str(), (int)(tokens[i].type));
assert(false);
std::exit(1);
}
} while (tokens[i++].val == ",");

Expand Down Expand Up @@ -187,7 +187,7 @@ T_observations load_observations(const std::string& path, T_schema& schema) {

if (!schema.contains(relname)) {
printf("Can not find %s in schema\n", relname.c_str());
assert(false);
std::exit(1);
}

std::string word;
Expand Down Expand Up @@ -358,7 +358,7 @@ void incorporate_observations(std::mt19937* prng,
if (!base_to_noisy.contains(noisy_name)) {
if (!observations.contains(noisy_name)) {
printf("Relation %s has no observations and is not the base of a noisy relation.\n", noisy_name.c_str());
assert(false);
std::exit(false);
}
}
noisy_to_base[noisy_name] = base_name;
Expand Down Expand Up @@ -577,7 +577,9 @@ load_clusters_hirm(const std::string& path) {

assert(relations.size() == irms.size());
for (const auto& [t, rs] : relations) {
assert(irms.count(t) == 1);
if (irms.count(t) != 1) {
assert(false);
}
}
fp.close();
return std::make_pair(relations, irms);
Expand Down
2 changes: 1 addition & 1 deletion cxx/util_parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bool tokenize(std::string line, std::vector<Token>* tokens) {
t.val += c;
} else if ((c == '"') || (c == '\'')) { // Strings
t.type = TokenType::string;
char sc;
char sc = '$';
while (i < line.length()) {
sc = line[i++];
if (sc == c) { // End of string
Expand Down

0 comments on commit 7f1c537

Please sign in to comment.