File Coverage

blib/lib/Lingua/YaTeA/IndexSet.pm
Criterion Covered Total %
statement 275 437 62.9
branch 77 158 48.7
condition 48 102 47.0
subroutine 37 52 71.1
pod 44 48 91.6
total 481 797 60.3


line stmt bran cond sub pod time code
1             package Lingua::YaTeA::IndexSet;
2 5     5   36 use strict;
  5         11  
  5         146  
3 5     5   24 use warnings;
  5         12  
  5         176  
4 5     5   35 use UNIVERSAL;
  5         19  
  5         26  
5 5     5   142 use Scalar::Util qw(blessed);
  5         11  
  5         23019  
6              
7             our $VERSION=$Lingua::YaTeA::VERSION;
8              
9             sub new
10             {
11 3570     3570 1 6172 my ($class,$words_a) = @_;
12 3570         5768 my $this = {};
13 3570         6309 bless ($this,$class);
14 3570         7114 $this->{INDEXES} = [];
15 3570         6642 return $this;
16             }
17              
18             sub copy
19             {
20 148     148 1 263 my ($this) = @_;
21 148         291 my $new = Lingua::YaTeA::IndexSet->new;
22 148         200 my $index;
23 148         195 foreach $index (@{$this->getIndexes})
  148         271  
24             {
25 564         732 push @{$new->getIndexes}, $index;
  564         844  
26             }
27 148         453 return $new;
28             }
29              
30              
31             sub getIndex
32             {
33 20544     20544 1 32150 my ($this,$rank) = @_;
34 20544         30054 return $this->getIndexes->[$rank];
35             }
36              
37             sub getIndexes
38             {
39 82357     82357 1 113625 my ($this) = @_;
40 82357         190039 return $this->{INDEXES};
41             }
42              
43              
44              
45             sub getLast
46             {
47 2564     2564 1 4152 my ($this) = @_;
48 2564         3883 return $this->getIndexes->[$#{$this->getIndexes}];
  2564         3950  
49             }
50              
51             sub getFirst
52             {
53 2395     2395 1 3699 my ($this) = @_;
54 2395         4121 return $this->getIndexes->[0];
55             }
56              
57              
58             sub appearsIn
59             {
60 0     0 1 0 my ($this,$larger_index_set) = @_;
61 0         0 my $index;
62 0         0 foreach $index (@{$this->getIndexes})
  0         0  
63             {
64 0 0       0 if(! $larger_index_set->indexExists($index))
65             {
66 0         0 return 0;
67             }
68             }
69 0         0 return 1;
70             }
71              
72              
73             sub searchSubIndexesSet
74             {
75 27     27 1 71 my ($this,$words_a,$chunking_data,$tag_set) = @_;
76 27         46 my @sub_indexes_set;
77             my $sub_index;
78              
79 27         64 my $max = (2**$this->getSize) -1;
80 27         48 my $i;
81              
82 27         89 for ($i = 1; $i <= $max; $i++)
83             {
84 2717         5376 $sub_index = $this->searchSubIndexes($i,$words_a,$this->getSize,$chunking_data,$tag_set);
85 2717 100 66     10322 if ((blessed($sub_index)) && ($sub_index->isa('Lingua::YaTeA::IndexSet'))) { # the substring is valid
86 277         952 push @sub_indexes_set, $sub_index;
87             }
88             }
89            
90 27         90 @sub_indexes_set = sort ({scalar @{$b->getIndexes} <=> scalar @{$a->getIndexes}}@sub_indexes_set);
  899         1137  
  899         1370  
  899         1284  
91 27         135 return \@sub_indexes_set;
92             }
93              
94              
95              
96              
97             sub isCoveredBy
98             {
99 60     60 1 104 my ($this,$other_index) = @_;
100 60         95 my $more_than_one_in_common = 0;
101 60 100 100     103 if
102             (
103             ($this->getLast <= $other_index->getFirst)
104             ||
105             ($this->getFirst >= $other_index->getLast)
106             )
107             {
108             # they do not cover at all;
109 21         85 return 0;
110            
111             }
112             # if($this->isDisjuncted($other_index))
113             # {
114             # return 0;
115             # }
116             else
117             {
118 39 100 100     97 if(
119             ($this->getFirst < $other_index->getFirst)
120             &&
121             ($this->getLast > $other_index->getLast)
122             )
123             {
124             # this one is larger than the other
125 12         67 return 0;
126             }
127             else
128             {
129             # $more_than_one_in_common = $this->moreThanOneInCommon($other_index);
130            
131 27         77 return ($this->moreThanOneInCommon($other_index));
132             }
133             }
134 0         0 return 1;
135             }
136            
137              
138             sub isDisjuncted
139             {
140 0     0 1 0 my ($this,$other_index) = @_;
141 0 0 0     0 if
142             (
143             ($this->getLast < $other_index->getFirst)
144             ||
145             ($this->getFirst > $other_index->getLast)
146             )
147             {
148             # they do not cover at all;
149 0         0 return 1;
150            
151             }
152 0         0 return 0;
153             }
154              
155             sub getIncluded
156             {
157 0     0 1 0 my ($this,$other_index_sets_a,$parsing_direction) = @_;
158 0         0 my $index_set;
159             my @included;
160 0         0 foreach $index_set (@{$other_index_sets_a})
  0         0  
161             {
162 0 0 0     0 if(
      0        
163             ($this->getFirst < $index_set->getFirst)
164             &&
165             ($this->getLast > $index_set->getLast)
166             &&
167             (!$this->moreThanOneInCommon($index_set))
168             )
169             {
170 0         0 push @included, $index_set;
171             }
172             }
173 0         0 $this->sortIncluded(\@included, $parsing_direction);
174 0         0 return \@included;
175             }
176              
177              
178             sub sortIncluded
179             {
180 0     0 0 0 my ($this,$included_index_sets_a,$parsing_direction) = @_;
181 0 0       0 if($parsing_direction eq "RIGHT")
182             {
183 0         0 @$included_index_sets_a = sort ({$b->getLast <=> $a->getLast} @$included_index_sets_a);
  0         0  
184             }
185             else
186             {
187 0         0 @$included_index_sets_a = sort ({$b->getFirst <=> $b->getFirst} @$included_index_sets_a);
  0         0  
188             }
189             }
190              
191             sub isIncluded
192             {
193 0     0 1 0 my ($this,$other_index,$pivot) = @_;
194 0 0 0     0 if(
      0        
195             ($this->getFirst >= $other_index->getFirst)
196             &&
197             ($this->getLast <= $other_index->getLast)
198             &&
199             (!$this->moreThanOneInCommon($other_index)
200             )
201             )
202             {
203 0 0 0     0 if(
204             (!defined $pivot)
205             ||
206             ($this->checkInclusion($other_index,$pivot))
207             )
208             {
209 0         0 return 1;
210             }
211 0         0 return 0;
212             }
213 0         0 return 0;
214             }
215              
216              
217             sub checkInclusion
218             {
219 0     0 1 0 my ($this,$other_index,$pivot) = @_;
220              
221 0         0 my $previous = $other_index->findPrevious($pivot);
222 0         0 my $next = $other_index->findNext($pivot);
223 0 0 0     0 if(
224             ($previous == -1)
225             ||
226             ($previous < $this->getFirst)
227             )
228             {
229 0 0 0     0 if(
230             ($next == -1)
231             ||
232             ($next > $this->getLast)
233             )
234             {
235 0         0 return 1;
236             }
237 0         0 return 0;
238             }
239 0         0 return 0;
240             }
241              
242              
243             sub appendPosition
244             {
245 23     23 1 56 my ($this,$pivot) = @_;
246 23 50       74 if($pivot == $this->getFirst)
247             {
248 0         0 return "LEFT";
249             }
250             else
251             {
252 23 100       73 if($pivot == $this->getLast)
253             {
254 19         53 return "RIGHT";
255             }
256             else
257             {
258 4         13 return "MIDDLE";
259             }
260             }
261             }
262              
263             sub adjunctionType
264             {
265 0     0 1 0 my ($this,$other_index,$pivot) = @_;
266 0 0       0 if ($this->getFirst == $other_index->getLast)
267             {
268 0         0 return "ADJUNCTION_LEFT";
269             }
270             else
271             {
272 0 0       0 if($this->getLast == $other_index->getFirst)
273             {
274 0         0 return "ADJUNCTION_RIGHT";
275             }
276             else
277             {
278 0         0 return "ADJUNCTION_MIDDLE";
279             }
280             }
281 0         0 return;
282             }
283              
284             sub moreThanOneInCommon
285             {
286 36     36 1 79 my ($this,$other_index) = @_;
287 36         57 my $counter = 0;
288 36         53 my $i = 0;
289 36         54 my $j = 0;
290              
291 36   66     85 while (
292             ($i < $this->getSize)
293             &&
294             ($j < $other_index->getSize)
295             )
296             {
297 36         78 while ($other_index->getIndex($j) == $this->getIndex($i))
298             {
299 43         77 $counter++;
300 43 100       90 if($counter > 1)
301             {
302 16         60 return 1;
303             }
304 27         36 $i++;
305 27         43 $j++;
306 27 100 66     53 if(
307             ($i >= $this->getSize)
308             ||
309             ($j >= $other_index->getSize)
310             )
311             {
312 1         7 return ($counter > 1);
313             }
314             }
315            
316 19   66     55 while (
317             ($i < $this->getSize)
318             &&
319             ($this->getIndex($i) < $other_index->getIndex($j))
320             )
321             {
322 4         9 $i++;
323             }
324            
325 19 50 33     46 if(
326             ($i >= $this->getSize)
327             ||
328             ($j >= $other_index->getSize)
329             )
330             {
331 0         0 return ($counter > 1);
332            
333             }
334            
335 19   100     41 while (
336             ($j < $other_index->getSize)
337             &&
338             ($other_index->getIndex($j) < $this->getIndex($i))
339             )
340             {
341 19         42 $j++;
342             }
343            
344              
345 19 100 66     49 if(
346             ($i >= $this->getSize)
347             ||
348             ($j >= $other_index->getSize)
349             )
350             {
351 10         67 return ($counter > 1);
352            
353             }
354            
355             }
356 9         77 return 0;
357             }
358              
359              
360              
361             sub searchSubIndexes
362             {
363 2717     2717 1 5114 my ($this,$i,$words_a,$size,$chunking_data,$tag_set) = @_;
364 2717         3607 my $j;
365 2717         3884 my $previous = -1;
366 2717         5057 my $sub_index = Lingua::YaTeA::IndexSet->new;
367 2717         5849 for ($j =1; $j <= $size ; $j++)
368             {
369              
370 21215 100       47006 if ($i&(2**($j-1)))
371             {
372 10670 100 100     33021 if (
      100        
373             (
374             ($previous == -1)
375             &&
376             ($words_a->[$j-1]->isCleaningFrontier($chunking_data))
377             )
378             ||
379             ($previous != -1)
380             )
381             {
382 10178         13622 $previous = $j;
383             # push @sub_indexes, $indexes_a->[$j-1];
384 10178         17684 $sub_index->addIndex($this->getIndex($j-1));
385             }
386             else
387             {
388 492         849 last;
389             }
390             }
391             }
392 2717 100       5102 if($sub_index->getSize > 1)
393             {
394 2124 100 100     3751 if(
      100        
395             ($sub_index->getSize < $this->getSize)
396             &&
397             ($words_a->[$sub_index->getLast]->isCleaningFrontier($chunking_data))
398             &&
399             ($sub_index->testSyntacticBreakAndRepetition($words_a,$tag_set))
400            
401             )
402             {
403 277         692 return $sub_index;
404             }
405             }
406             }
407              
408              
409             sub testSyntacticBreakAndRepetition
410             {
411 1815     1815 1 3666 my ($this,$words_a,$tag_set) = @_;
412 1815         3909 my @absent;
413             my $i;
414 1815         0 my %words;
415 1815         3556 my $j = $this->getFirst;
416              
417 1815         3901 for($i = 1; $i < $this->getSize; $i++)
418             {
419 4381         9628 $words{$words_a->[$j]->getLF}++;
420            
421 4381 100       7935 if($this->getIndex($i) != ($j+1))
422             {
423 2637         4624 while(($j+1) < $this->getIndex($i))
424             {
425 3593 100       8529 if($tag_set->existTag('PREPOSITIONS',$words_a->[($j+1)]->getIF)) # XXX TH 07/01/2008
426             {
427 1538         9110 return;
428             }
429 2055 100       4831 if($tag_set->existTag('CANDIDATES',$words_a->[($j+1)]->getPOS))
430             {
431 1821 50       3997 if (exists $words{$words_a->[($j+1)]->getLF})
432             {
433 0         0 return;
434             }
435             }
436            
437 2055         4232 $j++;
438             }
439             }
440             else
441             {
442 1744         3308 $j++;
443             }
444             }
445 277         1091 return 1;
446              
447             }
448              
449              
450             sub testSyntacticBreak
451             {
452 0     0 0 0 my ($this,$words_a,$tag_set) = @_;
453 0         0 my @absent;
454             my $i;
455 0         0 my %words;
456 0         0 my $j = $this->getFirst;
457              
458 0         0 for($i = 1; $i < $this->getSize; $i++)
459             {
460 0         0 $words{$words_a->[$j]->getLF}++;
461            
462 0 0       0 if($this->getIndex($i) != ($j+1))
463             {
464 0         0 while(($j+1) < $this->getIndex($i))
465             {
466 0 0       0 if($tag_set->existTag('PREPOSITIONS',$words_a->[($j+1)]->getIF)) # XXX TH 07/01/2008
467             {
468 0         0 return;
469             }
470 0 0       0 if($tag_set->existTag('CANDIDATES',$words_a->[($j+1)]->getPOS))
471             {
472 0 0       0 if (exists $words{$words_a->[($j+1)]->getLF})
473             {
474 0         0 return;
475             }
476             }
477            
478 0         0 $j++;
479             }
480             }
481             else
482             {
483 0         0 $j++;
484             }
485             }
486 0         0 return 1;
487              
488             }
489              
490              
491             sub buildIFSequence
492             {
493 259     259 1 467 my ($this,$words_a) = @_;
494 259         409 my $IF;
495             my $index;
496 259         344 foreach $index (@{$this->getIndexes})
  259         428  
497             {
498 978         2030 $IF .= $words_a->[$index]->getIF . " ";
499             }
500 259         471 chop $IF;
501 259         701 return $IF;
502             }
503              
504             sub buildPOSSequence
505             {
506 71     71 1 190 my ($this,$words_a,$tag_set) = @_;
507 71         130 my $POS;
508             my $index;
509 71         106 foreach $index (@{$this->getIndexes})
  71         160  
510             {
511 270 100       708 if ($tag_set->existTag('PREPOSITIONS',$words_a->[$index]->getIF)) # XXX TH 07/01/2008
512             {
513 41         107 $POS .= $words_a->[$index]->getLF . " ";
514             }
515             else
516             {
517 229         493 $POS .= $words_a->[$index]->getPOS . " ";
518             }
519             }
520 71         151 chop $POS;
521 71         190 return $POS;
522             }
523              
524             sub buildLFSequence
525             {
526 260     260 1 449 my ($this,$words_a) = @_;
527 260         402 my $LF;
528             my $index;
529 260         365 foreach $index (@{$this->getIndexes})
  260         408  
530             {
531 987         1898 $LF .= $words_a->[$index]->getLF . " ";
532             }
533 260         423 chop $LF;
534 260         632 return $LF;
535             }
536              
537             sub buildLinguisticKey
538             {
539 0     0 1 0 my ($this,$words_a,$tag_set) = @_;
540 0         0 my $key = $this->buildIFSequence($words_a) . "~" . $this->buildPOSSequence($words_a,$tag_set) . "~" . $this->buildLFSequence($words_a);
541 0         0 return $key;
542             }
543              
544             sub chooseBestSource
545             {
546 6     6 1 20 my ($this,$source_a,$words_a,$tag_set) = @_;
547 6 50       24 if(scalar @$source_a > 1)
548             {
549 0         0 @$source_a = sort ({$this->sortIslands($a,$b,$words_a,$tag_set)} @$source_a);
  0         0  
550             }
551 6         21 return $source_a->[0];
552             }
553              
554             sub sortIslands
555             {
556 0     0 1 0 my ($this,$a,$b,($words_a,$tag_set)) = @_;
557 0 0       0 if($a->getPOS eq $b->getPOS){ # both POS are the same
558 0         0 return 0;
559             }
560 0 0       0 if($a->getPOS eq $this->buildPOSSequence($words_a,$tag_set)){
561 0         0 return -1;
562            
563             }
564 0 0       0 if($b->getPOS eq $this->buildPOSSequence($words_a,$tag_set)){
565 0         0 return 1;
566             }
567 0         0 return 0; # both POS are different and different from the wanted POS
568              
569             }
570              
571             sub joinAll
572             {
573 168     168 1 293 my ($this,$joint) = @_;
574 168         211 return (join ($joint, @{$this->getIndexes}));
  168         279  
575             }
576              
577             sub fill
578             {
579 123     123 1 214 my ($this,$words_a) = @_;
580 123         191 my $i = 0;
581              
582 123         305 while ($i < scalar @$words_a)
583             {
584 288         402 push @{$this->getIndexes}, $i++;
  288         492  
585             }
586             }
587              
588             sub getSize
589             {
590 14774     14774 1 22695 my ($this) = @_;
591 14774         18650 return scalar @{$this->getIndexes};
  14774         21410  
592             }
593              
594             sub addIndex
595             {
596 11405     11405 1 17843 my ($this,$index) = @_;
597 11405         14634 push @{$this->getIndexes}, $index;
  11405         17280  
598 11405         15803 @{$this->getIndexes}= sort ({$a <=> $b} @{$this->getIndexes});
  11405         16974  
  38924         53306  
  11405         16811  
599             }
600              
601             sub getPartial
602             {
603 64     64 1 184 my ($this,$length,$position) = @_;
604 64         100 my $i;
605 64         170 my $partial = Lingua::YaTeA::IndexSet->new;
606              
607 64 50       231 if($position eq "LEFT")
608             {
609 0         0 for ($i = 0; $i < $length; $i++)
610             {
611 0         0 $partial->addIndex($this->getIndex($i));
612             }
613             }
614 64 50       161 if($position eq "RIGHT")
615             {
616 64         95 for ($i = (scalar @{$this->getIndexes} - $length); $i < scalar @{$this->getIndexes}; $i++){
  64         134  
  218         387  
617 154         284 $partial->addIndex($this->getIndex($i));
618             }
619             }
620              
621 64         238 return $partial;
622             }
623              
624             sub simplify
625             {
626 89     89 1 228 my ($this,$partial_index_set,$node_set,$tree,$pivot,$fh) = @_;
627 89         140 my $i;
628 89         149 my $j=0;
629 89         232 my $index;
630             my $index_partial;
631 89         0 my @simplified;
632 89 100       213 if(defined $fh)
633             {
634             # print $fh "arbre:";
635             # $tree->getIndexSet->print($fh);
636             # print $fh "\nsimplifie:";
637             # $this->print($fh);
638             # print $fh "\npartial:";
639             # $partial_index_set->print($fh);
640             # if(defined $pivot)
641             # {
642             # print $fh "\n";
643             # print $fh "pivot :" .$pivot ."\n";
644             # }
645             }
646 89         175 for ($i= 0; $i < scalar @{$this->getIndexes}; $i++)
  414         730  
647             {
648 325         565 $index = $this->getIndex($i);
649            
650             # if(defined $fh)
651             # {
652             # print $fh "index :" . $index . "\n";
653             # }
654 325 100       456 if($j < scalar @{$partial_index_set->getIndexes})
  325         540  
655             {
656 310         675 &simplifyByCurrent(\$j,\@simplified,$index,$partial_index_set,$node_set,$tree,$pivot,$fh);
657             }
658             else
659             {
660             # if(defined $fh)
661             # {
662             # print $fh "push2 dans simplified " . $index . "\n";
663             # }
664 15         33 push @simplified, $index;
665             }
666            
667             }
668 89         178 @{$this->getIndexes} = @simplified;
  89         170  
669             # if(defined $fh)
670             # {
671             # print $fh "apres\n";
672             # print $fh "arbre:";
673             # $tree->getIndexSet->print($fh);
674             # print $fh "\nsimplifie:";
675             # $this->print($fh);
676             # print $fh "\npartial:";
677             # $partial_index_set->print($fh);
678             # print $fh "\n";
679             # }
680              
681             }
682              
683              
684             sub simplifyByCurrent
685             {
686 310     310 0 652 my ($j_s,$simplified_a,$index,$partial_index_set,$node_set,$tree,$pivot,$fh) = @_;
687 310         593 my $index_partial = $partial_index_set->getIndex($$j_s);
688 310   33     799 while (
      33        
689             ($index_partial < $index)
690             &&
691             ($tree->getIndexSet->indexExists($index_partial))
692             &&
693 0         0 ($$j_s < scalar @{$partial_index_set->getIndexes} -1)
694             )
695             {
696 0         0 $$j_s++;
697 0         0 $index_partial = $partial_index_set->getIndex($$j_s);
698             }
699             # if(defined $fh)
700             # {
701             # print $fh "\tindex partiel :" . $index_partial . "\n";
702             # }
703            
704 310 100       537 if($index == $index_partial)
705             {
706 208 100 100     934 if(
      100        
707             (
708             (!defined $pivot)
709             ||
710             ($index != $pivot)
711             )
712             &&
713             (! $tree->getIndexSet->indexExists($index))
714             )
715             {
716 144         339 $tree->getIndexSet->addIndex($index);
717             # if(defined $fh)
718             # {
719             # print $fh "ajoute a iindex d'arbre :" . $index . "\n";
720             # }
721             }
722             # hack from TH
723 208         632 my $head = $node_set->getRoot->searchHead(0);
724 208 50 33     783 if((ref($head) ne "SCALAR") && (defined $head)) {
725 208 100       463 if ($index_partial == $head->getIndex)
726             {
727 89         176 push @$simplified_a, $index_partial;
728            
729             }
730             } else {
731 0         0 warn "The head is undefined\n";
732 0         0 return(-1);
733             }
734            
735 208         499 $$j_s++;
736             # if(defined $fh)
737             # {
738             # print $fh "incerementation j" . $$j_s . "\n";
739             # }
740             }
741            
742             else
743             {
744             # if(defined $fh)
745             # {
746             # print $fh "push dans simplified " . $index . "\n";
747             # }
748 102         251 push @$simplified_a, $index;
749             }
750             }
751              
752              
753             sub simplifyWithSeveralPivots
754             {
755 0     0 1 0 my ($this,$partial_index_set,$node_set,$tree,$pivots_h) = @_;
756 0         0 my $i;
757 0         0 my $j=0;
758 0         0 my $index;
759             my $index_partial;
760 0         0 my @simplified;
761            
762 0         0 for ($i= 0; $i < scalar @{$this->getIndexes}; $i++)
  0         0  
763             {
764 0         0 $index = $this->getIndex($i);
765              
766 0 0       0 if($j < scalar @{$partial_index_set->getIndexes})
  0         0  
767             {
768 0         0 $index_partial = $partial_index_set->getIndex($j);
769              
770 0 0       0 if($index == $index_partial)
771             {
772 0 0       0 if(exists $pivots_h->{$index})
773             {
774 0         0 push @simplified, $index_partial;
775             }
776 0         0 $j++;
777             }
778            
779             else
780             {
781 0         0 push @simplified, $index;
782             }
783             }
784             else
785             {
786 0         0 push @simplified, $index;
787             }
788            
789             }
790 0         0 @{$this->getIndexes} = @simplified;
  0         0  
791              
792             }
793              
794              
795             sub searchPivot
796             {
797 22     22 1 61 my ($this,$other_set) = @_;
798 22         48 my $i;
799             my $j;
800              
801 22         38 foreach $i (@{$this->getIndexes})
  22         54  
802             {
803 58         88 foreach $j (@{$other_set->getIndexes})
  58         95  
804             {
805 164 100       345 if ($i == $j)
806             {
807 19         67 return $i;
808             }
809             }
810             }
811 3         12 return;
812             }
813              
814              
815              
816             sub print
817             {
818 3     3 1 7 my ($this,$fh) = @_;
819 3         4 my $index;
820 3 50       8 if(defined $fh)
821             {
822 3         8 print $fh (joinAll($this,'-'));
823             }
824             else
825             {
826 0         0 print joinAll(($this,'-'));
827             }
828             }
829              
830             sub mergeWith
831             {
832 48     48 1 87 my ($this,$index_set_to_add) = @_;
833 48         71 my $index;
834 48         71 push @{$this->getIndexes},@{$index_set_to_add->getIndexes};
  48         100  
  48         81  
835 48         80 @{$this->getIndexes}= sort ({$a <=> $b} @{$this->getIndexes});
  48         113  
  290         450  
  48         86  
836             }
837              
838              
839             sub contains
840             {
841 30     30 1 56 my ($this,$other_index) = @_;
842            
843 30 100 66     69 if(
844             ($this->getFirst <= $other_index->getFirst)
845             &&
846             ($this->getLast >= $other_index->getLast)
847             )
848             {
849 24         90 return 1;
850             }
851 6         55 return 0;
852             }
853              
854             sub indexExists
855             {
856 196     196 1 392 my ($this,$index) = @_;
857 196         296 my %this_index = map ({ $_ => 1 } @{$this->getIndexes});
  617         1476  
  196         348  
858 196 100       887 if(exists $this_index{$index})
859             {
860 30         124 return 1;
861             }
862 166         585 return 0;
863             }
864              
865             sub getIncludedContext
866             {
867 1     1 1 4 my ($this,$included) = @_;
868 1         5 my $left_context = $this->findPrevious($included->getFirst);
869 1         4 my $right_context = $this->findNext($included->getLast);
870              
871 1         6 return ($left_context,$right_context);
872             }
873              
874             sub findPrevious
875             {
876 1     1 1 3 my ($this,$stop_index) = @_;
877 1         3 my $previous = -1;
878 1         2 my $index;
879 1         3 foreach $index (@{$this->getIndexes})
  1         5  
880             {
881 5 100       12 if($index >= $stop_index)
882             {
883 1         4 return $previous;
884             }
885 4         6 $previous = $index;
886             }
887 0         0 return -1;
888             }
889              
890             sub findNext
891             {
892 1     1 1 3 my ($this,$stop_index) = @_;
893 1         3 my $index;
894 1         2 foreach $index (@{$this->getIndexes})
  1         3  
895             {
896 5 50       13 if($index > $stop_index)
897             {
898 0         0 return $index;
899             }
900             }
901 1         3 return -1;
902             }
903              
904             sub removeIndex
905             {
906 0     0 1 0 my ($this,$index_to_remove) = @_;
907 0         0 my @tmp;
908             my $index;
909 0         0 foreach $index (@{$this->getIndexes})
  0         0  
910             {
911 0 0       0 if($index != $index_to_remove)
912             {
913 0         0 push @tmp, $index;
914             }
915             }
916 0         0 @{$this->getIndexes} = @tmp;
  0         0  
917             }
918              
919              
920             sub defineAppendMode
921             {
922 52     52 1 149 my ($this,$to_append,$pivot,$fh) = @_;
923 52         88 my $mode;
924             # if(defined $fh)
925             # {
926             # print $fh "-> this\n";
927             # print $fh $this->getFirst . "\n";
928             # print $fh $this->getLast . "\n";
929             # print $fh "-> to append\n";
930             # print $fh $to_append->getFirst . "\n";
931             # print $fh $to_append->getLast . "\n";
932             # }
933              
934 52 50       85 if (scalar(@{$this->getIndexes}) > 0) {
  52         97  
935 52 100 66     114 if(
936             ($this->getLast < $to_append->getFirst)
937             ||
938             ($this->getFirst > $to_append->getLast)
939             )
940             {
941             # if(defined $fh)
942             # {
943             # print $fh "dis 1 \n";
944             # }
945 3         14 return "DISJUNCTION"; # external disjunction
946             }
947            
948 49 100 100     155 if(
949             ($this->getLast == $to_append->getFirst)
950             ||
951             ($this->getFirst == $to_append->getLast)
952             )
953             {
954 25         91 return "ADJUNCTION";
955             }
956              
957 24 100       63 if($this->getFirst <= $to_append->getFirst)
958             {
959 1 50       2 if($this->getLast >= $to_append->getLast)
960             {
961 1 50       4 if(defined $pivot)
962             {
963              
964 1         5 return "INCLUSION";
965             }
966 0 0       0 if(! $this->crosses($to_append))
967             {
968 0         0 return "DISJUNCTION"; # internal disjunction
969             }
970            
971             }
972             else
973             {
974 0 0       0 if($this->getLast < $to_append->getLast)
975             {
976 0 0       0 if (defined $pivot)
977             {
978 0 0       0 if($pivot == $this->getLast)
979            
980             {
981 0         0 return "INCLUSION";
982             }
983             else
984             {
985 0         0 return "REVERSED_INCLUSION";
986             }
987             }
988             }
989             }
990             }
991 23 50       81 if($this->getFirst >= $to_append->getFirst)
992             {
993 23 100       59 if($this->getLast <= $to_append->getLast)
994             {
995 19 50       51 if(defined $pivot)
996             {
997 19         73 return "REVERSED_INCLUSION";
998             }
999 0 0       0 if(! $this->crosses($to_append))
1000             {
1001 0         0 return "DISJUNCTION"; # internal disjunction
1002             }
1003             }
1004             else
1005             {
1006 4 50       14 if($this->getLast > $to_append->getLast)
1007             {
1008 4 50       14 if (defined $pivot)
1009             {
1010 4 50       14 if ($pivot == $this->getFirst)
1011             {
1012 0         0 return "INCLUSION";
1013             }
1014             else
1015             {
1016 4         15 return "REVERSED_INCLUSION";
1017             }
1018             }
1019             }
1020             }
1021             }
1022             }
1023 0         0 return $mode;
1024             }
1025              
1026             sub crosses
1027             {
1028 0     0 1 0 my ($this,$to_append) = @_;
1029 0         0 my $gaps_a;
1030             my %gaps;
1031 0         0 my $gap_h;
1032 0         0 my $index;
1033 0         0 my @fulled_gaps;
1034 0 0 0     0 if
      0        
      0        
      0        
      0        
1035             (
1036             (
1037             ($this->getFirst < $to_append->getFirst)
1038             &&
1039             ($this->getLast > $to_append->getFirst)
1040             &&
1041             ($this->getLast < $to_append->getLast)
1042             )
1043             ||
1044             (
1045             ($this->getFirst > $to_append->getFirst)
1046             &&
1047             ($this->getFirst < $to_append->getLast)
1048             &&
1049             ($this->getLast > $to_append->getLast)
1050             )
1051             )
1052             {
1053 0         0 return 1;
1054             }
1055 0         0 $gaps_a = $this->getGaps;
1056 0 0       0 if(scalar @$gaps_a > 1)
1057             {
1058 0         0 foreach $index (@{$to_append->getIndexes})
  0         0  
1059             {
1060 0         0 foreach $gap_h (@$gaps_a)
1061             {
1062 0 0       0 if(exists $gap_h->{$index})
1063             {
1064 0         0 $gaps{$gap_h}++;
1065             }
1066             }
1067             }
1068             }
1069 0         0 @fulled_gaps = keys %gaps;
1070 0 0       0 if(scalar @fulled_gaps > 1)
1071             {
1072 0         0 return 1;
1073             }
1074 0         0 return 0;
1075             }
1076              
1077             sub getGaps
1078             {
1079 64     64 1 135 my ($this) = @_;
1080 64         111 my $index;
1081             my $previous;
1082 64         113 my $counter = 0;
1083 64         104 my @gaps;
1084              
1085             # print STDERR "gG1\n";
1086              
1087 64         105 foreach $index (@{$this->getIndexes})
  64         119  
1088             {
1089             # print STDERR "gG2 (index = $index)\n";
1090             # print STDERR "gG2 (counter = $counter)\n";
1091             # if (defined $previous) {print STDERR "gG2 (previous = $previous)\n";}
1092             # TH - 25072007
1093 205 100       394 if (defined $previous) {
1094              
1095 141 100       305 if ($index > $previous+1) {
1096 25         53 my %gap;
1097 25         42 $counter = $previous+1;
1098             # print STDERR "gG3\n";
1099 25         77 while ($counter != $index)
1100             {
1101             # print STDERR "gG3 (counter = $counter)\n";
1102            
1103 35         111 $gap{$counter++} = 0;
1104             }
1105             # print STDERR "gG4\n";
1106 25         80 push @gaps, \%gap;
1107             }
1108             }
1109 205         368 $previous = $index;
1110             }
1111             # print STDERR "gGF\n";
1112              
1113 64         171 return \@gaps;
1114             }
1115              
1116              
1117             sub isDiscontinuous
1118             {
1119 0     0 0   my ($this) = @_;
1120 0           my $i;
1121 0           for ($i = 1; $i < scalar @{$this->getIndexes}; $i++)
  0            
1122             {
1123 0 0         if($this->getIndexes->[$i] != $this->getIndexes->[$i-1] +1)
1124             {
1125 0           return 1;
1126             }
1127             }
1128 0           return 0;
1129             }
1130              
1131             sub removeDoubles
1132             {
1133 0     0 1   my ($this) = @_;
1134 0           my $index;
1135             my %uniq;
1136 0           my @tmp;
1137              
1138 0           foreach $index (@{$this->getIndexes})
  0            
1139             {
1140 0 0         if(!exists $uniq{$index})
1141             {
1142 0           push @tmp, $index;
1143             }
1144 0           $uniq{$index}++;
1145             }
1146 0           @{$this->getIndexes} = @tmp;
  0            
1147             }
1148              
1149             1;
1150              
1151             __END__