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   29 use strict;
  5         19  
  5         137  
3 5     5   22 use warnings;
  5         8  
  5         142  
4 5     5   28 use UNIVERSAL;
  5         15  
  5         24  
5 5     5   116 use Scalar::Util qw(blessed);
  5         15  
  5         18585  
6              
7             our $VERSION=$Lingua::YaTeA::VERSION;
8              
9             sub new
10             {
11 3570     3570 1 5367 my ($class,$words_a) = @_;
12 3570         5020 my $this = {};
13 3570         4685 bless ($this,$class);
14 3570         6140 $this->{INDEXES} = [];
15 3570         5248 return $this;
16             }
17              
18             sub copy
19             {
20 148     148 1 202 my ($this) = @_;
21 148         215 my $new = Lingua::YaTeA::IndexSet->new;
22 148         175 my $index;
23 148         161 foreach $index (@{$this->getIndexes})
  148         212  
24             {
25 564         575 push @{$new->getIndexes}, $index;
  564         738  
26             }
27 148         349 return $new;
28             }
29              
30              
31             sub getIndex
32             {
33 20544     20544 1 26017 my ($this,$rank) = @_;
34 20544         25364 return $this->getIndexes->[$rank];
35             }
36              
37             sub getIndexes
38             {
39 82357     82357 1 96841 my ($this) = @_;
40 82357         152582 return $this->{INDEXES};
41             }
42              
43              
44              
45             sub getLast
46             {
47 2564     2564 1 3350 my ($this) = @_;
48 2564         3171 return $this->getIndexes->[$#{$this->getIndexes}];
  2564         3379  
49             }
50              
51             sub getFirst
52             {
53 2395     2395 1 3099 my ($this) = @_;
54 2395         3132 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 52 my ($this,$words_a,$chunking_data,$tag_set) = @_;
76 27         40 my @sub_indexes_set;
77             my $sub_index;
78              
79 27         52 my $max = (2**$this->getSize) -1;
80 27         36 my $i;
81              
82 27         65 for ($i = 1; $i <= $max; $i++)
83             {
84 2717         4333 $sub_index = $this->searchSubIndexes($i,$words_a,$this->getSize,$chunking_data,$tag_set);
85 2717 100 66     8150 if ((blessed($sub_index)) && ($sub_index->isa('Lingua::YaTeA::IndexSet'))) { # the substring is valid
86 277         730 push @sub_indexes_set, $sub_index;
87             }
88             }
89            
90 27         71 @sub_indexes_set = sort ({scalar @{$b->getIndexes} <=> scalar @{$a->getIndexes}}@sub_indexes_set);
  899         908  
  899         1044  
  899         1065  
91 27         111 return \@sub_indexes_set;
92             }
93              
94              
95              
96              
97             sub isCoveredBy
98             {
99 60     60 1 115 my ($this,$other_index) = @_;
100 60         75 my $more_than_one_in_common = 0;
101 60 100 100     85 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         63 return 0;
110            
111             }
112             # if($this->isDisjuncted($other_index))
113             # {
114             # return 0;
115             # }
116             else
117             {
118 39 100 100     69 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         40 return 0;
126             }
127             else
128             {
129             # $more_than_one_in_common = $this->moreThanOneInCommon($other_index);
130            
131 27         56 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 53 my ($this,$pivot) = @_;
246 23 50       46 if($pivot == $this->getFirst)
247             {
248 0         0 return "LEFT";
249             }
250             else
251             {
252 23 100       48 if($pivot == $this->getLast)
253             {
254 19         42 return "RIGHT";
255             }
256             else
257             {
258 4         10 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 54 my ($this,$other_index) = @_;
287 36         44 my $counter = 0;
288 36         43 my $i = 0;
289 36         44 my $j = 0;
290              
291 36   66     67 while (
292             ($i < $this->getSize)
293             &&
294             ($j < $other_index->getSize)
295             )
296             {
297 36         65 while ($other_index->getIndex($j) == $this->getIndex($i))
298             {
299 43         72 $counter++;
300 43 100       72 if($counter > 1)
301             {
302 16         48 return 1;
303             }
304 27         35 $i++;
305 27         32 $j++;
306 27 100 66     35 if(
307             ($i >= $this->getSize)
308             ||
309             ($j >= $other_index->getSize)
310             )
311             {
312 1         6 return ($counter > 1);
313             }
314             }
315            
316 19   66     41 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     37 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     29 while (
336             ($j < $other_index->getSize)
337             &&
338             ($other_index->getIndex($j) < $this->getIndex($i))
339             )
340             {
341 19         36 $j++;
342             }
343            
344              
345 19 100 66     39 if(
346             ($i >= $this->getSize)
347             ||
348             ($j >= $other_index->getSize)
349             )
350             {
351 10         45 return ($counter > 1);
352            
353             }
354            
355             }
356 9         33 return 0;
357             }
358              
359              
360              
361             sub searchSubIndexes
362             {
363 2717     2717 1 3948 my ($this,$i,$words_a,$size,$chunking_data,$tag_set) = @_;
364 2717         2849 my $j;
365 2717         3273 my $previous = -1;
366 2717         4344 my $sub_index = Lingua::YaTeA::IndexSet->new;
367 2717         4713 for ($j =1; $j <= $size ; $j++)
368             {
369              
370 21215 100       38289 if ($i&(2**($j-1)))
371             {
372 10670 100 100     27236 if (
      100        
373             (
374             ($previous == -1)
375             &&
376             ($words_a->[$j-1]->isCleaningFrontier($chunking_data))
377             )
378             ||
379             ($previous != -1)
380             )
381             {
382 10178         11433 $previous = $j;
383             # push @sub_indexes, $indexes_a->[$j-1];
384 10178         14202 $sub_index->addIndex($this->getIndex($j-1));
385             }
386             else
387             {
388 492         707 last;
389             }
390             }
391             }
392 2717 100       3823 if($sub_index->getSize > 1)
393             {
394 2124 100 100     2852 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         577 return $sub_index;
404             }
405             }
406             }
407              
408              
409             sub testSyntacticBreakAndRepetition
410             {
411 1815     1815 1 2765 my ($this,$words_a,$tag_set) = @_;
412 1815         3113 my @absent;
413             my $i;
414 1815         0 my %words;
415 1815         2739 my $j = $this->getFirst;
416              
417 1815         3062 for($i = 1; $i < $this->getSize; $i++)
418             {
419 4381         8033 $words{$words_a->[$j]->getLF}++;
420            
421 4381 100       6573 if($this->getIndex($i) != ($j+1))
422             {
423 2637         4063 while(($j+1) < $this->getIndex($i))
424             {
425 3593 100       6955 if($tag_set->existTag('PREPOSITIONS',$words_a->[($j+1)]->getIF)) # XXX TH 07/01/2008
426             {
427 1538         7202 return;
428             }
429 2055 100       3894 if($tag_set->existTag('CANDIDATES',$words_a->[($j+1)]->getPOS))
430             {
431 1821 50       3239 if (exists $words{$words_a->[($j+1)]->getLF})
432             {
433 0         0 return;
434             }
435             }
436            
437 2055         3471 $j++;
438             }
439             }
440             else
441             {
442 1744         2730 $j++;
443             }
444             }
445 277         861 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 349 my ($this,$words_a) = @_;
494 259         297 my $IF;
495             my $index;
496 259         267 foreach $index (@{$this->getIndexes})
  259         373  
497             {
498 978         1552 $IF .= $words_a->[$index]->getIF . " ";
499             }
500 259         353 chop $IF;
501 259         555 return $IF;
502             }
503              
504             sub buildPOSSequence
505             {
506 71     71 1 122 my ($this,$words_a,$tag_set) = @_;
507 71         124 my $POS;
508             my $index;
509 71         86 foreach $index (@{$this->getIndexes})
  71         101  
510             {
511 270 100       519 if ($tag_set->existTag('PREPOSITIONS',$words_a->[$index]->getIF)) # XXX TH 07/01/2008
512             {
513 41         81 $POS .= $words_a->[$index]->getLF . " ";
514             }
515             else
516             {
517 229         389 $POS .= $words_a->[$index]->getPOS . " ";
518             }
519             }
520 71         122 chop $POS;
521 71         151 return $POS;
522             }
523              
524             sub buildLFSequence
525             {
526 260     260 1 364 my ($this,$words_a) = @_;
527 260         298 my $LF;
528             my $index;
529 260         277 foreach $index (@{$this->getIndexes})
  260         339  
530             {
531 987         1538 $LF .= $words_a->[$index]->getLF . " ";
532             }
533 260         323 chop $LF;
534 260         509 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 15 my ($this,$source_a,$words_a,$tag_set) = @_;
547 6 50       16 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         17 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 232 my ($this,$joint) = @_;
574 168         187 return (join ($joint, @{$this->getIndexes}));
  168         229  
575             }
576              
577             sub fill
578             {
579 123     123 1 173 my ($this,$words_a) = @_;
580 123         143 my $i = 0;
581              
582 123         249 while ($i < scalar @$words_a)
583             {
584 288         306 push @{$this->getIndexes}, $i++;
  288         712  
585             }
586             }
587              
588             sub getSize
589             {
590 14774     14774 1 18479 my ($this) = @_;
591 14774         14742 return scalar @{$this->getIndexes};
  14774         17806  
592             }
593              
594             sub addIndex
595             {
596 11405     11405 1 14696 my ($this,$index) = @_;
597 11405         12094 push @{$this->getIndexes}, $index;
  11405         13871  
598 11405         13212 @{$this->getIndexes}= sort ({$a <=> $b} @{$this->getIndexes});
  11405         14521  
  38924         45090  
  11405         14598  
599             }
600              
601             sub getPartial
602             {
603 64     64 1 151 my ($this,$length,$position) = @_;
604 64         73 my $i;
605 64         145 my $partial = Lingua::YaTeA::IndexSet->new;
606              
607 64 50       147 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       125 if($position eq "RIGHT")
615             {
616 64         82 for ($i = (scalar @{$this->getIndexes} - $length); $i < scalar @{$this->getIndexes}; $i++){
  64         113  
  218         311  
617 154         229 $partial->addIndex($this->getIndex($i));
618             }
619             }
620              
621 64         179 return $partial;
622             }
623              
624             sub simplify
625             {
626 89     89 1 176 my ($this,$partial_index_set,$node_set,$tree,$pivot,$fh) = @_;
627 89         98 my $i;
628 89         103 my $j=0;
629 89         184 my $index;
630             my $index_partial;
631 89         0 my @simplified;
632 89 100       184 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         135 for ($i= 0; $i < scalar @{$this->getIndexes}; $i++)
  414         570  
647             {
648 325         470 $index = $this->getIndex($i);
649            
650             # if(defined $fh)
651             # {
652             # print $fh "index :" . $index . "\n";
653             # }
654 325 100       386 if($j < scalar @{$partial_index_set->getIndexes})
  325         425  
655             {
656 310         520 &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         21 push @simplified, $index;
665             }
666            
667             }
668 89         131 @{$this->getIndexes} = @simplified;
  89         120  
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 495 my ($j_s,$simplified_a,$index,$partial_index_set,$node_set,$tree,$pivot,$fh) = @_;
687 310         484 my $index_partial = $partial_index_set->getIndex($$j_s);
688 310   33     615 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       460 if($index == $index_partial)
705             {
706 208 100 100     721 if(
      100        
707             (
708             (!defined $pivot)
709             ||
710             ($index != $pivot)
711             )
712             &&
713             (! $tree->getIndexSet->indexExists($index))
714             )
715             {
716 144         254 $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         449 my $head = $node_set->getRoot->searchHead(0);
724 208 50 33     612 if((ref($head) ne "SCALAR") && (defined $head)) {
725 208 100       365 if ($index_partial == $head->getIndex)
726             {
727 89         138 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         420 $$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         197 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 50 my ($this,$other_set) = @_;
798 22         36 my $i;
799             my $j;
800              
801 22         30 foreach $i (@{$this->getIndexes})
  22         53  
802             {
803 58         62 foreach $j (@{$other_set->getIndexes})
  58         90  
804             {
805 164 100       258 if ($i == $j)
806             {
807 19         47 return $i;
808             }
809             }
810             }
811 3         9 return;
812             }
813              
814              
815              
816             sub print
817             {
818 3     3 1 5 my ($this,$fh) = @_;
819 3         3 my $index;
820 3 50       14 if(defined $fh)
821             {
822 3         9 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 67 my ($this,$index_set_to_add) = @_;
833 48         53 my $index;
834 48         49 push @{$this->getIndexes},@{$index_set_to_add->getIndexes};
  48         80  
  48         72  
835 48         64 @{$this->getIndexes}= sort ({$a <=> $b} @{$this->getIndexes});
  48         68  
  290         347  
  48         67  
836             }
837              
838              
839             sub contains
840             {
841 30     30 1 39 my ($this,$other_index) = @_;
842            
843 30 100 66     54 if(
844             ($this->getFirst <= $other_index->getFirst)
845             &&
846             ($this->getLast >= $other_index->getLast)
847             )
848             {
849 24         81 return 1;
850             }
851 6         19 return 0;
852             }
853              
854             sub indexExists
855             {
856 196     196 1 312 my ($this,$index) = @_;
857 196         242 my %this_index = map ({ $_ => 1 } @{$this->getIndexes});
  617         1228  
  196         275  
858 196 100       469 if(exists $this_index{$index})
859             {
860 30         95 return 1;
861             }
862 166         452 return 0;
863             }
864              
865             sub getIncludedContext
866             {
867 1     1 1 3 my ($this,$included) = @_;
868 1         2 my $left_context = $this->findPrevious($included->getFirst);
869 1         2 my $right_context = $this->findNext($included->getLast);
870              
871 1         3 return ($left_context,$right_context);
872             }
873              
874             sub findPrevious
875             {
876 1     1 1 3 my ($this,$stop_index) = @_;
877 1         2 my $previous = -1;
878 1         1 my $index;
879 1         2 foreach $index (@{$this->getIndexes})
  1         2  
880             {
881 5 100       9 if($index >= $stop_index)
882             {
883 1         3 return $previous;
884             }
885 4         5 $previous = $index;
886             }
887 0         0 return -1;
888             }
889              
890             sub findNext
891             {
892 1     1 1 2 my ($this,$stop_index) = @_;
893 1         2 my $index;
894 1         3 foreach $index (@{$this->getIndexes})
  1         3  
895             {
896 5 50       9 if($index > $stop_index)
897             {
898 0         0 return $index;
899             }
900             }
901 1         2 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 119 my ($this,$to_append,$pivot,$fh) = @_;
923 52         68 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       61 if (scalar(@{$this->getIndexes}) > 0) {
  52         84  
935 52 100 66     96 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         9 return "DISJUNCTION"; # external disjunction
946             }
947            
948 49 100 100     107 if(
949             ($this->getLast == $to_append->getFirst)
950             ||
951             ($this->getFirst == $to_append->getLast)
952             )
953             {
954 25         74 return "ADJUNCTION";
955             }
956              
957 24 100       48 if($this->getFirst <= $to_append->getFirst)
958             {
959 1 50       3 if($this->getLast >= $to_append->getLast)
960             {
961 1 50       4 if(defined $pivot)
962             {
963              
964 1         3 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       42 if($this->getFirst >= $to_append->getFirst)
992             {
993 23 100       36 if($this->getLast <= $to_append->getLast)
994             {
995 19 50       35 if(defined $pivot)
996             {
997 19         55 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       10 if($this->getLast > $to_append->getLast)
1007             {
1008 4 50       9 if (defined $pivot)
1009             {
1010 4 50       9 if ($pivot == $this->getFirst)
1011             {
1012 0         0 return "INCLUSION";
1013             }
1014             else
1015             {
1016 4         12 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 102 my ($this) = @_;
1080 64         103 my $index;
1081             my $previous;
1082 64         83 my $counter = 0;
1083 64         85 my @gaps;
1084              
1085             # print STDERR "gG1\n";
1086              
1087 64         79 foreach $index (@{$this->getIndexes})
  64         105  
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       316 if (defined $previous) {
1094              
1095 141 100       238 if ($index > $previous+1) {
1096 25         36 my %gap;
1097 25         36 $counter = $previous+1;
1098             # print STDERR "gG3\n";
1099 25         57 while ($counter != $index)
1100             {
1101             # print STDERR "gG3 (counter = $counter)\n";
1102            
1103 35         86 $gap{$counter++} = 0;
1104             }
1105             # print STDERR "gG4\n";
1106 25         60 push @gaps, \%gap;
1107             }
1108             }
1109 205         264 $previous = $index;
1110             }
1111             # print STDERR "gGF\n";
1112              
1113 64         132 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__