File: CAcIFFileSystem.cc

package info (click to toggle)
gnuift 0.1.14%2Bds-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,632 kB
  • ctags: 2,973
  • sloc: cpp: 15,867; sh: 8,281; ansic: 1,812; perl: 1,007; php: 651; makefile: 483; lisp: 344
file content (1831 lines) | stat: -rw-r--r-- 46,608 bytes parent folder | download
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
// -*- 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

*/
/***************************************
*
* CAcIFFileSystem - class for the basic access to 
* the inverted file and the generation of the inverted file
*
****************************************
*
* modification history:
* 
* WM 260600 renamed file and class to reflect new architecture
* HM        removed some screen output
* HM 010499 changed to find error
* HM 090399 created the documentation
* WM 10  98 created most of the content
*
****************************************
*
* compiler defines used:
*
* _NO_PRINT_OFFSET_CHECK ?
* _NO_CHECK_OFFSET_FILE  ?
* _NO_CHECK_CONSISTENCY  do NOT check consistency after
*                        generating inverted file
* _NO_FIDPRINT           ?
* _NO_DIDPRINT           ?
* _NO_PRINT_ID           ?
*
* _NO_PRINT_INIT
*
* IGNORE_UNKNOWN_URLS    unknown images are considered to have an empty feature list
*
****************************************/


#include <cstring>
#include <cerrno> //show which error makes reads fail!

#include <unistd.h>     // for getpid
#include "libMRML/include/mrml_const.h" // for parsing
#include <unistd.h>
#include <cstdio>       // for sprintf
#include <sstream>
#include <string>
#include <set> 
#include <algorithm>
#include <functional>

#include "libMRML/include/my_assert.h"

#include "libGIFTAcInvertedFile/include/merge_sort_streams.h"    //newGenerateInvertedFile
#include "libGIFTAcInvertedFile/include/CIFBuilderTriplet.h"     //newGenerateInvertedFile
#include "libGIFTAcInvertedFile/include/CDocumentFrequencyList.h"
#include "libGIFTAcInvertedFile/include/CAcIFFileSystem.h"
#include "libGIFTAcInvertedFile/include/CInvertedFileChunk.h"
#include <iostream>
#include <fstream>
#include <cassert>
#include <cmath>
#include "libGIFTAcInvertedFile/include/CIFListStart.h"
#include "libMRML/include/CXMLElement.h" // constructor
#include "libMRML/include/directory.h"   // the install locations etc. as determined by ./configure
#define _NO_PRINT_OFFSET_CHECK
#define _NO_CHECK_OFFSET_FILE
#define _NO_CHECK_CONSISTENCY
#define _NO_CONSISTENCYPRINT
#define _NO_FIDPRINT
#define _NO_DIDPRINT
#define	_NO_PRINT_ID
#define	_NO_PRINT_INIT

extern class CMutex* gMutex;

/** used for reading the offset file */
typedef struct{
  unsigned int mUInt1;
  unsigned int mUInt2;
} SUIntUInt;

/** used for reading the offset file */
typedef struct{
  unsigned int mUInt1;
  //unsigned int mUInt2;
  streampos mStreampos;
} SUIntStreampos;


