STK++ 0.9.13
STK_Binary.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 Binary.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 **/
31
36#ifndef STK_BINARY_H
37#define STK_BINARY_H
38
39#include <map>
40
41namespace STK
42{
53
58 template<>
59 struct Arithmetic<Binary> : public std::numeric_limits<Binary>
60 {
62 static inline Binary NA() throw() { return binaryNA_;}
64 static const bool hasNA = true;
68 static inline bool isNA(const Binary& x) throw() { return (x==binaryNA_);}
72 static inline bool isInfinite(const Binary& x) throw() { return false; }
76 static inline bool isFinite(const Binary& x) throw() { return (!isNA(x) && !isInfinite(x));}
77 };
78
82template<>
84{
86 static inline Base::IdType returnType() { return(Base::binary_);}
87};
93inline ostream& operator << (ostream& os, Binary const& value)
94{
95 return Arithmetic<Binary>::isNA(value) ? (os << stringNa)
96 : (os << static_cast<int>(value));
97}
98
105{
106 int res;
107 // try to read an integer
108 if (!(is >> res).fail())
109 {
110 switch (res)
111 {
112 case 0:
113 value = zero_;
114 break;
115 case 1:
116 value = one_;
117 break;
118 default:
119 value = binaryNA_;
120 break;
121 }
122 }
123 else
124 { value = binaryNA_; is.clear(); is.setstate(std::ios::failbit);}
125 return is;
126}
127
135{
136 if (toUpperString(str) == toUpperString(_T("0"))) return zero_;
137 if (toUpperString(str) == toUpperString(_T("1"))) return one_;
138 return binaryNA_;
139}
140
148inline Binary stringToBinary( String const& str, std::map<String, Binary> const& mapping)
149{
150 std::map<String, Binary>::const_iterator it=mapping.find(str);
151 return (it == mapping.end()) ? binaryNA_ : it->second;
152}
153
160inline String binaryToString( Binary const& value, std::ios_base& (*f)(std::ios_base&) = std::dec)
161{
162 if (Arithmetic<Binary>::isNA(value)) return stringNa;
165 return os.str();
166}
167
174inline String binaryToString( Binary const& value, std::map<Binary, String> const& mapping)
175{
176 std::map<Binary, String>::const_iterator it=mapping.find(value);
177 return (it == mapping.end()) ? Arithmetic<String>::NA() : it->second;
178}
179
185template<>
188
194template<>
195inline String typeToString<Binary>( Binary const& t, std::ios_base& (*f)(std::ios_base&))
196{ return binaryToString(t);}
197
198} // namespace STK
199
200#endif /*STK_BINARY_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
Binary stringToType< Binary >(String const &str)
specialization for Binary
Definition STK_Binary.h:186
Binary stringToBinary(String const &str)
Convert a String to a Binary.
Definition STK_Binary.h:134
Binary
STK fundamental type of a binary.
Definition STK_Binary.h:49
String typeToString< Binary >(Binary const &t, std::ios_base &(*f)(std::ios_base &))
specialization for Binary
Definition STK_Binary.h:195
String const & toUpperString(String &s)
convert the characters of the String to upper case
Definition STK_String.h:134
String binaryToString(Binary const &value, std::ios_base &(*f)(std::ios_base &)=std::dec)
Convert a Binary to a String.
Definition STK_Binary.h:160
String stringNa
Representation of a Not Available value.
@ zero_
0 value
Definition STK_Binary.h:49
@ one_
1 value
Definition STK_Binary.h:50
@ binaryNA_
Not Available value.
Definition STK_Binary.h:51
IdType
Id for the Type of a variable.
@ binary_
Binary type {0, 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 Binary NA()
Adding a Non Available (NA) special number.
Definition STK_Binary.h:62
static bool isFinite(const Binary &x)
test if x is finite.
Definition STK_Binary.h:76
static bool isInfinite(const Binary &x)
test if x is infinite.
Definition STK_Binary.h:72
static bool isNA(const Binary &x)
Test if x is a Non Available (NA) special number.
Definition STK_Binary.h:68
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 Type NA()
Adding a Non Available (NA) special number.
static bool isNA(Type const &x)
static Base::IdType returnType()
Definition STK_Binary.h:86
Implementation of the return type.