Skip to content

Commit

Permalink
working on const correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
kosloot committed Oct 20, 2023
1 parent 7b0a366 commit aa4108e
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 111 deletions.
10 changes: 6 additions & 4 deletions include/timbl/Features.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ namespace Timbl {
void Min( const double val ){ n_min = val; };
double Max() const { return n_max; };
void Max( const double val ){ n_max = val; };
double fvDistance( FeatureValue *, FeatureValue *, size_t=1 ) const;
double fvDistance( const FeatureValue *,
const FeatureValue *,
size_t=1 ) const;
FeatureValue *add_value( const icu::UnicodeString&, TargetValue *, int=1 );
FeatureValue *add_value( size_t, TargetValue *, int=1 );
FeatureValue *Lookup( const icu::UnicodeString& ) const;
bool decrement_value( FeatureValue *, TargetValue * );
bool increment_value( FeatureValue *, TargetValue * );
bool decrement_value( FeatureValue *, const TargetValue * );
bool increment_value( FeatureValue *, const TargetValue * );
size_t EffectiveValues() const;
size_t TotalValues() const;
bool isNumerical() const;
Expand All @@ -143,7 +145,7 @@ namespace Timbl {
void NumStatistics( double, const Targets&, int, bool );
void ClipFreq( size_t f ){ matrix_clip_freq = f; };
size_t ClipFreq() const { return matrix_clip_freq; };
SparseSymetricMatrix<ValueClass *> *metric_matrix;
SparseSymetricMatrix<const ValueClass *> *metric_matrix;
private:
Feature( const Feature& );
Feature& operator=( const Feature& );
Expand Down
4 changes: 2 additions & 2 deletions include/timbl/IBtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace Timbl {
std::vector<unsigned int>& );
const ClassDistribution *exact_match( const Instance& ) const;
protected:
const IBtree *search_node( FeatureValue * ) const;
const IBtree *search_node( const FeatureValue * ) const;
};

typedef std::unordered_map<size_t, const IBtree*> FI_map;
Expand Down Expand Up @@ -226,7 +226,7 @@ namespace Timbl {
Targets&,
int );
void fill_index();
const IBtree *fast_search_node( FeatureValue * );
const IBtree *fast_search_node( const FeatureValue * );
};