/***************************************
*
* ()
*
****************************************
*
* modification history
*
* 
*
****************************************/
/* to test if the inverted file accessor is OK */
bool CAcIFFileSystem::operator()()const{
  /**/gMutex->lock();
  bool lReturnValue(mURL2FTS
		    && mInvertedFile
		    && *mInvertedFile
		    && mOffsetFile
		    && mURL2FTS->operator bool());
  /**/gMutex->unlock();
  return lReturnValue;
};

 
/***************************************
*
* generateInvertedFile - the inverted file is generated here
*
****************************************
*
* modification history
*
* 
*
****************************************/
bool CAcIFFileSystem::generateInvertedFile(){
  /**/gMutex->lock();
  // open the feature description file
  //
  cout << "I want to use/generate the files:"
       << mInvertedFileName << endl
       << mOffsetFileName << endl
       << mURL2FTS->getURLToFeatureFileName() << endl
       << mFeatureDescriptionFileName << endl;

  ofstream lNewInvertedFile(mInvertedFileName.c_str());
  ofstream lNewOffsetFile(mOffsetFileName.c_str());

  /* if one of the files does not open correctly */
  if(!(mURL2FTS->operator bool()
       && 
       mFeatureDescriptionFile
       &&
       lNewInvertedFile
       &&
       lNewOffsetFile)){
      cerr << "I could not open the necessary files for" 
	   << "generating an inverted file";
      /**/gMutex->unlock();
      return false;
  }
  cout << "files successfully opened" 
       << flush
       << endl;
  
  
  bool lError=false;
  //Local: A hash of Inverted File Chunks by the feature ID
  map<TID,CInvertedFileChunk> lInvertedFileHash;
  
  
  /// Meaning additional document information
  CADIHash lADI;
  
  list<CAccessorElement> lAllAccessorElements;

  mURL2FTS->getAllAccessorElements(lAllAccessorElements);

  /* process this for all the images in the file URL to Feature file name */
  for(list<CAccessorElement>::const_iterator i=lAllAccessorElements.begin();
      i!=lAllAccessorElements.end();
      i++){
    
    //This variable is used in the for loop to translate positions in the map into IDs
    int lDocumentID=i->getID();


    cout << endl
	 << "Processing File: "
	 << lDocumentID
	 << flush;
    cout << " "
	 << *i
	 << "..."
	 << flush
	 << endl;
    cout << "h" 
	 << flush
	 << endl;

    /* use the second part of the map as the 
       file name for the next feature file */
    ifstream lFeatureFile(i->getFeatureFileName().c_str());
    lADI[lDocumentID]=CAdditionalDocumentInformation(i->getFeatureFileName().c_str());
    
    unsigned int lNumberOfFeatures=0;
    /* reads the number of features out of the file */
    lFeatureFile.read((char*)&lNumberOfFeatures,
		      sizeof(lNumberOfFeatures));
    
    double lMaxDocumentFrequency=0;
    double lDocumentFrequencySquareSum=0;
    
    //Read features for one image
    if(lFeatureFile)
      {
	/* for each feature in the file */
	for(unsigned int j=0;
	    j<lNumberOfFeatures && lFeatureFile;
	    j++)
	  {
	    /* does this already read in the element from the file ? yes*/
	    struct{
	      TID first;
	      float second;
	    } lInElement;

	    lFeatureFile.read((char*)&lInElement,
			      sizeof(lInElement));


	    CDocumentFrequencyElement lElement(lInElement.first,
					       lInElement.second);
	    
	    // 	    cout << "RSIZE" << sizeof(lInElement) 
	    // 		 << " " << lElement.getID()
	    // 		 << " " << lInElement.first
	    // 		 << " " << lElement.getDocumentFrequency()
	    // 		 << "== 0x" << hex << lInElement.second
	    // 		 << endl;
	    
	    //if this assertion fails, there has been a misunderstanding
	    //about the data format of the feature file 
	    //(padding: where and how?) WM
	    assert(lElement.getDocumentFrequency()>1/1000000000);
	    assert(lElement.getDocumentFrequency()<=1);

	    //Adjust values which depend on the document Frequency
	    //in the Additional Document Information
	    lADI[lDocumentID].
	      adjustDF(lElement.getDocumentFrequency());
	
	    /* this adds a feature into the inverted file list of features */
	    lInvertedFileHash[lElement.getID()].
	      addElement(lDocumentID,
			 lElement.getDocumentFrequency());
	  }
      }

    cout << "...finished" 
	 << flush
	 << endl;


    //one or more errors...
    if(!lFeatureFile){
      lError=true;
      cout << "Error reading file "
	   << i->getFeatureFileName()
	   << "!"
	   << endl;
    }
  }

  // For each Inverted file chunk: this means for each feature 
  // including the documents containing this feature
  // Write it and its offset
  for(map<TID,CInvertedFileChunk>::const_iterator i=lInvertedFileHash.begin();
      i!=lInvertedFileHash.end() && lNewInvertedFile;
      i++)
    {

      assert((*i).second.size());
      
      //find out the ID of the feature
      TID lFeatureID=(*i).first;
      //writing the offset
      {
	// this is the position of the next feature in the inverted file
	streampos lPos(lNewInvertedFile.tellp());
	
	/* updating the offset file */

	writeOffsetFileElement(lFeatureID,
			       lPos,
			       lNewOffsetFile);
			       

	cout << endl
	     << "Writing Chunk for Feature ID "
	     << hex
	     << lFeatureID
	     << ". The Offset is 0x"
	     << hex
	     << lPos
	     << dec
	     << "="
	     << lPos
	     << endl;
      }
      // Writing the next piece of the inverted file
      {
	/* this writes the actual data for the feature, 
	   meaning the list of all the documents containg 
	   this feature */
	bool lSuccessfullyWritten(((*i).second).writeBinary(lNewInvertedFile,
							    lFeatureID,
							    mURL2FTS->size()));
	if(!lSuccessfullyWritten)
	  cout << "!!!!!!!!!!!Error in writing Inverted File at Feature"
	       << lFeatureID
	       << flush
	       << endl;
      }
      
    } /* end of the loop for all the features */
  if(!lNewInvertedFile){
    cout << "ERROR in writing INVERTED FILE" << endl;
  }

  
  lNewInvertedFile.close();
  lNewOffsetFile.close();
 
  {
    //maybe calculate the ADI for each type of feature independently
    //could be useful


    //the value for the document frequency is known only 
    //when one is ready to write. This has just now been the case, 
    //and so...
    
    //For each feature
    for(map<TID,CInvertedFileChunk>::iterator iChunk=lInvertedFileHash.begin();
	iChunk!=lInvertedFileHash.end();
	iChunk++) {
	/* for each document frequency (each document) for this feature */
	for(CInvertedFileChunk::iterator iDocumentFrequency=(*iChunk).second.begin();
	    iDocumentFrequency!=(*iChunk).second.end();
	    iDocumentFrequency++)
	  {
	    
	    //The document for which we want to calculate the sum
	    TID lDID=(*iDocumentFrequency).first;
	    
	    /* if the document frequency does exist bigger than zero */
	    if((*iDocumentFrequency).second!=0)
	      {
       		double lDFLogICF=
		  (*iDocumentFrequency).second
		  *
		  (-log((*iChunk).second.getCollectionFrequency(mURL2FTS->size())));
		
		assert(((*iChunk).second).size()!=0);
		assert((*iChunk).second.getCollectionFrequency(mURL2FTS->size()));

		lADI[lDID].adjustSquareDFLogICF(lDFLogICF*lDFLogICF);
	      } /* end of the if */
	  }
      }
  } /* bracket with no meaning */

  //as last thing give as output the Additional Document Information
  /* writes the additional document information */
  assert(lADI.output());
  cout << "ADI WRITTEN ****************************************"
       << endl
       << endl;



#ifndef _NO_CHECK_CONSISTENCY
  cout << "BEFORE checking inverted file consistency" << flush;
  assert(checkConsistency());
  cout << "AFTER checking inverted file consistency" << endl
       << "The check was successful" << endl;
#endif
  
  /**/gMutex->unlock();
  return(!lError);
}

