00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
#ifndef RealMapping_INC
00042
#define RealMapping_INC
00043
00044
00045
#include "Object.h"
00046
#include <plearn/math/TMat.h>
00047
#include <map>
00048
00049
namespace PLearn {
00050
using namespace std;
00051
00053 class RealRange
00054 {
00055
public:
00056 real low;
00057 real high;
00058 char leftbracket;
00059 char rightbracket;
00060
00061
public:
00062 RealRange():
00063
low(0),
high(0),
leftbracket(']'),
rightbracket('[')
00064 {}
00065
00066
00067 RealRange(
char leftbracket_,
real low_,
real high_,
char rightbracket_):
00068
low(low_),
high(high_),
leftbracket(leftbracket_),
rightbracket(rightbracket_)
00069 {
checkbrackets(); }
00070
00071 real span(){
return abs(
high-
low);}
00072
00073 void checkbrackets()
const
00074
{
00075
if( (
leftbracket!=
'[' &&
leftbracket!=
']') || (
rightbracket!=
'[' &&
rightbracket!=
']') )
00076
PLERROR(
"In RealRange: Brackets must be either '[' or ']'");
00077 }
00078
00079 void print(ostream& out)
const
00080
{ out <<
leftbracket <<
low <<
' ' <<
high <<
rightbracket; }
00081
00082 void write(ostream& out)
const
00083
{ out <<
leftbracket <<
low <<
' ' <<
high <<
rightbracket; }
00084
00085 void read(istream& in)
00086 { in >>
leftbracket >>
low >>
high >>
rightbracket;
checkbrackets(); }
00087
00088
00089
string getString() const;
00090
00096
bool contains(
real val) const;
00097
bool operator<(
real x) const;
00098
bool operator>(
real x) const;
00099
00100
00101
00102
00103
00104
00105
00106
00111
bool operator<(const
RealRange& x) const;
00112
bool operator>(const
RealRange& x) const;
00113
bool operator==(const
RealRange& rr) const;
00114 };
00115
00116 inline
void write(ostream& out, const
RealRange& range) { range.write(out); }
00117 inline ostream&
operator<<(ostream& out,
const RealRange& range) { range.
print(out);
return out; }
00118 inline void read(istream& in,
RealRange& range) { range.
read(in); }
00119
00120
PStream&
operator<<(
PStream& out,
const RealRange& x);
00121
PStream&
operator>>(
PStream& in, RealRange &x);
00122
00123
00124 class RealMapping:
public Object
00125 {
00126
protected:
00127
static void declareOptions(
OptionList& ol);
00128
00129
public:
00130 typedef pair<RealRange, real>
single_mapping_t;
00131 typedef TVec< single_mapping_t > ordered_mapping_t;
00132 typedef map<RealRange, real> mapping_t;
00133 typedef mapping_t::iterator iterator;
00134 typedef mapping_t::const_iterator
const_iterator;
00135 mapping_t mapping;
00136
00137
00138
00139 ordered_mapping_t o_mapping;
00140 real missing_mapsto;
00141 bool keep_other_as_is;
00142 real other_mapsto;
00143
00144
public:
00145 typedef Object inherited;
00146
PLEARN_DECLARE_OBJECT(
RealMapping);
00147
00148 RealMapping()
00149 :missing_mapsto(
MISSING_VALUE),
00150 keep_other_as_is(true),
00151 other_mapsto(
MISSING_VALUE)
00152 {}
00153
00154 int size()
const {
return (
int)mapping.size(); }
00155 int length()
const {
return (
int)mapping.size(); }
00156
00158 inline void clear() { mapping.clear(); }
00159
00160
void buildOrderedMapping();
00161
00162
bool checkConsistency();
00163
00164 void removeMapping(
const RealRange& range)
00165 {
00166 mapping_t::iterator it= mapping.find(range);
00167
if(it != mapping.end())
00168 mapping.erase(it);
00169
else
00170
PLWARNING(
"In RealMapping::removeMapping mapping not removed: does not exist.");
00171 }
00172
00173 void removeMapping(
real x)
00174 {
00175 mapping_t::iterator it= mapping.lower_bound(
RealRange(
'[',
x,
x,
']'));
00176
if(it != mapping.end() && it->first.contains(
x))
00177 mapping.erase(it);
00178
else
00179
PLWARNING(
"In RealMapping::removeMapping mapping not removed: does not exist.");
00180 }
00181
00182
void addMapping(
const RealRange& range,
real val);
00183
00185 void setMappingForMissing(
real what_missing_mapsto)
00186 { missing_mapsto = what_missing_mapsto; }
00187
00189 void setMappingForOther(
real what_other_mapsto)
00190 { keep_other_as_is=
false; other_mapsto = what_other_mapsto; }
00191
00192 void keepOtherAsIs()
00193 { keep_other_as_is=
true; }
00194
00195
00196
real map(
real val)
const;
00197
00198
00199
int binnumber(
real val)
const;
00200
00201
00202
void transform(
const Vec& v)
const;
00203
00204 pair<RealRange, real> lastMapping()
00205 {
return *(mapping.rbegin()); }
00206
00207
00208
00209
00210
00211
00212 iterator begin()
00213 {
return mapping.begin(); }
00214 const_iterator begin()
const
00215
{
return mapping.begin(); }
00216 iterator end()
00217 {
return mapping.end(); }
00218 const_iterator end()
const
00219
{
return mapping.end(); }
00220 void erase(iterator it)
00221 { mapping.erase(it); }
00222
00223
bool operator==(
const RealMapping& rm)
const;
00224
00225
virtual void print(ostream& out)
const;
00226
virtual void write(ostream& out)
const;
00227
virtual void read(istream& in);
00228
00229
real maxMappedToValue();
00230
00233
Vec getCutPoints() const;
00234
00235 };
00236
00237 DECLARE_OBJECT_PTR(
RealMapping);
00238
00239 }
00240
00241 #endif