STK++ 0.9.13
STK_Sign.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2007-2015 Serge Iovleff
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: Base
27 * Purpose: Define the fundamental type Sign.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 **/
31
36#ifndef STK_SIGN_H
37#define STK_SIGN_H
38
39#include <map>
40
41namespace STK
42{
53
54// forward declaration
55template<typename Type> struct Arithmetic;
56template<typename Type> struct IdTypeImpl;
57
63template<>
64struct Arithmetic<Sign>: public std::numeric_limits<Sign>
65{
67 static Sign NA() throw() { return signNA_;}
69 static const bool hasNA = true;
73 static bool isNA(const Sign& x) throw() { return (x==signNA_);}
77 static bool isInfinite(const Sign& x) throw() { return false; }
81 static bool isFinite(const Sign& x) throw() { return (!isNA(x) && !isInfinite(x));}
82 };
83
89template<>
91{
93 static Base::IdType returnType() { return(Base::sign_);}
94};
95
101inline ostream& operator << (ostream& os, const Sign& value)
102{ return Arithmetic<Sign>::isNA(value)
104}
105
112{
113 // get current file position
114 int res;
115 // try to read an integer
116 if (!(is >> res).fail())
117 {
118 switch (res)
119 {
120 case 1:
121 value = positive_;
122 break;
123 case -1:
124 value = negative_;
125 break;
126 default:
127 value = signNA_;
128 break;
129 }
130 }
131 else
132 { value = signNA_; is.clear(); is.setstate(std::ios::failbit);}
133 return is;
134}
135
143{
144 if (toUpperString(str) == toUpperString(_T("-1"))) return negative_;
145 if (toUpperString(str) == toUpperString(_T("1"))) return positive_;
146 return signNA_;
147}
148
156inline Sign stringToSign( String const& str, std::map<String, Sign> const& mapping)
157{
158 std::map<String, Sign>::const_iterator it=mapping.find(str);
159 if (it == mapping.end()) return signNA_;
160 return it->second;
161}
162
169inline String signToString( Sign const& value, std::ios_base& (*f)(std::ios_base&) = std::dec)
170{
171 if (Arithmetic<Sign>::isNA(value)) return stringNa;
174 return os.str();
175}
176
183inline String signToString( Sign const& value, std::map<Sign, String> mapping)
184{ return mapping.find(value)->second;}
185
191template<>
193{ return stringToSign(str);}
194
200template<>
201inline String typeToString<Sign>( Sign const& t, std::ios_base& (*f)(std::ios_base&))
202{ return signToString(t);}
203
204
205} // namespace STK
206
207#endif /*STK_SIGN_H*/
#define _T(x)
Let x unmodified.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
ostream & operator<<(ostream &s, const CAllocator< Type, SizeRows_, SizeCols_, Orient_ > &V)
output stream for CAllocator.
Definition STK_CArray.h:221
String typeToString< Sign >(Sign const &t, std::ios_base &(*f)(std::ios_base &))
specialization for Sign
Definition STK_Sign.h:201
String signToString(Sign const &value, std::ios_base &(*f)(std::ios_base &)=std::dec)
Convert a Sign to a String.
Definition STK_Sign.h:169
Sign
STK fundamental type of a sign.
Definition STK_Sign.h:49
String const & toUpperString(String &s)
convert the characters of the String to upper case
Definition STK_String.h:134
Sign stringToSign(String const &str)
Convert a String to a Sign.
Definition STK_Sign.h:142
String stringNa
Representation of a Not Available value.
Sign stringToType< Sign >(String const &str)
specialization for Sign
Definition STK_Sign.h:192
@ signNA_
Not Available value.
Definition STK_Sign.h:51
@ positive_
positive sign
Definition STK_Sign.h:50
@ negative_
negative sign
Definition STK_Sign.h:49
IdType
Id for the Type of a variable.
@ sign_
Sign type {-1, 1}.
std::basic_string< Char > String
STK fundamental type of a String.
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
std::basic_ostringstream< Char > ostringstream
ostringstream (ostringstream) for Char
Definition STK_Stream.h:65
std::basic_istream< Char > istream
istream for Char
Definition STK_Stream.h:55
The namespace STK is the main domain space of the Statistical ToolKit project.
istream & operator>>(istream &is, Binary &value)
Overloading of the istream >> for the type Binary.
Definition STK_Binary.h:104
static Sign NA()
Adding a Non Available (NA) special number.
Definition STK_Sign.h:67
static bool isNA(const Sign &x)
Test if x is a Non Available (NA) special number.
Definition STK_Sign.h:73
static bool isInfinite(const Sign &x)
Test if x is infinite.
Definition STK_Sign.h:77
static bool isFinite(const Sign &x)
Test if x is finite.
Definition STK_Sign.h:81
Arithmetic properties of STK fundamental types.
static bool isInfinite(Type const &x)
static const bool hasNA
True if the type has a representation for a "Not Available."
static bool isNA(Type const &x)
static Base::IdType returnType()
Give the IdType of the type Sign.
Definition STK_Sign.h:93
Implementation of the return type.