/** 
    add a pair of FeatureID,Offset to the open offset file 
    (helper function for inverted file construction)
*/
void CAcIFFileSystem::writeOffsetFileElement(TID inFeatureID,
					     streampos inPosition,
					     ostream& inOpenOffsetFile){
  /**/gMutex->lock();/** used for reading the offset file */

  SUIntStreampos lWriter;

  lWriter.mUInt1=inFeatureID;
  lWriter.mStreampos=inPosition;
#ifndef _NO_PRINT_OFFSET_CHECK
  lWriter.mUInt2=int(inPosition);
#endif

//   inOpenOffsetFile.write((char*)&inFeatureID,
// 			 sizeof(TID));
//   inOpenOffsetFile.write((char*)&inPosition,
// 			 sizeof(streampos));
  inOpenOffsetFile.write((char*)&lWriter,
 			 sizeof(lWriter));
  std::cout << "[inFeatureID:" << sizeof(TID) << "/" << inFeatureID
	    << ";inPosition:" << sizeof(streampos) << "/" << inPosition << "==" << int(inPosition)
	    << "]" << sizeof(lWriter) << endl;
  /**/gMutex->unlock();
  
};
/***************************************
*
* newGenerateInvertedFile - the inverted file is generated here
*
****************************************
*
* modification history
*
* 
*
****************************************/
bool CAcIFFileSystem::newGenerateInvertedFile(){
  /**/gMutex->lock();
  // open the feature description file
  //
  cout << "NEWGEN I want to use/generate the files:"
       << mInvertedFileName << endl
       << mOffsetFileName << endl
       << mURL2FTS->getURLToFeatureFileName() << endl
       << mFeatureDescriptionFileName << endl;

  ofstream lNewInvertedFile(mInvertedFileName.c_str());
  ofstream lNewOffsetFile(mOffsetFileName.c_str());

  /* if one of the files does not open correctly */
  if(!(mURL2FTS->operator bool()
       && 
       mFeatureDescriptionFile
       &&
       lNewInvertedFile
       &&
       lNewOffsetFile)){
    cerr << "I could not open the necessary files for" 
	 << "generating an inverted file";
    /**/gMutex->unlock();
    return false;
  }
  cout << "files successfully opened" 
       << flush
       << endl;
  
  bool lError=false;
  
  /// Meaning additional document information
  CADIHash lADI;
  
  string lAuxiliaryName1(mTemporaryIndexingFileBase+"gift-auxiliary-1");
  string lAuxiliaryName2(mTemporaryIndexingFileBase+"gift-auxiliary-2");

  ofstream lAuxOutputStream(lAuxiliaryName1.c_str());
  
  list<CAccessorElement> lAllAccessorElements;

  mURL2FTS->getAllAccessorElements(lAllAccessorElements);
  /* process this for all the images in the file URL to Feature file name */
  for(list<CAccessorElement>::const_iterator i=lAllAccessorElements.begin();
      i!=lAllAccessorElements.end();
      i++){
    //This variable is used in the for loop to translate positions in the map into IDs
    int lDocumentID=i->getID();


    cout << endl
	 << "Processing File: "
	 << lDocumentID
	 << flush;
    cout << " "
	 << *i
	 << "..."
	 << flush
	 << endl;
    cout << "h" 
	 << flush
	 << endl;

    /* use the second part of the map as the 
       file name for the next feature file */
    ifstream lFeatureFile(i->getFeatureFileName().c_str());
    lADI[lDocumentID]=CAdditionalDocumentInformation(i->getFeatureFileName().c_str());
    
    unsigned int lNumberOfFeatures=0;
    /* reads the number of features out of the file */
    lFeatureFile.read((char*)&lNumberOfFeatures,
		      sizeof(lNumberOfFeatures));
    
    double lMaxDocumentFrequency=0;
    double lDocumentFrequencySquareSum=0;

    //Read features for one image
    if(lFeatureFile){
      /* for each feature in the file */
      for(unsigned int j=0;
	  j<lNumberOfFeatures && lFeatureFile;
	  j++)
	{
	  /* does this already read in the element from the file ? yes*/
	  struct{
	    TID mFeatureID;
	    float mDocumentFrequency;
	  } lInElement;
	    
	    lFeatureFile.read((char*)&lInElement,
			      sizeof(lInElement));

	    CIFBuilderTriplet lBuilderTriplet(lInElement.mFeatureID,
					      lDocumentID,
					      lInElement.mDocumentFrequency);
	    
	    
	    
	    lAuxOutputStream.write((char*)&lBuilderTriplet,
				   sizeof(lBuilderTriplet));
	}
    }

    cout << "...finished" 
	 << endl
	 << flush;


    //one or more errors...
    if(!lFeatureFile){
      lError=true;
      cout << "Error reading file "
	   << i->getFeatureFileName()
	   << "!"
	   << endl;
    }
  }

  

  if(!lAuxOutputStream){
    cout << "ERROR in writing AUXILIARY FILE" << endl;
  }
  lAuxOutputStream.close();
  

  /* now sort the file of the triplets which we just have created 
     the first name is the name of the auxiliary file which we just
     wrote, the second name is the one of another auxiliary file which
     we need because we do not do any in-place-merging.

     the result of this is supposed to be in gift-auxiliary-1.
  */
  cout << "before mergesort " 
       << flush
       << endl;
    
  string lLastFileUsed(merge_sort_streams<CIFBuilderTriplet>(lAuxiliaryName1.c_str(),
							     lAuxiliaryName2.c_str()));


  cout << "after mergesort. The last file I used was " 
       << lLastFileUsed
       << flush
       << endl;
  
  
  /* now read the file  buildertriplet by buildertriplet.
     each time the ID changes, we are finished with one list */
  ifstream lInAuxiliaryFile(lLastFileUsed.c_str());

  cout << "Opening sorted stream for reading. State (should be '1'): "
       << lInAuxiliaryFile.is_open()
       << endl;
  
  if(lInAuxiliaryFile){
    // the right starting conditions for 
    // the following do-loop.
    CIFBuilderTriplet lOldTriplet(0,0,0);
    CIFBuilderTriplet lTriplet(0,0,0);
    // read the first IFBuilder Triplet from the sorted
    // triplet file
    lInAuxiliaryFile.read((char*)&lTriplet,
			  sizeof(lTriplet));
    do{
      CInvertedFileChunk lChunk;      
      TID lFeatureID;

      //Read now the list for one feature from
      //the auxiliary file
      {    
	do{
	  lOldTriplet=lTriplet;
	  lFeatureID=lOldTriplet.mFeatureID;
	  lChunk.addElement(lOldTriplet.mDocumentID,
			    lOldTriplet.mDocumentFrequency);
	  
	  lInAuxiliaryFile.read((char*)&lTriplet,
				sizeof(lTriplet));
	}while(lInAuxiliaryFile 
	       &&
	       (lTriplet.mFeatureID
		==lOldTriplet.mFeatureID));

	/* special case for very last feature */
	if((!lInAuxiliaryFile) 
	   &&
	   (!lTriplet.isIdentical(lOldTriplet))){
	  lChunk.addElement(lTriplet.mDocumentID,
			    lTriplet.mDocumentFrequency);
	  lFeatureID=lTriplet.mFeatureID;
	}else{
	  lFeatureID=lOldTriplet.mFeatureID;
	}
      }
      // Write this chunk just read as the next piece of the inverted file
      {
	/* 
	   this writes the current position and the current feature ID
	   into the offset file
	*/
	writeOffsetFileElement(lFeatureID,
			       lNewInvertedFile.tellp(), 
			       lNewOffsetFile); 
	/* this writes the actual data for the feature, 
	   meaning the list of all the documents containg 
	   this feature */
	cout //<< endl
	     << "Writing Chunk for Feature ID "
	     << hex
	     << lFeatureID
	     << ". The Offset is 0x"
	     << hex
	     << lNewInvertedFile.tellp()
	     << dec
	     << "="
	     << lNewInvertedFile.tellp()
	     << endl;
	bool lSuccessfullyWritten(lChunk.writeBinary(lNewInvertedFile,
						     lFeatureID,
						     mURL2FTS->size()));
      
	if(!lSuccessfullyWritten)
	  cout << "!!!!!!!!!!!Error in writing Inverted File at Feature"
	       << lFeatureID
	       << flush
	       << endl;
      }
    
    

      /* Now use this data also for creating additional document information
       
	 for each document frequency (each document) for this feature */
      for(CInvertedFileChunk::iterator iDocumentFrequency=lChunk.begin();
	  iDocumentFrequency!=lChunk.end();
	  iDocumentFrequency++){
      
	//The document for which we want to calculate the sum
	TID lDID=(*iDocumentFrequency).first;
      
	/* if the document frequency does exist bigger than zero */
	if((*iDocumentFrequency).second!=0){
	  double lDFLogICF=
	    (*iDocumentFrequency).second
	    *
	    (-log(lChunk.getCollectionFrequency(mURL2FTS->size())));
	  
	  assert((lChunk).size()!=0);
	  assert(lChunk.getCollectionFrequency(mURL2FTS->size()));
	  
	  //Adjust values which depend on the document Frequency
	  //in the Additional Document Information
	  lADI[lDID].
	    adjustDF(iDocumentFrequency->second);
	  lADI[lDID].adjustSquareDFLogICF(lDFLogICF*lDFLogICF);
	} /* end of the if */
      }// for each document in the list for one feature
    }while(lInAuxiliaryFile);//while there is still data in the auxiliary file
  }else{
    cout << "ERROR in reading sorted auxiliary file"
	 << endl;
  }

  if(!lNewInvertedFile){
    cout << "ERROR in writing INVERTED FILE" << endl;
    /**/gMutex->unlock();
    return false;
  }
  
  
  
  lNewInvertedFile.close();
  lNewOffsetFile.close();
  
  
  
  //as last thing give as output the Additional Document Information
  /* writes the additional document information */
  cout << "NEW WRITING ADI ****************************************"
       << endl;
  assert(lADI.output());
  cout << "NEW ADI WRITTEN ****************************************"
       << endl
       << endl;
  
  
  cout << "BEFORE checking inverted file consistency" << flush;
  assert(checkConsistency());
  cout << "AFTER checking inverted file consistency" << endl
       << "The check was successful"
       << endl;
#ifndef _NO_CHECK_CONSISTENCY
#endif
  
  // delete the helper files created by merge_sort_streams
 
  unlink("gift-auxiliary-1");
  unlink("gift-auxiliary-2");
  /**/gMutex->unlock();
  return(!lError);
}




