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
|
/* -*- mode: c++ -*-
*/
/*
GIFT, a flexible content based image retrieval system.
Copyright (C) 1998, 1999, 2000, 2001, 2002, CUI University of Geneva
Copyright (C) 2003, 2004 Bayreuth University
2005 Bamberg University
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _CPERSISTENTTRANSLATEDINDEXMATRIX
#define _CPERSISTENTTRANSLATEDINDEXMATRIX
#include "libGIFTAcDistanceMatrix/include/uses-declarations.h"
#include "assert.h"
#include <vector>
#include <map>
#include <string>
#include "libMRML/include/GIFTExceptions.h"
#include "libGIFTAcDistanceMatrix/include/CPersistentMatrix.h"
///#include "CIDToMatrixIndex.h"
//defining this activates printouts in this file
#undef CPTIM_DEBUG
#define HERE __FILE__ << ":" << __LINE__ << ":"
template<class TTID,class TTContent>
class CPersistentTranslatedIndexMatrix{
public:
///
typedef map<TTID,TTContent> CIDContentPairList;
///
typedef vector<TTID> CIndexList;
protected:
///
typedef vector<TTContent> CLineVector;
///FIXME: taking a map here is a performance flaw
typedef map<TTID,long> CTranslator;
///
typedef CPersistentMatrix<TTContent> CMatrix;
///
CTranslator mTranslator;
///
CMatrix* mContent;
public:
///
CPersistentTranslatedIndexMatrix();
///
bool create(const char* inFileName,
const CIndexList& inIndexList);
///
bool open(const char* inFileName);
///
bool putLineVector(const TTID& inID,
const CLineVector& inVector)const;
///
bool getLineVector(const TTID& inID,
CLineVector& outVector)const;
///
bool putLineVector(const TTID& inID,
const CIDContentPairList& inList)const;
///
bool getLineVector(const TTID& inI,
CIDContentPairList& outList)const;
///
bool setValue(const TTID& inX1,
const TTID& inX2,
const TTContent& inValue)const;
///
operator bool()const;
///
long size()const;
};
///
template<class TTID,class TTContent>
CPersistentTranslatedIndexMatrix<TTID,TTContent>
::CPersistentTranslatedIndexMatrix():
mContent(0)
{
};
///
template<class TTID,class TTContent>
bool CPersistentTranslatedIndexMatrix<TTID,TTContent>
::create(const char* inFileName,
const CIndexList& inIndexList){
CIndexList lIndexList(inIndexList);
sort(lIndexList.begin(),lIndexList.end());
long lCount=0;
for(typename CIndexList::const_iterator i=lIndexList.begin();
i!=lIndexList.end();
i++,lCount++){
mTranslator.insert(make_pair(*i,lCount));
}
mContent=new CMatrix(lCount,lCount);
if(mContent){
string lStreamName=string(inFileName)+string(".trans");
ofstream lStream(lStreamName.c_str());
if(lStream){
for(typename CTranslator::const_iterator i=mTranslator.begin();
i!=mTranslator.end();
i++){
lStream << i->first
<< " "
<< i->second
<< endl;
}
}
return lStream && mContent->create(inFileName) ;
}
return false;
};
///
template<class TTID,class TTContent>
bool CPersistentTranslatedIndexMatrix<TTID,TTContent>
::open(const char* inFileName){
long lCount=0;
delete mContent;
mTranslator.clear();
string lStreamName=string(inFileName)+string(".trans");
ifstream lStream(lStreamName.c_str());
if(lStream){
cout << HERE
<< "Opened translator stream"
<< endl;
}else{
cout << HERE << "FAILED" << endl;
assert(0);
}
//reading pairs: left side is ID-element in ascii
//right side is index in vector
if(lStream){
while(lStream){
TTID lFirst;
long lSecond;
lStream >> lFirst
>> lSecond;
mTranslator.insert(make_pair(lFirst,lSecond));
}
}
cout << HERE
<< "After reading translator: "
<< this->size()
<< " elements in translator"
<< endl;
mContent=new CMatrix(size(),
size());
mContent->open(inFileName);
assert(mContent->operator bool());
cout << HERE
<< "OPEN3"
<< endl;
return lStream && mContent->operator bool();
}
///
template<class TTID,class TTContent>
bool CPersistentTranslatedIndexMatrix<TTID,TTContent>
::getLineVector(const TTID& inID,
CLineVector& outVector)const{
assert(mContent);
#ifdef CPTIM_DEBUG
cout << "(" << inID << ")" << endl;
#endif
if(mTranslator.find(inID)!=mTranslator.end()){
long lLocalLine=mTranslator.find(inID)->second;
#ifdef CPTIM_DEBUG
cout << "getting Line: "
<< lLocalLine
<< endl;
#endif
return mContent->getLineVector(lLocalLine,outVector);
}else{
cout <<"<<dumping"<<endl;
for(typename CTranslator::const_iterator i=mTranslator.begin();
i!=mTranslator.end();
i++){
cout << i->first << "," << i->second << endl;
}
cout << "dumping>>"<<endl;
assert(!"NotFound");
}
return false;
};
///
template<class TTID,class TTContent>
bool CPersistentTranslatedIndexMatrix<TTID,TTContent>
::getLineVector(const TTID& inI,
CIDContentPairList& outList)const{
assert(mContent);
CLineVector lVector(size());
bool lReturnValue=getLineVector(inI,
lVector);
outList.clear();
try{
for(typename CTranslator::const_iterator i=mTranslator.begin();
i!=mTranslator.end();
i++){
outList.insert(make_pair(i->first,
lVector[i->second]));
}
}catch(...){
return false;
}
return lReturnValue;
};
///
template<class TTID,class TTContent>
bool CPersistentTranslatedIndexMatrix<TTID,TTContent>
::putLineVector(const TTID& inID,
const CLineVector& inVector)const{
if(mTranslator.find(inID)!=mTranslator.end()){
long lLine=mTranslator.find(inID)->second;
return mContent->putLineVector(lLine,inVector);
}else{
assert(!"There is no ID of this kind");
}
return 0;
};
///
template<class TTID,class TTContent>
bool CPersistentTranslatedIndexMatrix<TTID,TTContent>
::putLineVector(const TTID& inID,
const CIDContentPairList& inList)const{
CLineVector lVector(size());
bool lReturnValue=getLineVector(inID,
lVector);
cout << "inID" << inID << endl;
assert(lReturnValue);
for(typename CIDContentPairList::const_iterator i=inList.begin();
i!=inList.end();
i++){
typename CTranslator::const_iterator lXPosition=mTranslator.find(i->first);
if(lXPosition!=mTranslator.end()){
lVector[lXPosition->second]=i->second;
}
}
return lReturnValue && putLineVector(inID,
lVector);
};
///
template<class TTID,class TTContent>
long CPersistentTranslatedIndexMatrix<TTID,TTContent>
::size()const{
return mTranslator.size();
}
///
template<class TTID,class TTContent>
CPersistentTranslatedIndexMatrix<TTID,TTContent>::operator bool()const{
return mContent && bool(*mContent);
}
#endif
|