Skip to content

Commit 3683acb

Browse files
committed
tietze.c: refactor FuncTzOccurrences
1 parent 9cf3837 commit 3683acb

File tree

1 file changed

+34
-50
lines changed

1 file changed

+34
-50
lines changed

src/tietze.c

+34-50
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "bool.h"
1616
#include "error.h"
17+
#include "gaputils.h"
1718
#include "lists.h"
1819
#include "modules.h"
1920
#include "plist.h"
@@ -588,18 +589,12 @@ static Obj FuncTzOccurrences(Obj self, Obj args)
588589
{
589590
Obj tietze; /* handle of the Tietze stack */
590591
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 */
593593
Obj cnts; /* list of the counts */
594-
Obj * ptCnts; /* pointer to the counts list */
595594
Obj mins; /* list of minimal occurrence list */
596-
Obj * ptMins; /* pointer to the minimals list */
597595
Obj lens; /* list of lengths of those */
598-
Obj * ptLens; /* pointer to the lengths list */
599596
Obj rel; /* handle of a relator */
600597
Obj * ptRel; /* pointer to this relator */
601-
Obj aux; /* auxiliary list */
602-
Int * ptAux; /* pointer to the lengths list */
603598
Int numgens; /* number of Tietze generators */
604599
Int numrels; /* number of Tietze relators */
605600
Int leng; /* length of a relator */
@@ -623,7 +618,6 @@ static Obj FuncTzOccurrences(Obj self, Obj args)
623618

624619
/* get and check the Tietze relators list */
625620
rels = CheckTietzeRelators(tietze);
626-
ptRels = ADDR_OBJ(rels);
627621
numrels = LEN_PLIST(rels);
628622
numgens = INT_INTOBJ(ELM_PLIST(tietze, TZ_NUMGENS));
629623

@@ -639,47 +633,14 @@ static Obj FuncTzOccurrences(Obj self, Obj args)
639633
num = numgens;
640634
}
641635

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-
676636
/* handle special case of single generator in generator list */
677637
if ( numgens == 1 ) {
678638

679639
/* initialize the counters */
680640
nr = 0; nrm = 0; min = 0;
681641

682642
/* loop over all relators */
643+
ptRels = CONST_ADDR_OBJ(rels);
683644
for ( i = 1; i <= numrels; i++ ) {
684645
rel = ptRels[i];
685646
if ( rel == 0 || ! IS_PLIST(rel) ) {
@@ -710,20 +671,43 @@ static Obj FuncTzOccurrences(Obj self, Obj args)
710671
}
711672

712673
/* 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();
720682
}
721683
}
722684

723685
/* handle general case of all Tietze generators */
724686
else {
725687

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+
726709
/* loop over all relators */
710+
ptRels = CONST_ADDR_OBJ(rels);
727711
for ( i = 1; i <= numrels; i++ ) {
728712
rel = ptRels[i];
729713
if ( rel == 0 || ! IS_PLIST(rel) ) {
@@ -774,7 +758,7 @@ static Obj FuncTzOccurrences(Obj self, Obj args)
774758
SET_LEN_PLIST( lens, k );
775759
}
776760

777-
return res;
761+
return NewPlistFromArgs(cnts, mins, lens);
778762
}
779763

780764

0 commit comments

Comments
 (0)