/***************************************
*
* Constructor
*
****************************************
*
* modification history
*
* 
*
****************************************/
CAcIFFileSystem::CAcIFFileSystem(const CXMLElement& inCollectionElement):
  mURL2FTS(new CAcURL2FTS(inCollectionElement)),
  mOffsetFileName(inCollectionElement.stringReadAttribute(mrml_const::cui_base_dir).second
		  +inCollectionElement.stringReadAttribute(mrml_const::cui_offset_file_location).second),
  mInvertedFileName(inCollectionElement.stringReadAttribute(mrml_const::cui_base_dir).second
		    +inCollectionElement.stringReadAttribute(mrml_const::cui_inverted_file_location).second),
  mFeatureDescriptionFileName(inCollectionElement.stringReadAttribute(mrml_const::cui_base_dir).second
			      +inCollectionElement.stringReadAttribute(mrml_const::cui_feature_description_location).second),
  mTemporaryIndexingFileBase(inCollectionElement.stringReadAttribute(mrml_const::cui_base_dir).second),
  mInvertedFile(0),
#ifdef V295
  mInvertedFileBuffer(0),
#else
  mInvertedFileBuffer(""),
#endif
  mMaximumFeatureID(0){
  /**/gMutex->lock();

  if(inCollectionElement.stringReadAttribute(mrml_const::cui_generate_inverted_file).first
     && inCollectionElement.boolReadAttribute(mrml_const::cui_generate_inverted_file).second){
    newGenerateInvertedFile();
  }else{

    
  bool lSuccessfulStart(inCollectionElement.stringReadAttribute(mrml_const::cui_base_dir).first
			&& inCollectionElement.stringReadAttribute(mrml_const::cui_feature_file_location).first
			&& inCollectionElement.stringReadAttribute(mrml_const::cui_offset_file_location).first
			&& inCollectionElement.stringReadAttribute(mrml_const::cui_inverted_file_location).first);
  
  if(lSuccessfulStart 
     && init(inCollectionElement.boolReadAttribute(mrml_const::cui_in_memory).first
	     && inCollectionElement.boolReadAttribute(mrml_const::cui_in_memory).second)){
    cout << "CInvertedFile succuessfully initialised. Parameters:"
	 << mInvertedFileName << endl
	 << mOffsetFileName << endl
	 << mURL2FTS->getURLToFeatureFileName() << endl
	 << mFeatureDescriptionFileName << endl
	 << endl;

#ifndef _NO_PRINT_INIT
    {
      cout << "DIAGNOSE" << flush << endl;
      cout << "The current size of mURLToID is " << mURLToID.size() << endl;
      cout << "All elements:" 
	   << endl;
      int lCount=1;
      for(string_TID_map::const_iterator i=mURLToID.begin();
	  i!=mURLToID.end();
	  i++){
	cout << ","
	     << lCount++
	     << flush;
      }
      lCount=1;
      for(string_TID_map::const_iterator i=mURLToID.begin();
	  i!=mURLToID.end();
	  i++){
	cout << lCount++
	     << ":"
	     << flush
	     << i->first
	     << "->"
	     << i->second
	     << flush
	     << endl 
	     << flush;
      }
    }
#endif


  };
#ifndef _NO_PRINT_INIT
  checkNPrint();
  cout << "Constructor left"
       << endl;
#endif
}
  /**/gMutex->unlock();
}