class IB_InstanceBase: public InstanceBase_base {
Expand Down
43 changes: 22 additions & 21 deletions include/timbl/Metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ namespace Timbl{
virtual bool isSimilarityMetric() const = 0;
virtual bool isNumerical() const = 0;
virtual bool isStorable() const = 0;
virtual double distance( FeatureValue *, FeatureValue *,
virtual double distance( const FeatureValue *,
const FeatureValue *,
size_t=1, double = 1.0 ) const = 0;
virtual double get_max_similarity() const {
throw std::logic_error( "get_max_similarity not implemented for " +
Expand All @@ -68,8 +69,8 @@ namespace Timbl{
OverlapMetric(): distanceMetricClass( Overlap ){};
bool isNumerical() const override { return false; };
bool isStorable() const override { return false; };
double distance( FeatureValue *,
FeatureValue *,
double distance( const FeatureValue *,
const FeatureValue *,
size_t,
double ) const override;
};
Expand All @@ -85,17 +86,17 @@ namespace Timbl{
class NumericMetric: public NumericMetricClass {
public:
NumericMetric(): NumericMetricClass( Numeric ){};
double distance( FeatureValue *,
FeatureValue *,
double distance( const FeatureValue *,
const FeatureValue *,
size_t,
double ) const override;
};

class EuclideanMetric: public NumericMetricClass {
public:
EuclideanMetric(): NumericMetricClass( Euclidean ){};
double distance( FeatureValue *,
FeatureValue *,
double distance( const FeatureValue *,
const FeatureValue *,
size_t,
double ) const override;
};
Expand All @@ -105,8 +106,8 @@ namespace Timbl{
ValueDiffMetric(): distanceMetricClass( ValueDiff ){};
bool isNumerical() const override { return false; };
bool isStorable() const override { return true; };
double distance( FeatureValue *,
FeatureValue *,
double distance( const FeatureValue *,
const FeatureValue *,
size_t,
double ) const override;
};
Expand All @@ -116,8 +117,8 @@ namespace Timbl{
DiceMetric(): distanceMetricClass( Dice ){};
bool isNumerical() const override { return false; };
bool isStorable() const override { return true; };
double distance( FeatureValue *,
FeatureValue *,
double distance( const FeatureValue *,
const FeatureValue *,
size_t,
double ) const override;
};
Expand All @@ -127,8 +128,8 @@ namespace Timbl{
JeffreyMetric(): distanceMetricClass( JeffreyDiv ){};
bool isNumerical() const override{ return false; };
bool isStorable() const override { return true; };
double distance( FeatureValue *,
FeatureValue *,
double distance( const FeatureValue *,
const FeatureValue *,
size_t,
double ) const override;
};
Expand All @@ -138,8 +139,8 @@ namespace Timbl{
JSMetric(): distanceMetricClass( JSDiv ){};
bool isNumerical() const override { return false; };
bool isStorable() const override { return true; };
double distance( FeatureValue *,
FeatureValue *,
double distance( const FeatureValue *,
const FeatureValue *,
size_t,
double ) const override;
};
Expand All @@ -149,8 +150,8 @@ namespace Timbl{
LevenshteinMetric(): distanceMetricClass( Levenshtein ){};
bool isNumerical() const override { return false; };
bool isStorable() const override { return true; };
double distance( FeatureValue *,
FeatureValue *,
double distance( const FeatureValue *,
const FeatureValue *,
size_t,
double ) const override;
};
Expand All @@ -167,8 +168,8 @@ namespace Timbl{
class CosineMetric: public similarityMetricClass {
public:
CosineMetric(): similarityMetricClass( Cosine ){};
double distance( FeatureValue *,
FeatureValue *,
double distance( const FeatureValue *,
const FeatureValue *,
size_t,
double ) const override;
double get_max_similarity() const override { return 1.0; };
Expand All @@ -177,8 +178,8 @@ namespace Timbl{
class DotProductMetric: public similarityMetricClass {
public:
DotProductMetric(): similarityMetricClass( DotProduct ){};
double distance( FeatureValue *,
FeatureValue *,
double distance( const FeatureValue *,
const FeatureValue *,
size_t,
double ) const override;
double get_max_similarity() const override {
Expand Down
28 changes: 14 additions & 14 deletions include/timbl/Testers.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ namespace Timbl{
class metricTestFunction {
public:
virtual ~metricTestFunction(){};
virtual double test( FeatureValue *,
FeatureValue *,
Feature * ) const = 0;
virtual double test( const FeatureValue *,
const FeatureValue *,
const Feature * ) const = 0;
};

class overlapTestFunction: public metricTestFunction {
public:
double test( FeatureValue *FV,
FeatureValue *G,
Feature *Feat ) const override;
double test( const FeatureValue *FV,
const FeatureValue *G,
const Feature *Feat ) const override;
};

class valueDiffTestFunction: public metricTestFunction {
Expand All @@ -51,9 +51,9 @@ namespace Timbl{
metricTestFunction(),
threshold( t )
{};
double test( FeatureValue *,
FeatureValue *,
Feature * ) const override;
double test( const FeatureValue *,
const FeatureValue *,
const Feature * ) const override;
protected:
int threshold;
};
Expand All @@ -65,7 +65,7 @@ namespace Timbl{
TesterClass& operator=( const TesterClass& ) = delete; // inhibit copies
virtual ~TesterClass(){};
void init( const Instance&, size_t, size_t );
virtual size_t test( std::vector<FeatureValue *>&,
virtual size_t test( const std::vector<FeatureValue *>&,
size_t,
double ) = 0;
virtual double getDistance( size_t ) const = 0;
Expand All @@ -87,7 +87,7 @@ namespace Timbl{
int );
~DistanceTester();
double getDistance( size_t ) const override;
size_t test( std::vector<FeatureValue *>&,
size_t test( const std::vector<FeatureValue *>&,
size_t,
double ) override;
private:
Expand All @@ -99,7 +99,7 @@ namespace Timbl{
explicit SimilarityTester( const Feature_List& pf ):
TesterClass( pf ){};
~SimilarityTester() {};
virtual size_t test( std::vector<FeatureValue *>&,
virtual size_t test( const std::vector<FeatureValue *>&,
size_t,
double ) override = 0;
protected:
Expand All @@ -111,7 +111,7 @@ namespace Timbl{
explicit CosineTester( const Feature_List& pf ):
SimilarityTester( pf ){};
double getDistance( size_t ) const override;
size_t test( std::vector<FeatureValue *>&,
size_t test( const std::vector<FeatureValue *>&,
size_t,
double ) override;
private:
Expand All @@ -122,7 +122,7 @@ namespace Timbl{
explicit DotProductTester( const Feature_List& pf ):
SimilarityTester( pf ){};
double getDistance( size_t ) const override;
size_t test( std::vector<FeatureValue *>&,
size_t test( const std::vector<FeatureValue *>&,
size_t,
double ) override;
private:
Expand Down
32 changes: 16 additions & 16 deletions src/Choppers.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ namespace Timbl{

UnicodeString C45_Chopper::getString() const{
UnicodeString res;
for ( const auto& chop : choppedInput ) {
res += CodeToStr( chop ) + ",";
for ( const auto& part : choppedInput ) {
res += CodeToStr( part ) + ",";
}
return res;
}
Expand Down Expand Up @@ -390,11 +390,11 @@ namespace Timbl{
UnicodeString Bin_Chopper::getString() const {
UnicodeString res;
int i = 1;
for ( const auto& chop : choppedInput ){
if ( &chop == &choppedInput.back() ){
for ( const auto& part : choppedInput ){
if ( &part == &choppedInput.back() ){
break;
}
if ( chop[0] == '1' ){
if ( part[0] == '1' ){
res += TiCC::toUnicodeString(i) + ",";
}
++i;
Expand All @@ -415,13 +415,13 @@ namespace Timbl{
return false;
}
size_t i = 0;
for ( auto& chop : choppedInput ){
for ( auto& part : choppedInput ){
size_t index = i * fLen;
// Scan the value.
//
chop.remove();
part.remove();
for ( int j = 0; j < fLen; ++j ) {
chop += strippedInput[index++];
part += strippedInput[index++];
}
++i;
}
Expand All @@ -430,8 +430,8 @@ namespace Timbl{

UnicodeString Compact_Chopper::getString() const {
UnicodeString res;
for ( const auto& chop : choppedInput ){
res += CodeToStr( chop );
for ( const auto& part : choppedInput ){
res += CodeToStr( part );
}
return res;
}
Expand Down Expand Up @@ -475,8 +475,8 @@ namespace Timbl{

UnicodeString Tabbed_Chopper::getString() const {
UnicodeString res;
for ( const auto& chop : choppedInput ){
res += CodeToStr( chop ) + "\t";
for ( const auto& part : choppedInput ){
res += CodeToStr( part ) + "\t";
}
return res;
}
Expand Down Expand Up @@ -528,13 +528,13 @@ namespace Timbl{
UnicodeString Sparse_Chopper::getString() const {
UnicodeString res;
int i = 1;
for ( const auto& chop : choppedInput ){
if ( &chop == &choppedInput.back() ){
for ( const auto& part : choppedInput ){
if ( &part == &choppedInput.back() ){
break;
}
if ( chop != DefaultSparseString ){
if ( part != DefaultSparseString ){
res += "(" + TiCC::toUnicodeString( i ) + ",";
res += CodeToStr(chop);
res += CodeToStr(part);
res += ")";
}
++i;
Expand Down
15 changes: 8 additions & 7 deletions src/Features.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ namespace Timbl {
}

bool Feature::increment_value( FeatureValue *FV,
TargetValue *tv ){
const TargetValue *tv ){
bool result = false;
if ( FV ){
FV->incr_val_freq();
Expand All @@ -208,7 +208,8 @@ namespace Timbl {
return result;
}

bool Feature::decrement_value( FeatureValue *FV, TargetValue *tv ){
bool Feature::decrement_value( FeatureValue *FV,
const TargetValue *tv ){
bool result = false;
if ( FV ){
FV->decr_val_freq();
Expand Down Expand Up @@ -538,8 +539,8 @@ namespace Timbl {
}
}

double Feature::fvDistance( FeatureValue *F,
FeatureValue *G,
double Feature::fvDistance( const FeatureValue *F,
const FeatureValue *G,
size_t limit ) const {
double result = 0.0;
if ( F != G ){
Expand Down Expand Up @@ -757,7 +758,7 @@ namespace Timbl {
double sum = 0.0;
vector<double> store( values_array.size() );
for ( unsigned int i=0; i < values_array.size(); ++i ){
FeatureValue *FV = values_array[i];
const FeatureValue *FV = values_array[i];
double val = TiCC::stringTo<double>( FV->name() );
store[i] = val;
sum += val;
Expand Down Expand Up @@ -809,7 +810,7 @@ namespace Timbl {
return true;
}
if ( !metric_matrix ){
metric_matrix = new SparseSymetricMatrix<ValueClass*>();
metric_matrix = new SparseSymetricMatrix<const ValueClass*>();
}
if ( PrestoreStatus != ps_failed && metric->isStorable( ) ) {
try {
Expand Down Expand Up @@ -925,7 +926,7 @@ namespace Timbl {

bool Feature::fill_matrix( istream &is ) {
if ( !metric_matrix ){
metric_matrix = new SparseSymetricMatrix<ValueClass*>();
metric_matrix = new SparseSymetricMatrix<const ValueClass*>();
}
else {
metric_matrix->Clear();
Expand Down
Loading

0 comments on commit aa4108e

Please sign in to comment.