Skip to content

Commit

Permalink
Add RequireMutable and use in various places
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJefferson authored and wilfwilson committed Apr 7, 2019
1 parent 6f1661e commit 3d6f4d7
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 52 deletions.
8 changes: 8 additions & 0 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ Obj RequireArgumentEx(const char * funcname,
RequireArgumentCondition(funcname, op, op == True || op == False, \
"must be true or false")

/****************************************************************************
**
*F RequireMutable
*/
#define RequireMutable(funcname, op, type) \
RequireArgumentCondition(funcname, op, IS_MUTABLE_OBJ(op), \
"must be a mutable " type)


/****************************************************************************
**
Expand Down
3 changes: 1 addition & 2 deletions src/listfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ static Obj FuncAPPEND_LIST_INTR(Obj self, Obj list1, Obj list2)
Int i; /* loop variable */

/* check the mutability of the first argument */
if (!IS_MUTABLE_OBJ(list1))
ErrorMayQuit("Append: <list1> must be a mutable list", 0, 0);
RequireMutable("Append", list1, "list");


/* handle the case of strings now */
Expand Down
15 changes: 6 additions & 9 deletions src/lists.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,24 +863,21 @@ void ASSB_LIST (
DoOperation3Args( AssListOper, list, pos, obj );
}

void ASS2_LIST(Obj list, Obj pos1, Obj pos2, Obj obj)
void ASS2_LIST(Obj mat, Obj pos1, Obj pos2, Obj obj)
{
if (!IS_MUTABLE_OBJ(list)) {
ErrorMayQuit("Matrix Assignment: <mat> must be a mutable matrix", 0,
0);
}
if (IS_POS_INTOBJ(pos1) && IS_POS_INTOBJ(pos2) && IS_PLIST(list)) {
RequireMutable("Matrix Assignment", mat, "matrix");
if (IS_POS_INTOBJ(pos1) && IS_POS_INTOBJ(pos2) && IS_PLIST(mat)) {
Int p1 = INT_INTOBJ(pos1);
if ( p1 <= LEN_PLIST(list) ) {
Obj row = ELM_PLIST( list, p1 );
if (p1 <= LEN_PLIST(mat)) {
Obj row = ELM_PLIST(mat, p1);
Int p2 = INT_INTOBJ(pos2);

ASS_LIST( row, p2, obj );
return;
}
}

DoOperation4Args( AssListOper, list, pos1, pos2, obj );
DoOperation4Args(AssListOper, mat, pos1, pos2, obj);
}


Expand Down
49 changes: 12 additions & 37 deletions src/vecgf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1845,11 +1845,7 @@ static Obj FuncELMS_GF2VEC(Obj self, Obj list, Obj poss)
static Obj FuncASS_GF2VEC(Obj self, Obj list, Obj pos, Obj elm)
{
// check that <list> is mutable
if (!IS_MUTABLE_OBJ(list)) {
ErrorReturnVoid("List Assignment: <list> must be a mutable list", 0L,
0L, "you can 'return;' and ignore the assignment");
return 0;
}
RequireMutable("List Assignment", list, "list");

// get the position
UInt p = GetSmallInt("ASS_GF2VEC", pos);
Expand Down Expand Up @@ -1913,11 +1909,7 @@ static Obj FuncPLAIN_GF2MAT(Obj self, Obj list)
static Obj FuncASS_GF2MAT(Obj self, Obj list, Obj pos, Obj elm)
{
// check that <list> is mutable
if (!IS_MUTABLE_OBJ(list)) {
ErrorReturnVoid("List Assignment: <list> must be a mutable list", 0L,
0L, "you can 'return;' and ignore the assignment");
return 0;
}
RequireMutable("List Assignment", list, "list");

// get the position
UInt p = GetSmallInt("ASS_GF2MAT", pos);
Expand Down Expand Up @@ -1984,11 +1976,7 @@ static Obj FuncELM_GF2MAT(Obj self, Obj mat, Obj row)
static Obj FuncUNB_GF2VEC(Obj self, Obj list, Obj pos)
{
// check that <list> is mutable
if (!IS_MUTABLE_OBJ(list)) {
ErrorReturnVoid("List Unbind: <list> must be a mutable list", 0L, 0L,
"you can 'return;' and ignore the unbind");
return 0;
}
RequireMutable("List Unbind", list, "vector");

if (DoFilter(IsLockedRepresentationVector, list) == True) {
ErrorReturnVoid("Unbind forbidden on locked GF2 vector", 0L, 0L,
Expand Down Expand Up @@ -2027,11 +2015,7 @@ static Obj FuncUNB_GF2VEC(Obj self, Obj list, Obj pos)
static Obj FuncUNB_GF2MAT(Obj self, Obj list, Obj pos)
{
// check that <list> is mutable
if (!IS_MUTABLE_OBJ(list)) {
ErrorReturnVoid("List Unbind: <list> must be a mutable list", 0L, 0L,
"you can 'return;' and ignore the unbind");
return 0;
}
RequireMutable("List Unbind", list, "matrix");

// get the position
UInt p = GetSmallInt("UNB_GF2MAT", pos);
Expand Down Expand Up @@ -2532,8 +2516,8 @@ static Obj FuncCOPY_SECTION_GF2VECS(
if (ihowmany < 0 ||
ifrom + ihowmany - 1 > lens || ito + ihowmany - 1 > lend)
ErrorMayQuit("Bad argument values", 0, 0);
if (!IS_MUTABLE_OBJ(dest))
ErrorMayQuit("Immutable destination vector", 0, 0);
RequireMutable("COPY_SECTION_GF2VECS", dest, "vector");

CopySection_GF2Vecs(src, dest, (UInt)ifrom, (UInt)ito, (UInt)ihowmany);
return (Obj)0;
}
Expand Down Expand Up @@ -3381,11 +3365,8 @@ static void ResizeGF2Vec(Obj vec, UInt newlen)
static Obj FuncRESIZE_GF2VEC(Obj self, Obj vec, Obj newlen)
{
Int newlen1;
if (!IS_MUTABLE_OBJ(vec)) {
ErrorReturnVoid("RESIZE_GF2VEC: the vector must be mutable", 0, 0,
"you may 'return;' to skip the operation");
return (Obj)0;
}
RequireMutable("RESIZE_GF2VEC", vec, "vector");

newlen1 = GetNonnegativeSmallInt("RESIZE_GF2VEC", newlen);
ResizeGF2Vec(vec, newlen1);
return (Obj)0;
Expand Down Expand Up @@ -3445,11 +3426,8 @@ static void ShiftLeftGF2Vec(Obj vec, UInt amount)
static Obj FuncSHIFT_LEFT_GF2VEC(Obj self, Obj vec, Obj amount)
{
Int amount1;
if (!IS_MUTABLE_OBJ(vec)) {
ErrorReturnVoid("SHIFT_LEFT_GF2VEC: the vector must be mutable", 0, 0,
"you may 'return;' to skip the operation");
return (Obj)0;
}
RequireMutable("SHIFT_LEFT_GF2VEC", vec, "vector");

amount1 = GetNonnegativeSmallInt("SHIFT_LEFT_GF2VEC", amount);
ShiftLeftGF2Vec(vec, amount1);
return (Obj)0;
Expand Down Expand Up @@ -3513,11 +3491,8 @@ static void ShiftRightGF2Vec(Obj vec, UInt amount)
static Obj FuncSHIFT_RIGHT_GF2VEC(Obj self, Obj vec, Obj amount)
{
Int amount1;
if (!IS_MUTABLE_OBJ(vec)) {
ErrorReturnVoid("SHIFT_RIGHT_GF2VEC: the vector must be mutable", 0,
0, "you may 'return;' to skip the operation");
return (Obj)0;
}
RequireMutable("SHIFT_RIGHT_GF2VEC", vec, "vector");

amount1 = GetNonnegativeSmallInt("SHIFT_RIGHT_GF2VEC", amount);
ShiftRightGF2Vec(vec, amount1);
return (Obj)0;
Expand Down
2 changes: 1 addition & 1 deletion tst/testinstall/kernel/listfunc.tst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Error, Remove: <list> must not be empty

#
gap> APPEND_LIST_INTR(fail, fail);
Error, Append: <list1> must be a mutable list
Error, Append: <list1> must be a mutable list (not the value 'fail')
gap> APPEND_LIST_INTR(rec(), fail);
Error, AppendList: <list1> must be a small list (not a record (plain))

Expand Down
4 changes: 2 additions & 2 deletions tst/testinstall/listindex.tst
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ gap> l := [];; Append(l,l); l;
gap> l := [1,2,3,4];; Append(l,l); l;
[ 1, 2, 3, 4, 1, 2, 3, 4 ]
gap> Append(Immutable([1,2,3]), [1,2,3]);
Error, Append: <list1> must be a mutable list
Error, Append: <list1> must be a mutable list (not a list (plain,cyc,imm))
gap> Append([1,2,3], () );
Error, AppendList: <list2> must be a small list (not a permutation (small))
gap> Append( () , [1,2,3] );
Error, Append: <list1> must be a mutable list
Error, Append: <list1> must be a mutable list (not a permutation (small))
gap> s;
[ ]

Expand Down
9 changes: 8 additions & 1 deletion tst/testinstall/matblock.tst
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,15 @@ gap> tmp = MatrixByBlockMatrix(m2);
true

# block matrices are immutable
#@if IsHPCGAP
gap> m1[1,2] := 5;
Error, Matrix Assignment: <mat> must be a mutable matrix
Error, Matrix Assignment: <mat> must be a mutable matrix (not a atomic compone\
nt object)
#@else
gap> m1[1,2] := 5;
Error, Matrix Assignment: <mat> must be a mutable matrix (not a object (compon\
ent))
#@fi

#
gap> STOP_TEST( "matblock.tst", 1);

0 comments on commit 3d6f4d7

Please sign in to comment.