/***************************************
*
* init - initialization needed for the inverted file
*
****************************************
*
* modification history
*
* 
*
****************************************/
bool CAcIFFileSystem::init(bool inMemory)
{
  /**/gMutex->lock();
  try{
    mMaximumFeatureID=0;
    cout << "Opening _"
	 << mInvertedFileName        
	 << "_";
    if(inMemory){
      ifstream lInvertedFile(mInvertedFileName.c_str());
      if(lInvertedFile){
	
	cout << endl 
	     << "(TRYING TO READ THE WHOLE FILE INTO MEMORY"
	     << endl;
	
	lInvertedFile.seekg(0,ios::end);
	size_t lFileSize=lInvertedFile.tellg();
	
#ifdef V295
	mInvertedFileBuffer=new char[lFileSize];
#else
	mInvertedFileBuffer="x";
	mInvertedFileBuffer.resize(lFileSize);
#endif
	lInvertedFile.seekg(0,ios::beg);
#ifdef V295
	lInvertedFile.read(mInvertedFileBuffer,
			   lFileSize);
#else
	// this is a kludge but speed does not matter here
	for(int i=0;i<lFileSize && lInvertedFile;i++){
	  char lChar;
	  lInvertedFile.get(lChar);
	  mInvertedFileBuffer[i]=lChar;
	}
#endif
	
	mInvertedFile=new istringstream(mInvertedFileBuffer);
	// lFileSize does not need to be given
	cout << "DONE)"
	     << endl;
      }else{
	mInvertedFile=0;
      }
    }

  mInvertedFile=new ifstream(mInvertedFileName.c_str());
  if(!(*mInvertedFile)){
    cout << " ...FAILED:" << strerror(errno) << endl;
  }else{
    cout << " ...success. " << endl;
  }

  cout << "Opening _"
       << mOffsetFileName 
       << "_";
  mOffsetFile.close();
  mOffsetFile.clear();
  mOffsetFile.open(mOffsetFileName.c_str());
  if(!mOffsetFile){
    cout << " FAILED!" << strerror(errno) << endl;
  }else{
    cout << " ...success. " << endl;
  }


  cout << "Opening _"
       << mFeatureDescriptionFileName
       << "_";
  mFeatureDescriptionFile.close();
  mFeatureDescriptionFile.clear();
  mFeatureDescriptionFile.open(mFeatureDescriptionFileName.c_str());
  if(!mFeatureDescriptionFile){
    cout << " ...FAILED!" << strerror(errno) << endl;
  }else{
    cout << " ...success. " << endl;
  }



  bool lRetVal=(mURL2FTS->operator bool()
		//was the superclass well constructed?
		&&
		mFeatureDescriptionFile
		&&
		mOffsetFile
		&&
		*mInvertedFile);

  assert(mURL2FTS->size());

  cout << endl
       << "Current success status"
       << lRetVal
       << endl;

  {
    list<CAccessorElement> lAllAccessorElements;
    
    mURL2FTS->getAllAccessorElements(lAllAccessorElements);

    //for each element in the database
    for(list<CAccessorElement>::const_iterator i=lAllAccessorElements.begin();
	i!=lAllAccessorElements.end();
	i++){
      TID    lID =i->getID();
      string lURL=i->getURL();

      pair<bool,string> lFeatureFileName=mURL2FTS->URLToFFN(lURL);

      assert(lFeatureFileName.first);
      
      mDocumentInformation
	.insert(make_pair(lID,
			  CAdditionalDocumentInformation(lFeatureFileName.second)));
      
      lRetVal = 
	lRetVal && mDocumentInformation[lID].input();
      
    }
  }

  cout << "URLFile " 
       << mURL2FTS->getURLToFeatureFileName() 
       << " processed. Current success status"
       << lRetVal
       << endl;

  /* erase the offset file */
  mIDToOffset.erase(mIDToOffset.begin(),
		    mIDToOffset.end());

  mOffsetFile.seekg(0);


  /* while there is no end of file for the offsets */
  while(mOffsetFile){
    SUIntStreampos lVal;

    /* read a value into the offset file */
    mOffsetFile.read((char*)&lVal,sizeof(lVal));
    

    if(mOffsetFile){


      // actually useful code here! ;-I
      unsigned int lFeatureID=lVal.mUInt1;
      streampos lOffset(lVal.mStreampos);//FIXME streampos
      mMaximumFeatureID=(mMaximumFeatureID < lFeatureID)?lFeatureID:mMaximumFeatureID;
      mIDToOffset[lFeatureID]=lOffset;

#ifndef _NO_PRINT_OFFSET_CHECK
      // hexdump of the characters read
      for(int i=0;i<sizeof(lVal);i++){
	cout << hex << (unsigned int)(((unsigned char*)&lVal)[i]) << "/" << dec << flush;
      }
      

      // value different from check value?
      if((unsigned int)(lVal.mStreampos) != lVal.mUInt2){
	cout << "Size:" << sizeof(lVal) << ":"  << lVal.mUInt1 << ":" << lVal.mStreampos << "!=" << lVal.mUInt2 << "!!!" << long(mOffsetFile.tellg()) << endl;
      }
      assert((unsigned int)(lVal.mStreampos) == lVal.mUInt2);

      // the actual values
      cout << "[read:" << sizeof(lVal) << ":inFeatureID:" << flush
	   << hex
	   << lFeatureID << "==" << dec << lFeatureID
	   << ";inPosition:"
	   << dec
	   << lOffset
	   << "/"
	   << lVal.mStreampos
	   << "]" << long(mOffsetFile.tellg()) << endl;
#endif
      //Reading the offsetfile
      { //move to the right position
	mInvertedFile->seekg(lOffset);
	
	//read the list start chunk 
	//(by constructing an instance of the list start cunk)
	CIFListStart lListStart(*mInvertedFile);
	
	//checking it
	if(lListStart.getFeatureID()!=lFeatureID){
	  cout << "[ERROR" << flush
	       << hex
	       << lFeatureID
	       << ","
	       << hex
	       << lListStart.getFeatureID()
	       << dec
	       << "]" << flush;
	  assert(0);
	}
#ifndef _NO_PRINT_OFFSET_CHECK
	else{
	  cout << "-" << flush;
	}
#endif

	//And setting up the translation table from feature 
	//ID to collection frequency
	mFeatureToCollectionFrequency[lListStart.getFeatureID()]=
	  lListStart.getCollectionFrequency();
	
	/*      assert(mFeatureToCollectionFrequency[lListStart.getFeatureID()]);*/
	
      }
#ifndef _NO_CHECK_OFFSET_FILE
#endif
    } // if the file is valid --> ID is valid!
  }

  cout << "OffsetFile " 
       << mOffsetFileName
       << " processed. Current success status: "
       << lRetVal
       << endl;

  {
    /* as long as there is no end of file */
    while(mFeatureDescriptionFile){
      TID lFeatureID;
      unsigned int lType;

      
      mFeatureDescriptionFile >> lFeatureID 
			      >> lType;

      mFeatureDescription[lFeatureID]=lType;
    } /* end of while */

  }

  cout << "FeatureDescriptionFile " 
       << mFeatureDescriptionFileName
       << " processed: "
       << mFeatureDescription.size()
       << " elements in hash."
       << endl
       << "Initialisation successful? Returning "
       << lRetVal
       << endl;

#ifndef _NO_PRINT_INIT
  checkNPrint();
#endif
  /**/gMutex->unlock();
  return lRetVal;
  }catch(...){
    cerr << "caught here " << endl;
  }

};



