14
14
15
15
#include "bool.h"
16
16
#include "error.h"
17
+ #include "gaputils.h"
17
18
#include "lists.h"
18
19
#include "modules.h"
19
20
#include "plist.h"
@@ -588,18 +589,12 @@ static Obj FuncTzOccurrences(Obj self, Obj args)
588
589
{
589
590
Obj tietze ; /* handle of the Tietze stack */
590
591
Obj rels ; /* handle of the relators list */
591
- Obj * ptRels ; /* pointer to this list */
592
- Obj res ; /* handle of the result */
592
+ const Obj * ptRels ; /* pointer to this list */
593
593
Obj cnts ; /* list of the counts */
594
- Obj * ptCnts ; /* pointer to the counts list */
595
594
Obj mins ; /* list of minimal occurrence list */
596
- Obj * ptMins ; /* pointer to the minimals list */
597
595
Obj lens ; /* list of lengths of those */
598
- Obj * ptLens ; /* pointer to the lengths list */
599
596
Obj rel ; /* handle of a relator */
600
597
Obj * ptRel ; /* pointer to this relator */
601
- Obj aux ; /* auxiliary list */
602
- Int * ptAux ; /* pointer to the lengths list */
603
598
Int numgens ; /* number of Tietze generators */
604
599
Int numrels ; /* number of Tietze relators */
605
600
Int leng ; /* length of a relator */
@@ -623,7 +618,6 @@ static Obj FuncTzOccurrences(Obj self, Obj args)
623
618
624
619
/* get and check the Tietze relators list */
625
620
rels = CheckTietzeRelators (tietze );
626
- ptRels = ADDR_OBJ (rels );
627
621
numrels = LEN_PLIST (rels );
628
622
numgens = INT_INTOBJ (ELM_PLIST (tietze , TZ_NUMGENS ));
629
623
@@ -639,47 +633,14 @@ static Obj FuncTzOccurrences(Obj self, Obj args)
639
633
num = numgens ;
640
634
}
641
635
642
- /* allocate the result lists */
643
- cnts = NEW_PLIST ( T_PLIST , numgens );
644
- SET_LEN_PLIST ( cnts , numgens );
645
- for ( k = 1 ; k <= numgens ; k ++ )
646
- ADDR_OBJ (cnts )[k ] = INTOBJ_INT (0 );
647
-
648
- mins = NEW_PLIST ( T_PLIST , numgens );
649
-
650
- lens = NEW_PLIST ( T_PLIST , numgens );
651
-
652
- res = NEW_PLIST ( T_PLIST , 3 );
653
- SET_LEN_PLIST ( res , 3 );
654
- ADDR_OBJ (res )[1 ] = cnts ;
655
- ADDR_OBJ (res )[2 ] = mins ;
656
- ADDR_OBJ (res )[3 ] = lens ;
657
- CHANGED_BAG (res );
658
-
659
- /* allocate an auxiliary list */
660
- ptAux = 0 ;
661
- if ( numgens > 1 ) {
662
- aux = NEW_STRING ( (numgens + 1 )* sizeof (Int ) );
663
- ptAux = (Int * )ADDR_OBJ (aux );
664
- ptAux [0 ] = numgens ;
665
- for ( k = 1 ; k <= numgens ; k ++ )
666
- ptAux [k ] = 0 ;
667
-
668
- }
669
-
670
- /* now we can safely grab pointers */
671
- ptRels = ADDR_OBJ (rels );
672
- ptCnts = ADDR_OBJ (cnts );
673
- ptLens = ADDR_OBJ (lens );
674
- ptMins = ADDR_OBJ (mins );
675
-
676
636
/* handle special case of single generator in generator list */
677
637
if ( numgens == 1 ) {
678
638
679
639
/* initialize the counters */
680
640
nr = 0 ; nrm = 0 ; min = 0 ;
681
641
682
642
/* loop over all relators */
643
+ ptRels = CONST_ADDR_OBJ (rels );
683
644
for ( i = 1 ; i <= numrels ; i ++ ) {
684
645
rel = ptRels [i ];
685
646
if ( rel == 0 || ! IS_PLIST (rel ) ) {
@@ -710,20 +671,43 @@ static Obj FuncTzOccurrences(Obj self, Obj args)
710
671
}
711
672
712
673
/* put the information into the result bags */
713
- ptCnts [1 ] = INTOBJ_INT ( nr );
714
- if ( nr != 0 ) {
715
- ptCnts [1 ] = INTOBJ_INT ( nr );
716
- SET_LEN_PLIST ( lens , 1 );
717
- SET_ELM_PLIST ( lens , 1 , INTOBJ_INT (nrm ) );
718
- SET_LEN_PLIST ( mins , 1 );
719
- SET_ELM_PLIST ( mins , 1 , INTOBJ_INT (min ) );
674
+ cnts = NewPlistFromArgs (INTOBJ_INT (nr ));
675
+ if (nr != 0 ) {
676
+ lens = NewPlistFromArgs (INTOBJ_INT (nrm ));
677
+ mins = NewPlistFromArgs (INTOBJ_INT (min ));
678
+ }
679
+ else {
680
+ lens = NewEmptyPlist ();
681
+ mins = NewEmptyPlist ();
720
682
}
721
683
}
722
684
723
685
/* handle general case of all Tietze generators */
724
686
else {
725
687
688
+ // allocate the result lists
689
+ cnts = NEW_PLIST (T_PLIST , numgens );
690
+ SET_LEN_PLIST (cnts , numgens );
691
+ for (k = 1 ; k <= numgens ; k ++ )
692
+ ADDR_OBJ (cnts )[k ] = INTOBJ_INT (0 );
693
+
694
+ mins = NEW_PLIST (T_PLIST , numgens );
695
+ lens = NEW_PLIST (T_PLIST , numgens );
696
+
697
+ // allocate an auxiliary list
698
+ Obj aux = NEW_STRING ((numgens + 1 ) * sizeof (Int ));
699
+ Int * ptAux = (Int * )ADDR_OBJ (aux );
700
+ ptAux [0 ] = numgens ;
701
+ for (k = 1 ; k <= numgens ; k ++ )
702
+ ptAux [k ] = 0 ;
703
+
704
+ // now we can safely grab pointers
705
+ Obj * ptCnts = ADDR_OBJ (cnts );
706
+ Obj * ptLens = ADDR_OBJ (lens );
707
+ Obj * ptMins = ADDR_OBJ (mins );
708
+
726
709
/* loop over all relators */
710
+ ptRels = CONST_ADDR_OBJ (rels );
727
711
for ( i = 1 ; i <= numrels ; i ++ ) {
728
712
rel = ptRels [i ];
729
713
if ( rel == 0 || ! IS_PLIST (rel ) ) {
@@ -774,7 +758,7 @@ static Obj FuncTzOccurrences(Obj self, Obj args)
774
758
SET_LEN_PLIST ( lens , k );
775
759
}
776
760
777
- return res ;
761
+ return NewPlistFromArgs ( cnts , mins , lens ) ;
778
762
}
779
763
780
764
0 commit comments