1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/* lb-blackbox-data.h
* Copyright (C) 2005 Pascal Giorgi
*
* Written by Pascal Giorgi <pgiorgi@uwaterloo.ca>
*
* ========LICENCE========
* This file is part of the library LinBox.
*
* LinBox is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
du -h tes *
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* ========LICENCE========
*/
#ifndef __LINBOX_lb_blackbox_data_H
#define __LINBOX_lb_blackbox_data_H
#include "linbox/matrix/sparse-matrix.h"
#include "linbox/util/matrix-stream.h"
#include <map>
#include <utility>
#include <lb-utilities.h>
#include <lb-blackbox-collection.h>
#include <lb-blackbox-abstract.h>
#include <lb-domain-function.h>
#include <lb-garbage.h>
extern BlackboxTable blackbox_hashtable;
/******************************************************
* Functor to determine domain in Abstract Blackboxes *
******************************************************/
template<template<class Domain> class Blackbox, class Functor>
class BlackboxSpecFunctor{
const Functor & fct;
void *ptr;
public:
BlackboxSpecFunctor(const Functor &f, void *p) : fct(f), ptr(p) {}
template<class Domain, class Result>
void operator()(Result &res, Domain *d) const
{
Blackbox<Domain> * b= static_cast<Blackbox<Domain>*> (ptr);
fct(res, b);
}
};
/********************************************
* Factory to construct Abstract Blackboxes *
********************************************/
class Blackbox_Factory {
public:
typedef BlackboxAbstract* (*createBlackbox_1_CallBack)(const DomainKey &, size_t, size_t, const char*);
typedef BlackboxAbstract* (*createBlackbox_2_CallBack)(const DomainKey &, std::istream&, const char*);
typedef std::map<const char*, std::pair<createBlackbox_1_CallBack, createBlackbox_2_CallBack > , ltstr> CallBackMap;
bool add(const char *name, std::pair<createBlackbox_1_CallBack, createBlackbox_2_CallBack> createD){
return _callback.insert(CallBackMap::value_type(name, createD)).second;
}
bool remove(const char *name){return _callback.erase(name) == 1;}
BlackboxAbstract* create(const char *name, const DomainKey &k, size_t m, size_t n){
CallBackMap::iterator it = _callback.find(name);
if (it != _callback.end()){
return it->second.first(k, m, m,name);
}
else {
std::string mes("LinBox ERROR: you are trying to construct a non defined blackbox << ");
mes+= std::string(name);
mes+= std::string(" >>\n");
throw lb_runtime_error(mes.c_str());// throw an exception
}
}
BlackboxAbstract* create(const char *name, const DomainKey &k, std::istream &is){
CallBackMap::iterator it = _callback.find(name);
if (it != _callback.end()){ return it->second.second(k, is, name); }
else {
std::string mes("LinBox ERROR: you are trying to construct a non defined blackbox << ");
mes+= std::string(name);
mes+= std::string(" >>\n");
throw lb_runtime_error(mes.c_str());// throw an exception
}
}
size_t size() { return _callback.size(); }
private:
CallBackMap _callback;
};
/******************************
* Functor to copy Blackboxes *
******************************/
class CopyBlackboxFunctor {
public:
template<class Blackbox>
void operator()(void *&res, Blackbox *B) const
{
res= new Blackbox(static_cast<const Blackbox&>(*B));
}
};
/*********************************
* Functors to rebind Blackboxes *
*********************************/
template<template<class T> class Blackbox>
class RebindBlackboxFunctor{
void *&ptr;
public:
RebindBlackboxFunctor(void *&p) : ptr(p) {}
template<class Domain>
void operator()(const DomainKey &key, Domain *D) const
{
RebindBlackboxFunctor fct(ptr);
DomainFunction::call(*D, key, fct);
}
template<class DomainSource, class DomainTarget>
void operator()(DomainSource &res, DomainTarget *D) const
{
Blackbox<DomainSource> *B_source= static_cast<Blackbox<DomainSource> * > (ptr);
Blackbox<DomainTarget> *B_target = NULL; /* was not init't */
// typename Blackbox<DomainSource>::template rebind<DomainTarget>()(*B_target, *B_source, *D);
typename Blackbox<DomainSource>::template rebind<DomainTarget>()(*B_target, *B_source);
delete B_source;
ptr = B_target;
}
};
/************************************************************
* Blackbox Envelope to be compliant with Blackbox Abstract *
************************************************************/
template<template<class Domain> class Blackbox>
class BlackboxEnvelope : public BlackboxAbstract{
protected:
void *ptr;
DomainKey key;
const char* _info;
public:
BlackboxEnvelope(void *p, const DomainKey &k, const char *Info) :
ptr(p), key(k, true), _info(Info)
{}
~BlackboxEnvelope(){}
BlackboxAbstract* clone() const
{
CopyBlackboxFunctor Fct;
void *b;
launch(b, Fct);
return new BlackboxEnvelope<Blackbox>(b, key, _info);
}
void * getPtr() const
{ return ptr;}
virtual const DomainKey& getDomainKey() const
{
return key;
}
LINBOX_VISITABLE();
template<class Functor, class Result>
void launch (Result &res, const Functor &fct) const
{
BlackboxSpecFunctor<Blackbox, Functor> bbs(fct, ptr);
DomainFunction::call(res, key, bbs);
}
const char* info() const
{
std::string msg= "[ LinBox Blackbox (storage = ";
msg+= std::string(_info);
msg+= std::string(", domain = [LinBox Domain (type = ");
msg+= std::string(key.Type());
msg+= std::string(", charact = ");
msg+= std::string(key.Characteristic());
msg+= std::string(")] )]\n");
return msg.c_str();
}
void rebind(const DomainKey &k)
{
RebindBlackboxFunctor<Blackbox> Fct(ptr);
DomainFunction::call(k, key, Fct);
key = k;
key.set_autogc();
}
};
/**********************************
* Functors to construct Blackbox *
**********************************/
template<template<class T> class Blackbox>
class CreateBlackboxFunctor{
size_t &_row;
size_t &_col;
public:
CreateBlackboxFunctor(size_t &m, size_t &n) : _row(m), _col(n) {}
template<class Domain>
void operator()(void *&res, Domain *D) const
{
res = new Blackbox<Domain>(*D, _row, _col);
}
};
template<template<class T> class Blackbox>
class CreateBlackboxFromStreamFunctor{
std::istream ∈
public:
CreateBlackboxFromStreamFunctor(std::istream &i) : in(i) {}
template<class Domain>
void operator()(void *&res, Domain *D) const
{
LinBox::MatrixStream<Domain> ms(*D, in);
res = new Blackbox<Domain>(ms);
}
};
/******************************************************
* Blackbox construction function used in the Factory *
******************************************************/
template<template<class T> class Blackbox>
BlackboxAbstract* constructBlackbox_from_size(const DomainKey &k, size_t m, size_t n, const char* info){
CreateBlackboxFunctor<Blackbox> fct(m,n);
void *bb;
DomainFunction::call(bb, k, fct);
BlackboxEnvelope<Blackbox> *bbe = new BlackboxEnvelope<Blackbox> (bb, k, info);
return bbe;
}
template<template<class T> class Blackbox>
BlackboxAbstract* constructBlackbox_from_stream (const DomainKey &k, std::istream &in, const char *info){
CreateBlackboxFromStreamFunctor<Blackbox> fct(in);
void *bb;
DomainFunction::call(bb, k, fct);
BlackboxEnvelope<Blackbox> *bbe = new BlackboxEnvelope<Blackbox> (bb, k, info);
return bbe;
}
/************************************************************
* Function to add an abstract blackbox in linbox hashtable *
************************************************************/
const BlackboxKey& addBlackbox(BlackboxAbstract * v){
std::pair<BlackboxTable::const_iterator, bool> status;
status = blackbox_hashtable.insert(std::pair<BlackboxKey, BlackboxAbstract*> (BlackboxKey(v), v));
if (status.second)
return status.first->first;
else
throw lb_runtime_error("LinBox ERROR: blackbox creation failed \n");// throw an exception
}
#endif
// Local Variables:
// mode: C++
// tab-width: 4
// indent-tabs-mode: nil
// c-basic-offset: 4
// End:
// vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
|