/***************************************
*
* FeatureToCollectionFrequency - Returns the collection frequency for a given feature
*
****************************************
*
* modification history
*
* 
*
****************************************/
double CAcIFFileSystem::
FeatureToCollectionFrequency(TFeatureID inFeatureID)const{
  /**/gMutex->lock();

  if((mFeatureToCollectionFrequency.find(inFeatureID))!=
     mFeatureToCollectionFrequency.end())
    {
      assert(0<(*(mFeatureToCollectionFrequency.find(inFeatureID))).second);
      
      double lReturnValue((*(mFeatureToCollectionFrequency.find(inFeatureID))).second);
      /**/gMutex->unlock();
      return lReturnValue;
    }
  else
    {
      /**/gMutex->unlock();
      return 1;
    }
}



/***************************************
*
* DIDToMaxDocumentFrequency - return the maximum document frequency for a given document ID
*
****************************************
*
* modification history
*
* 
*
****************************************/
double CAcIFFileSystem::DIDToMaxDocumentFrequency(TID inID)const{
  /**/gMutex->lock();

  if(mDocumentInformation.find(inID)!=mDocumentInformation.end())
    {
      double lReturnValue((*mDocumentInformation.find(inID)).second.getMaximumDF());
      /**/gMutex->unlock();
      return lReturnValue;
    }
  else
    {
      assert(1==0);
      /**/gMutex->unlock();
      return 1;
    }
};



/***************************************
*
* DIDToDFSquareSum - returns the Document frequency squaresum for a given document ID
*
****************************************
*
* modification history
*
* 
*
****************************************/
/// returns the Document frequency squaresum for a given document ID 
double CAcIFFileSystem::DIDToDFSquareSum(TID inID)const{
  /**/gMutex->lock();
  if(mDocumentInformation.find(inID)!=mDocumentInformation.end()){
    double lReturnValue(mDocumentInformation.find(inID)->second.getDFSquareSum());
      
    /**/gMutex->unlock();
    return lReturnValue;
  }else{
    assert(1+1==0);
    /**/gMutex->unlock();
    return 1;
  }
}



/***************************************
*
* DIDToSquareDFLogICFSum - returns this function for a given document ID
*
****************************************
*
* modification history
*
* 
*
****************************************/
double CAcIFFileSystem::DIDToSquareDFLogICFSum(TID inID)const{
  if(mDocumentInformation.find(inID)!=mDocumentInformation.end()){
    /**/gMutex->lock();
    double lReturnValue((*mDocumentInformation.find(inID))
			.second.getSquareDFLogICFSum());
    /**/gMutex->unlock();
    return lReturnValue;
  }else{
    assert(1+1+1==0);
    /**/gMutex->unlock();
    return 1;
  }
};
  


/***************************************
*
* FeatureToList - returns a list of document frequencies (documents) for a given feature ID
*
****************************************
*
* modification history
*
* 
*
****************************************/
CDocumentFrequencyList* CAcIFFileSystem::
FeatureToList(TFeatureID inFeatureID)const
{
  /**/gMutex->lock();
    CDocumentFrequencyList* lRetVal=0;

    
    {
      mInvertedFile->clear();


      //Find the list of URL-IDs for the feature
      if(mIDToOffset.find(inFeatureID)!=mIDToOffset.end()){
	mInvertedFile->seekg((*mIDToOffset.find(inFeatureID)).second);
      	assert(*mInvertedFile);
	/* if the inverted file has been able to be opened */
	if(*mInvertedFile){

	  //read the beginning chunk of the list;
	  CIFListStart lListStart(*mInvertedFile);

	  //
	  lRetVal=new CDocumentFrequencyList(lListStart.getNumberOfElements());

	  if(lListStart.getFeatureID()!=inFeatureID){
	    cerr << "Feature "
		 << hex
		 << inFeatureID
		 << " not found.";
	    /**/gMutex->unlock();
	    return 0;
	  }
	  
#ifndef _NO_DIDPRINT
	  cout << endl;
#endif


	  lRetVal->readBinary(*mInvertedFile);


#ifndef _NO_DIDPRINT
	  cout << endl;
#endif
	}
      }else{
	cerr << "II:Feature "
	     << hex
	     << inFeatureID
	     << " not found."
	     << mIDToOffset.size()
	     << endl;
	/**/gMutex->unlock();
	return 0;
      }
    }
    /**/gMutex->unlock();
    return lRetVal;
};




