00001 #ifndef DEFAULTREPRESENTATION
00002 #define DEFAULTREPRESENTATION
00003
00004 #pragma warning( disable : 4786 )
00005
00006 #include "GInvertedList.h"
00007 #include <string>
00008 #include <cstdlib>
00009 #include <assert.h>
00010
00011
00012 namespace groupindex
00013 {
00014
00018 template <
00019 class Set,
00020 class Group,
00021 class GList = GInvertedList<Group>,
00022 class GroupContainers = default_group_container<Group, GList>,
00023 class SetContainers = default_set_container<Set>
00024 >
00025
00026 class DefaultRepresentation
00027
00028 {
00029
00030 protected:
00031
00036 Set* gListRepresentatives;
00037
00039 long gList_count;
00040
00042 long expected_size;
00043
00047 long increment_size;
00048
00050 long current_size;
00051
00053 long filecounter;
00054
00055
00057 void expandGLists();
00058
00060 GList** gLists;
00061
00064 void insertGList(const Set& r, long pos);
00065
00073 virtual void writeToFile(std::ofstream& ofs);
00074
00075 void initFileRead(std::fstream& ifs);
00076
00077
00078 std::fstream* m_heapStream;
00079
00080 public:
00081
00088 class iterator
00089 {
00090
00091 protected:
00092
00093 Set* pos;
00094 Representaion* rep;
00095
00096 public:
00097
00098 std::pair<Set,GList*> operator*()
00099 {
00100 return std::pair<Set,GList*>(*pos, rep->gLists[*pos]);
00101 }
00102
00103 iterator& operator++()
00104 {
00105 pos++;
00106 return *this;
00107 }
00108
00109 iterator& operator++(int)
00110 {
00111 pos++;
00112 return *this;
00113 }
00114
00115 bool operator==(const iterator& cmp) const
00116 {
00117 return (rep==cmp.rep && pos==cmp.pos);
00118 }
00119
00120 iterator& operator=(const iterator& copy_from)
00121 {
00122 rep = copy_from.rep;
00123 pos = copy_from.pos;
00124 }
00125
00126 iterator()
00127 {
00128 rep = NULL;
00129 pos = NULL;
00130 }
00131
00132 iterator(Representation* r)
00133 {
00134 rep = r;
00135 pos = NULL;
00136 }
00137
00138 iterator(Representation* r, Set* p)
00139 {
00140 rep = r;
00141 pos = p;
00142 }
00143
00144 iterator(const iterator& copy_from)
00145 {
00146 rep = copy_from.rep;
00147 pos = copy_from.pos;
00148 }
00149
00150 };
00151
00152 iterator begin();
00153
00154 iterator end();
00155
00156
00158 DefaultRepresentation(long i_expected_size, long i_increment_size);
00159
00161 DefaultRepresentation();
00162
00163 GList* operator[](int index);
00164
00174 GList* operator[](const Set& r);
00175
00178 Set getRepresentative(long i);
00179
00181
00185 virtual void insert(Set m, int i);
00186
00187
00194 void remove(int fileindex);
00195
00205 virtual void init();
00206
00210
00211 GList* operator[](const int& i) const;
00212
00222 long getGListIndex( Set& r);
00223
00226 long size() const;
00227
00228
00230 ~DefaultRepresentation();
00231
00237 void clear();
00238
00241 void writeStatistics(FILE* stream);
00242
00243 friend std::ofstream& operator<< ANSI_BRACES (std::ofstream& ofs, DefaultRepresentation& dr);
00244
00245 friend std::fstream& operator>>(std::fstream& ifs, DefaultRepresentation& dr);
00246
00247
00248 void setHeapStream(std::fstream& hps);
00249 std::fstream& getHeapStream();
00250 };
00251
00252
00253
00254 template <
00255 class Set,
00256 class Group,
00257 class GList,
00258 class GroupContainers,
00259 class SetContainers
00260 >
00261
00262 void DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::expandGLists()
00263
00264 {
00265
00266 current_size += increment_size;
00267 GList** new_gLists = new GList*[current_size];
00268 Set* new_gListRepresentatives = new Set[current_size];
00269
00270 for (long i=0; i<gList_count; i++)
00271 {
00272 new_gLists[i] = gLists[i];
00273 new_gListRepresentatives[i] = gListRepresentatives[i];
00274 }
00275
00276
00277
00278 delete[] gLists;
00279 delete[] gListRepresentatives;
00280
00281 gLists = new_gLists;
00282 gListRepresentatives = new_gListRepresentatives;
00283
00284 };
00285
00286
00287
00288 template <
00289 class Set,
00290 class Group,
00291 class GList,
00292 class GroupContainers,
00293 class SetContainers
00294 >
00295
00296 std::ofstream& operator<<(std::ofstream& ofs, DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>& dr)
00297
00298 {
00299 dr.writeToFile(ofs);
00300 return ofs;
00301 }
00302
00303
00304
00305 template <
00306 class Set,
00307 class Group,
00308 class GList,
00309 class GroupContainers,
00310 class SetContainers
00311 >
00312
00313 std::fstream& operator>>(std::fstream& ifs, DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>& dr)
00314
00315 {
00316 dr.initFileRead(ifs);
00317 return ifs;
00318 }
00319
00320
00321
00322 template <
00323 class Set,
00324 class Group,
00325 class GList,
00326 class GroupContainers,
00327 class SetContainers
00328 >
00329
00330 void DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::writeStatistics(FILE* stream)
00331
00332 {
00333 unsigned long number_of_gLists = gList_count;
00334 unsigned long max_size = 0;
00335 unsigned long min_size = 0xffffffff;
00336 unsigned long total_size = 0;
00337 int glist_insertion_fails = 0;
00338 int valid_list_count = 0;
00339
00340 for (long i=0; i<gList_count; i++)
00341 {
00342 long size = gLists[i]->size();
00343 if (size>0)
00344 {
00345 total_size += size;
00346 if (size>max_size)
00347 max_size = size;
00348 if (size<min_size)
00349 min_size = size;
00350
00351 valid_list_count++;
00352 }
00353 #ifdef VERBOSE_STATISTICS
00354 fprintf(stream,"%d %d\n",i,size);
00355
00356 #endif
00357 }
00358
00359 fprintf(stream,"Anzahl G-invertierter Listen: %d \n",number_of_gLists);
00360 fprintf(stream,"Einträge in größter Liste: %d \n",max_size);
00361 fprintf(stream,"Einträge in kleinster Liste: %d \n",min_size);
00362 fprintf(stream,"Einträge insgesamt: %d \n",total_size);
00363 fprintf(stream,"Einträge durchschnittlich: %f \n",(double)total_size/(double)valid_list_count);
00364 fprintf(stream,"glist_insertion_fails: %d \n",glist_insertion_fails);
00365
00366 }
00367
00368
00369
00370 template <
00371 class Set,
00372 class Group,
00373 class GList,
00374 class GroupContainers,
00375 class SetContainers
00376 >
00377
00378 GList* DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::operator[](int index)
00379
00380 {
00381 return gLists[index];
00382 }
00383
00384
00385
00386 template <
00387 class Set,
00388 class Group,
00389 class GList,
00390 class GroupContainers,
00391 class SetContainers
00392 >
00393
00394 void DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::clear()
00395
00396 {
00397
00398 if (gListRepresentatives!=NULL)
00399 delete[] gListRepresentatives;
00400
00401 for (int i=0; i<gList_count; i++)
00402 delete gLists[i];
00403
00404 if (gLists!=NULL)
00405 delete[] gLists;
00406
00407 gLists = NULL;
00408 gListRepresentatives = NULL;
00409
00410 current_size = 0;
00411
00412 gList_count = 0;
00413
00414 filecounter = 0;
00415
00416 }
00417
00418
00419
00420 template <
00421 class Set,
00422 class Group,
00423 class GList,
00424 class GroupContainers,
00425 class SetContainers
00426 >
00428
00429 DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::DefaultRepresentation(long i_expected_size, long i_increment_size)
00430
00431 {
00432
00433 expected_size = i_expected_size;
00434 increment_size = i_increment_size;
00435
00436
00437 gLists = NULL;
00438 gListRepresentatives = NULL;
00439
00440 current_size = 0;
00441
00442 gList_count = 0;
00443
00444 filecounter = 0;
00445
00446 };
00447
00448
00449
00450 template <
00451 class Set,
00452 class Group,
00453 class GList,
00454 class GroupContainers,
00455 class SetContainers
00456 >
00457
00458 Set DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::getRepresentative(long i)
00459
00460 {
00461
00462 if (gListRepresentatives!=NULL)
00463 return gListRepresentatives[i];
00464 else
00465 throw IndexingException(IndexingException::INDEX_OUT_OF_BOUNDS);
00466
00467 }
00468
00469
00470
00471
00472 template <
00473 class Set,
00474 class Group,
00475 class GList,
00476 class GroupContainers,
00477 class SetContainers
00478 >
00479
00480 DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::DefaultRepresentation()
00481
00482 {
00483 expected_size = 1000;
00484 increment_size = 100;
00485 gLists = NULL;
00486 gListRepresentatives = NULL;
00487 current_size = 0;
00488 gList_count = 0;
00489 filecounter = 0;
00490 };
00491
00492
00493
00494 template <
00495 class Set,
00496 class Group,
00497 class GList,
00498 class GroupContainers,
00499 class SetContainers
00500 >
00501
00502 GList* DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::operator[](const Set& r)
00503
00504 {
00505
00506 if (gList_count==0)
00507 {
00508 return NULL;
00509 }
00510
00511 long left = 0;
00512 long right = gList_count-1;
00513 bool found = false;
00514 int mid = 0;
00515
00516
00517
00518 while (left<=right && !found)
00519 {
00520
00521 mid = (left+right)/2;
00522
00523 Set cmp = gListRepresentatives[mid];
00524 if (r<cmp)
00525 right = mid-1;
00526 else if (r==cmp)
00527 found = true;
00528 else
00529 left = mid+1;
00530 }
00531
00532 if (found)
00533 return gLists[mid];
00534 else
00535 return NULL;
00536 };
00537
00538
00539
00540 template <
00541 class Set,
00542 class Group,
00543 class GList,
00544 class GroupContainers,
00545 class SetContainers
00546 >
00547
00548 void DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::insert(Set m, int i)
00549
00550 {
00551
00552 typename SetContainers::representation_container r_m;
00553 typename GroupContainers::stabilizer_container stab;
00554
00555 getRepresentation(m, stab, r_m);
00556
00557
00558
00559
00560
00561
00562
00563
00564 typename SetContainers::representation_container::iterator r_it = r_m.begin();
00565 typename SetContainers::representation_container::iterator r_last = r_m.end();
00566
00567 for (; r_it != r_last; r_it++)
00568 {
00569
00570 long r_m_list_idx = getGListIndex(*(r_it));
00571
00572
00573 typename GroupContainers::stabilizer_container::iterator s_it = stab.begin();
00574 typename GroupContainers::stabilizer_container::iterator s_last = stab.end();
00575
00576 for (; s_it!=s_last; s_it++)
00577 {
00578 gLists[r_m_list_idx]->insert(*s_it,i);
00579 }
00580
00581 }
00582
00583 }
00584
00585
00586
00587 template <
00588 class Set,
00589 class Group,
00590 class GList,
00591 class GroupContainers,
00592 class SetContainers
00593 >
00594
00595 void DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::writeToFile(std::ofstream& ofs)
00596
00597 {
00598
00599
00600 ofs.write((char*)&gList_count, sizeof(long));
00601 ofs.write((char*)&filecounter, sizeof(long));
00602
00603 for (long i=0; i<gList_count; i++)
00604 {
00605
00606
00607 ofs<<gListRepresentatives[i];
00608
00609
00610
00611
00612 gLists[i]->setHeapStream(*m_heapStream);
00613 ofs<<*(gLists[i]);
00614
00615 }
00616
00617 };
00618
00619
00620
00621 template <
00622 class Set,
00623 class Group,
00624 class GList,
00625 class GroupContainers,
00626 class SetContainers
00627 >
00628
00629 void DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::remove(int fileindex)
00630
00631 {
00632 for (long i=0; i<gList_count; i++)
00633 {
00634 gLists[i]->remove(fileindex);
00635 }
00636 }
00637
00638
00639
00640 template <
00641 class Set,
00642 class Group,
00643 class GList,
00644 class GroupContainers,
00645 class SetContainers
00646 >
00647
00648 void DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::init()
00649
00650 {
00651
00652 if (gLists!=NULL)
00653 delete[] gLists;
00654 if (gListRepresentatives!=NULL)
00655 delete[] gListRepresentatives;
00656
00657 gListRepresentatives = new Set[expected_size];
00658 gLists = new GList*[expected_size];
00659
00660 current_size = expected_size;
00661
00662 };
00663
00664
00665 template <
00666 class Set,
00667 class Group,
00668 class GList,
00669 class GroupContainers,
00670 class SetContainers
00671 >
00672
00673 void DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::setHeapStream(std::fstream& hps)
00674
00675 {
00676 m_heapStream = &hps;
00677 }
00678
00679 template <
00680 class Set,
00681 class Group,
00682 class GList,
00683 class GroupContainers,
00684 class SetContainers
00685 >
00686
00687 std::fstream& DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::getHeapStream()
00688
00689 {
00690 return *m_heapStream;
00691 }
00692
00693 template <
00694 class Set,
00695 class Group,
00696 class GList,
00697 class GroupContainers,
00698 class SetContainers
00699 >
00700
00701 void DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::initFileRead(std::fstream& ifs)
00702
00703 {
00704
00705 Set represents;
00706
00707
00708 long gcount;
00709 ifs.read((char*)&gcount, sizeof(long));
00710 if (!ifs)
00711 throw IndexingException(IndexingException::FILE_ERROR_READ);
00712 ifs.read((char*)&filecounter, sizeof(long));
00713
00714 gListRepresentatives = new Set[gcount];
00715 gLists = new GList*[gcount];
00716 current_size = gcount;
00717
00718 gList_count = gcount;
00719
00720 for (int i=0; i<gcount; i++)
00721 {
00722
00723
00724 long ffpos = ifs.tellg();
00725
00726
00727
00728 ifs>>represents;
00729 if (!ifs)
00730 throw IndexingException(IndexingException::FILE_ERROR_READ);
00731
00732
00733 ffpos = ifs.tellg();
00734
00735
00736 gListRepresentatives[i] = represents;
00737
00738 gLists[i] = new GList(ifs);
00739
00740
00741 ffpos = ifs.tellg();
00742
00743
00744
00745 gLists[i]->setHeapStream(*m_heapStream);
00746
00747 }
00748
00749 }
00750
00751
00752
00753 template <
00754 class Set,
00755 class Group,
00756 class GList,
00757 class GroupContainers,
00758 class SetContainers
00759 >
00760
00761 GList* DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::operator[](const int& i) const
00762
00763 {
00764 return gLists[i];
00765 }
00766
00767
00768
00769
00770 template <
00771 class Set,
00772 class Group,
00773 class GList,
00774 class GroupContainers,
00775 class SetContainers
00776 >
00777
00778 long DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::getGListIndex(Set& r)
00779
00780 {
00781
00782 if (gList_count==0 || r<gListRepresentatives[0])
00783 {
00784 insertGList(r, 0);
00785 return 0;
00786 }
00787
00788 if (gListRepresentatives[gList_count-1]<r)
00789 {
00790 long lpos = gList_count;
00791 insertGList(r, gList_count);
00792 return lpos;
00793 }
00794
00795 long left = 0;
00796 long right = gList_count-1;
00797 bool found = false;
00798 int mid = 0;
00799
00800 while (left<=right && !found)
00801 {
00802
00803 mid = (left+right)/2;
00804
00805 Set cmp = gListRepresentatives[mid];
00806
00807 if (r<cmp)
00808 right = mid-1;
00809 else if (r==cmp)
00810 found = true;
00811 else
00812 left = mid+1;
00813
00814
00815 }
00816
00817 if (found)
00818 {
00819 return mid;
00820 }
00821
00822 if (gListRepresentatives[mid]<r)
00823 mid++;
00824
00825 insertGList(r, mid);
00826
00827 return mid;
00828
00829 }
00830
00831
00832
00833 template <
00834 class Set,
00835 class Group,
00836 class GList,
00837 class GroupContainers,
00838 class SetContainers
00839 >
00840
00841 long DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::size() const
00842
00843 {
00844 return gList_count;
00845 }
00846
00847
00848
00849 template <
00850 class Set,
00851 class Group,
00852 class GList,
00853 class GroupContainers,
00854 class SetContainers
00855 >
00856
00857 void DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::insertGList(const Set& r, long pos)
00858
00859 {
00860
00861 if (gList_count>=(current_size-1))
00862 expandGLists();
00863
00864 for (long i=gList_count; i>pos; i--)
00865 {
00866 gLists[i] = gLists[i-1];
00867 gListRepresentatives[i] = gListRepresentatives[i-1];
00868 };
00869
00870 gList_count++;
00871 gListRepresentatives[pos] = r;
00872 gLists[pos] = new GList(filecounter++);
00873
00874 };
00875
00876
00877
00878
00879 template <
00880 class Set,
00881 class Group,
00882 class GList,
00883 class GroupContainers,
00884 class SetContainers
00885 >
00886
00887 DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::~DefaultRepresentation()
00888
00889 {
00890 clear();
00891 }
00892
00893
00894 template <
00895 class Set,
00896 class Group,
00897 class GList,
00898 class GroupContainers,
00899 class SetContainers
00900 >
00901
00902 DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::iterator
00903 DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::begin()
00904
00905 {
00906 return iterator(this,&(gListRepresentatives[0]));
00907 }
00908
00909 template <
00910 class Set,
00911 class Group,
00912 class GList,
00913 class GroupContainers,
00914 class SetContainers
00915 >
00916
00917 DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::iterator
00918 DefaultRepresentation<Set,Group,GList,GroupContainers,SetContainers>::end()
00919
00920 {
00921 return iterator(this,&(gListRepresentatives[gList_count]));
00922 }
00923
00924
00925 }
00926 #endif