/***************************************
*
* findWithinStream - needed for checking the consistency
*
****************************************
*
* modification history
*
* 
*
****************************************/
///needed for checking the consistency
bool CAcIFFileSystem::findWithinStream(TID inFeatureID,
				       TID inDocumentID,
				       double inDocumentFrequency)const{
  /**/gMutex->lock();
  bool lRetVal=false;

  //Find the list of URL-IDs for the feature
  if(mIDToOffset.find(inFeatureID)!=mIDToOffset.end()) /* end points to the element 
							  after the last one for a list */
    {
      /* seekg searches the file position to read (get) */
      mInvertedFile->seekg((*mIDToOffset.find(inFeatureID)).second);
      
      /* if the file is correctly opened */
      if(*mInvertedFile){
	//read the beginning chunk of the list of documents
	//which contain a given feature
	CIFListStart lStart(*mInvertedFile);
	int    lFeatureID          = lStart.getFeatureID();
	double lCollectionFrequency= lStart.getCollectionFrequency();
	int    lEndFor             = lStart.getNumberOfElements();
	
	cout << endl
	     << "Feature:"
	     << inFeatureID
	     << "Size of CIFListStart:"
	     << sizeof(lStart)
	     << endl
	     << "Position in inverted file: "
	     << hex
	     << mInvertedFile->tellg()
	     << endl;

	
	if(lFeatureID!=inFeatureID){
	  cerr << "Feature "
	       << hex
	       << inFeatureID
	       << " not found."
	       << flush;
	  assert(0==1);
	  /**/gMutex->unlock();
	  return 0;
	}else{
	  
	  cout << "ENDFOR=" 
	       << lEndFor
	       << "!"
	       << flush
	       << endl;
	  
	  /* checks the document list for one feature */
	  for(int i=0;
	      i<lEndFor;
	      i++){
	    
	    CDocumentFrequencyElement lDFE(*mInvertedFile);
	    //	    cout << "[DID" ;
	    if((lDFE.getID()==inDocumentID)
	       &&
	       (lDFE.getDocumentFrequency()==inDocumentFrequency)
	       ){
	      lRetVal=true;
//  	      cout << "==" 
// 		   << inDocumentID
// 		   << " "
// 		   << flush;
	    }
// 	    cout << lDFE.getID()
// 		 << ".."
// 		 << lDFE.getDocumentFrequency()
// 		 << "]" 
// 		 << flush;
// 	    if(!(i%20)) cout << endl;
	  }
	}
      }
    }else{
      cout << "OFFSET "
	   << inFeatureID
	   << " NOT FOUND! Size of the container: " 
	   << mIDToOffset.size()	 
	   << "."
	   << endl;
      assert(0);
    }
  cout << "[Document ID:"
       << hex
       << inDocumentID
       << ",FeatureID"
       << inFeatureID
       << ","
       << inDocumentFrequency
       << "]" 
       << dec
       << flush;

  /**/gMutex->unlock();
  return lRetVal;
};




/***************************************
*
* checkConsistency - checks if the data in the inverted file is the same as in the single files
*
****************************************
*
* modification history
*
* 
*
****************************************/
bool CAcIFFileSystem::checkConsistency()
{
  /**/gMutex->lock();
#ifndef _NO_CONSISTENCYPRINT

  cout << "I am now checking the consistency between" << endl
       << "Feature files and the Inverted File"
       << endl
       << "Initialising...";
  
  
  init(false);

  cout << "...done";
  
  bool lRetVal=true;
  
  //for each image (URL)
  for(string_string_map::const_iterator i=mURL2FTS->mURLToFFN.begin();
      i!=mURL2FTS->mURLToFFN.end();
      i++)
    {
      CDocumentFrequencyList* lFeatures=URLToFeatureList((*i).first);
      
      cout << "FeatureFilename:-" 
	   << (*i).first
	   << "-"
	   << endl
	   << flush;
      
      assert(lFeatures);
      
      TID lDocumentID=(*mURL2FTS->mURLToID.find((*i).first)).second;
      
      /* for every feature of one image */
      for(CDocumentFrequencyList::const_iterator j=lFeatures->begin();
	  j!=lFeatures->end();
	  j++)
	{
	  /* looks if it really in the inverted file */
	  lRetVal &= findWithinStream((*j).getID(),
				      lDocumentID,
				      (*j).getDocumentFrequency());
	  assert(lRetVal);
	} /* end for each feature of the image */
    } /* end for each image */
  /**/gMutex->unlock();
  return lRetVal;
#else
  /**/gMutex->unlock();
  return true;
#endif
}




/***************************************
*
* URLToFeatureList -  
* returns the feature list (with document IDs) for a given URL
*
****************************************
*
* modification history
*
* 
*
****************************************/
CDocumentFrequencyList* 
CAcIFFileSystem::URLToFeatureList(string inURL)const
{
  /**/gMutex->lock();

#ifdef PRINT_ADI
  cout <<inURL
       << "(ADI:" ;
#endif

  pair<bool,TID> lID=URLToID(inURL);

  if(!lID.first){//i.e. the URL is not part of the collection

#ifdef IGNORE_UNKNOWN_URLS
    CDocumentFrequencyList* lReturnValue(0);
#else
    pid_t lPID= getpid();
    
    char lFeatureFileName[30];
    sprintf(lFeatureFileName,"/tmp/testFTS-%d.fts",int(lPID));

    system(string(string(__PERL_LOCATION__)+" "+string(__EXECBINDIR__)+"/gift-url-to-fts.pl "+inURL+" "+lFeatureFileName).c_str());
    
    CDocumentFrequencyList* lReturnValue(this->getFeatureFile(lFeatureFileName));
 
#endif
    if(!lReturnValue){
      lReturnValue=new CDocumentFrequencyList();
    }
    /**/gMutex->unlock();
    return lReturnValue;
    // {
    //       cout << endl << "this= " << this << endl;
    //       checkNPrint();
    //       cout << "could not find ID for URL "
    // 	   << inURL << endl;
    //       cout << "The current size of mURLToID is " << mURLToID.size() << endl;
    //       cout << "All elements:" 
    // 	   << endl;
    //       int lCount=1;
    //       for(string_TID_map::const_iterator i=mURLToID.begin();
    // 	  i!=mURLToID.end();
    // 	  i++){
    // 	cout << ","
    // 	     << lCount++
    // 	     << flush;
    //       }
    //       lCount=1;
    //       for(string_TID_map::const_iterator i=mURLToID.begin();
    // 	  i!=mURLToID.end();
    // 	  i++){
    // cout << lCount++
    // 	     << ":"
    // 	     << flush
    // 	     << i->first
    // 	     << "->"
    // 	     << i->second
    // 	     << flush
    // 	     << endl 
    // 	     << flush;
    //       }
    //     }
  }
  my_assert(lID.first,inURL.c_str());   

  /**/gMutex->unlock();
  return DIDToFeatureList(lID.second);
};



CDocumentFrequencyList* CAcIFFileSystem::getFeatureFile(string inFileName)const{
  /**/gMutex->lock();
  CDocumentFrequencyList* lRetVal(0);
  /* if the filename has a size bigger than one, meaning it is defined */
  if(inFileName.size())
    {
      /* a file with the given file name */
      ifstream lFile(inFileName.c_str());
      unsigned int lNumberOfFeatures(0);
      if(lFile){
	lFile.read((char*)&lNumberOfFeatures,
		   sizeof(lNumberOfFeatures));
      
	if(lFile && (lRetVal=new CDocumentFrequencyList(lNumberOfFeatures))){
	
	  lRetVal->readBinary(lFile);
	  
	}
      }
    } /* end of if the URL was proper */
  /**/gMutex->unlock();
  return lRetVal;
}

/***************************************
*
* DIDToFeatureList - returns the feature list for a special image with a document ID
*
****************************************
*
* modification history
*
* 
*
****************************************/
CDocumentFrequencyList* 
CAcIFFileSystem::DIDToFeatureList(TID inDID)const{
  /**/gMutex->lock();

#ifdef PRINT_ADI
  cout <<inURL
       << "(ADI:" ;
#endif

  CADIHash::const_iterator iADI=mDocumentInformation.find(inDID);

  assert(iADI!=mDocumentInformation.end());

#ifdef PRINT_ADI
  iADI->second.output(cout);

  cout << ":ADI)"
       << endl;
#endif


  CDocumentFrequencyList* lRetVal=0;
  
  /* tests if URL is found */
  pair<bool,string> lFeatureFileName=mURL2FTS->IDToFFN(inDID);
  if(lFeatureFileName.first){
      
    lRetVal=this->getFeatureFile(lFeatureFileName.second);
    

  } /* end of the if statement */

  if(!lRetVal){
    lRetVal=new CDocumentFrequencyList(0);
  }
  /**/gMutex->unlock();
  return lRetVal;
};



 
/***************************************
*
* IDToURL - return the URL for an image for a given image ID
*
****************************************
*
* modification history
*
* 
*
****************************************/
string CAcIFFileSystem::IDToURL(TID inID)const
{
  /**/gMutex->lock();

  pair<bool,CAccessorElement> lElement=mURL2FTS->IDToAccessorElement(inID);
  
  if(lElement.first){
    /**/gMutex->unlock();
    return lElement.second.getURL();
  } else {
    cerr << "Error in Conversion from ID "
	 << inID 
	 << " to URL."
	 << endl;
    /**/gMutex->unlock();
    return mrml_const::error;
  }
}


/***************************************
*
* getFeatureDescription - returns the feature description for a given feature ID
*
****************************************
*
* modification history
*
* 
*
****************************************/
unsigned int CAcIFFileSystem::getFeatureDescription(TID inID)const{
  /**/gMutex->lock();
    
  if(mFeatureDescription.find(inID)!=mFeatureDescription.end()){
    unsigned int lReturnValue((*mFeatureDescription.find(inID)).second);
    /**/gMutex->unlock();  
    return lReturnValue;
  }else{
    cout << "[UnknownFeature: "
	 << inID
	 << "]"
	 << flush;

    /** debugging code */
    cout << mFeatureDescription.size() << flush;
    /** /debugging code */

    /**/gMutex->unlock();
    return 0;
  }
}



/***************************************
*
* Destructor
*
****************************************
*
* modification history
*
* 
*
****************************************/
CAcIFFileSystem::~CAcIFFileSystem(){
  cout << "CAcIFFileSystem::~CAcIFFileSystem() called "
       << endl
       << flush;
  /**/gMutex->lock();
  /**/gMutex->unlock();
};

TID CAcIFFileSystem::getMaximumFeatureID()const{
  return mMaximumFeatureID;
};

/** 
    Getting a list of all features contained in this
*/
list<TID>* CAcIFFileSystem::getAllFeatureIDs()const{
  /**/gMutex->lock();
  list<TID>* lReturnValue=new list<TID>();

  if(lReturnValue){
    for(CIDToOffset::const_iterator i=mIDToOffset.begin();
	i!=mIDToOffset.end();
	i++){
      lReturnValue->push_back(i->first);
    }
  }
  lReturnValue->sort();

  /**/gMutex->unlock();
  return lReturnValue;
}

/** List of the IDs of all documents present in the inverted file */
void CAcIFFileSystem::getAllIDs(list<TID>& outIDList)const{
  /**/gMutex->lock();

  mURL2FTS->getAllIDs(outIDList);
  /**/gMutex->unlock();
};
/** List of triplets (ID,imageURL,thumbnailURL) of all
    the documents present in the inverted file */
void CAcIFFileSystem::getAllAccessorElements(list<CAccessorElement>& outAccessorElementList)const{
  /**/gMutex->lock();
  mURL2FTS->getAllAccessorElements(outAccessorElementList);
  /**/gMutex->unlock();
};
/** get a given number of random AccessorElement's 
    @param inoutResultList the list which will contain the result
    @param inSize          the desired size of the inoutResultList
*/
void CAcIFFileSystem::getRandomIDs(list<TID>& outRandomIDList,
				   list<TID>::size_type inSize)const{
  /**/gMutex->lock();
   mURL2FTS->getRandomIDs(outRandomIDList,
			  inSize);
  /**/gMutex->unlock();
};
/** For drawing random sets. Why is this part of an CAccessorImplementation?
    The way the accessor is organised might influence the way
    random sets can be drawn. At present everything happens in
    RAM, but we do not want to be fixed on that.
    
      @param inoutResultList the list which will contain the result
      @param inSize          the desired size of the inoutResultList
*/
void CAcIFFileSystem::getRandomAccessorElements(list<CAccessorElement>& outResult,
						list<CAccessorElement>::size_type inSize)const{
  /**/gMutex->lock();
  mURL2FTS->getRandomAccessorElements(outResult,
				      inSize);
  /**/gMutex->unlock();
};
/**
 *
 * Translate a DocumentID to an accessor Element
 *
 */
pair<bool,CAccessorElement> CAcIFFileSystem::IDToAccessorElement(TID inID)const{
  /**/gMutex->lock();
  pair<bool,CAccessorElement> lReturnValue(mURL2FTS->IDToAccessorElement(inID));
  /**/gMutex->unlock();
  return lReturnValue;
};
/** The number of images in this accessor */
int CAcIFFileSystem::size()const{
  /**/gMutex->lock();
  int lReturnValue(mURL2FTS->size());
  /**/gMutex->unlock();
  return lReturnValue;
};
/** The number of images in this accessor */
pair<bool,TID> CAcIFFileSystem::URLToID(const string& inURL)const{
  /**/gMutex->lock();
  pair<bool,TID> lReturnValue(mURL2FTS->URLToID(inURL));
  /**/gMutex->unlock();
  return lReturnValue;
};

CAcIFFileSystem::operator bool() const{
  /**/gMutex->lock();
  bool lReturnValue(this->operator()());
  /**/gMutex->unlock();
  return lReturnValue;
}