1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
#############################################################################
##
#W sparselistsorted.gi GAP library Steve Linton
##
##
#Y Copyright (C) 1996, Lehrstuhl D für Mathematik, RWTH Aachen, Germany
#Y (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland
#Y Copyright (C) 2002 The GAP Group
##
## This file contains a specific representation of sparse lists and
## associated methods
##
#############################################################################
##
#R IsSparseListBySortedListRep( <sl> )
##
## This is a positional representation with 4 components, the length,
## the default value a sorted list of positions with non-default values
## and the values for those positions, in the corresponding order
##
## The lists must be homogenous, and will only tolerate assignment of
## elements in the proper family. The user must ensure that mutable elements
## do not change family
##
DeclareRepresentation( "IsSparseListBySortedListRep", IsPositionalObjectRep, 4);
SL_LENGTH := 1;
SL_DEFAULT := 2;
SL_POSS := 3;
SL_VALS := 4;
#############################################################################
##
#M Length( <sl> )
##
InstallMethod(Length, "for sparse list by sorted list", [IsSparseListBySortedListRep and IsList],
v->v![SL_LENGTH]);
#############################################################################
##
#M SparseStructureOfList( <sl> )
##
InstallMethod( SparseStructureOfList, "sparse list by sorted list",
[IsSparseListBySortedListRep and IsList],
sl -> [ sl![SL_DEFAULT], sl![SL_POSS], sl![SL_VALS]]);
#############################################################################
##
#M <sl> [ <pos> ] -- list element access, bypassing the generic function
##
InstallMethod(\[\], "access to SLbySL (shortcut)",
[IsSparseListBySortedListRep and IsList, IsPosInt],
function(v,i)
local p;
if i > v![SL_LENGTH] then
Error("Position ",i," must have a bound value");
else
p := PositionSet(v![SL_POSS],i);
if p = fail then
return v![SL_DEFAULT];
else
return v![SL_VALS][p];
fi;
fi;
end);
#############################################################################
##
#F SparseListBySortedListNC( <poss>, <vals>, <length>, <default> )
##
## This is the main constructor, responsible for assembling the data structure
## (easy) and assigning the appropriate type (harder)
##
## All lists in this representation know their length, of course, and
## so know if they are finite and/or small. Otherwise, we just need
## to sort out whether we are a table
##
InstallGlobalFunction(SparseListBySortedListNC, function(poss, vals, length, default)
local filt, fam, type, l;
filt := IsMutable and IsList and IsDenseList and IsSparseList and
IsListDefault
and IsSparseListBySortedListRep and HasLength and
HasIsSmallList and HasIsFinite and IsHomogeneousList and IsCollection;
if length = 0 then
Error("Don't like empty sparse lists");
fi;
if length <= MAX_SIZE_LIST_INTERNAL then
filt := filt and IsSmallList;
fi;
if length < infinity then
filt := filt and IsFinite;
fi;
if IsList(default) then
filt := filt and IsTable;
fi;
fam := CollectionsFamily(FamilyObj(default));
type := NewType( fam, filt);
l := [];
l[SL_LENGTH] := length;
l[SL_DEFAULT] := default;
l[SL_POSS] := poss;
l[SL_VALS] := vals;
Objectify( type, l );
Assert(1,Length(poss) = Length(vals));
Assert(1,IsEmpty(poss) or IsSSortedList(poss));
Assert(1,IsEmpty(poss) or IsPositionsList(poss));
Assert(1,IsEmpty(poss) or poss[Length(poss)] <= length);
Assert(1,IsEmpty(vals) or (IsHomogeneousList(vals) and FamilyObj(vals) = fam));
return l;
end);
#############################################################################
##
#F SparseListBySortedList( <poss>, <vals>, <length>, <default> )
##
## This is the checking version of the constructor
##
InstallGlobalFunction(SparseListBySortedList, function(poss, vals, length, default)
local p;
if length <> infinity and (not IsInt(length) or length < 0) then
Error("SparseListBySortedList: Impossible list length");
fi;
if Length(poss) <> Length(vals) then
Error("SparseListBySortedList: Unequal numbers of positions and values");
fi;
if Length(poss) > 0 then
if not IsPositionsList(poss) or not IsSSortedList(poss) or
poss[Length(poss)] > length then
Error("SparseListBySortedList: bad list of exception positions");
fi;
if not IsDenseList(vals) then
Error("SparseListBySortedList: hole in list of exception values");
fi;
if not IsHomogeneousList(vals) or FamilyObj(default) <>
FamilyObj(vals[1]) then
Error("SparseListBySortedList: list must be homogeneous");
fi;
poss := ShallowCopy(poss);
vals := ShallowCopy(vals);
p := Position(vals, default);
while p <> fail do
Unbind(vals[p]);
Unbind(poss[p]);
p := Position(vals, default, p);
od;
fi;
#
# This cleans up after removal of defaults
#
poss := Compacted(poss);
vals := Compacted(vals);
return SparseListBySortedListNC( poss, vals, length, default);
end);
#############################################################################
##
#M ShallowCopy( <sl> )
##
InstallMethod(ShallowCopy, "sparse list by sorted list",
[IsSparseListBySortedListRep and IsDenseList],
function(sl)
return SparseListBySortedListNC( ShallowCopy(sl![SL_POSS]),
ShallowCopy(sl![SL_VALS]),sl![SL_LENGTH],
sl![SL_DEFAULT]);
end);
#############################################################################
##
#M <sl>[<pos>] := <obj>
##
## This is a little complicated because of the various cases, and
## the possible need to adjust the type
##
InstallMethod( \[\]\:\=, "sparse list by sorted list", IsCollsXElms,
[ IsSparseListBySortedListRep and IsList and IsMutable,
IsPosInt, IsObject ],
function(sl, pos, obj)
local poss, vals, p, l, newfilt;
#
# If the resulting list will have a hole then we have to make it a
# plain list
#
if sl![SL_LENGTH] <> infinity and pos > sl![SL_LENGTH] + 1 then
PLAIN_SL( sl );
sl[pos] := obj;
return;
fi;
#
# Otherwise we stay sparse. There are four cases according to
# whether the list currently has a non-default entry in that position
# and whether the value to be assigned is the default. In one case
# there is nothing to do.
#
#
# First adjust the length
#
if sl![SL_LENGTH] <> infinity and pos = sl![SL_LENGTH] + 1 then
sl![SL_LENGTH] := pos;
if pos > MAX_SIZE_LIST_INTERNAL then
ResetFilterObj( sl, IsSmallList );
SetFilterObj(sl, HasIsSmallList);
fi;
fi;
#
# Now actually change the list
#
poss := sl![SL_POSS];
vals := sl![SL_VALS];
p := PositionSorted( poss, pos);
if p <= Length (poss) and poss[p] = pos then
if obj = sl![SL_DEFAULT] then
#
# Case 1 default replacing a non-default
#
l := Length(vals);
RemoveSet(poss, pos);
vals{[p..l-1]} := vals{[p+1..l]};
Unbind(poss[l]);
Unbind(vals[l]);
else
#
# Case 2 non-default replacing non-default
#
vals[p] := obj;
fi;
else
if obj <> sl![SL_DEFAULT] then
#
# Case 3: non-default replacing default
#
AddSet(poss, pos);
l := Length(vals);
vals{[l+1,l..p+1]} := vals{[l,l-1..p]};
vals[p] := obj;
fi;
fi;
return;
end);
#############################################################################
##
#M Unbind( <sl> [ <pos> ] )
##
## Note that this is NOT the way to set an entry back to the default
## unless you are unbinding the last entry, this will always make
## a plain list.
##
InstallMethod( Unbind\[\], "sparse list by sorted list",
[IsSparseListBySortedListRep and IsMutable and IsList, IsPosInt],
function( sl, pos)
local poss;
if pos > sl![SL_LENGTH] then
return;
fi;
if pos < sl![SL_LENGTH] then
PLAIN_SL(sl);
Unbind(sl[pos]);
return;
fi;
poss := sl![SL_POSS];
if Length(poss) > 0 and poss[Length(poss)] = pos then
Unbind(sl![SL_VALS][Length(poss)]);
Unbind(poss[Length(poss)]);
fi;
sl![SL_LENGTH] := pos-1;
if pos -1 <= MAX_SIZE_LIST_INTERNAL then
SetFilterObj( sl, IsSmallList );
fi;
return;
end);
InstallMethod( ListOp, "sparse list by sorted list",
[ IsSparseListBySortedListRep and IsList, IsFunction],
function(sl,func)
return SparseListBySortedListNC( sl![SL_POSS],
List(sl![SL_VALS], func),
sl![SL_LENGTH],
func(sl![SL_DEFAULT]));
end);
InstallMethod(Append, "two compatible sparse sorted lists",
[ IsSparseListBySortedListRep and IsList and IsMutable and IsFinite,
IsSparseList],
function( sl1, sl2)
local len1, ss, poss, poss2, i;
ss := SparseStructureOfList( sl2);
if not IsBound(ss[1]) or sl1![SL_DEFAULT] <> ss[1] then
TryNextMethod();
fi;
len1 := sl1![SL_LENGTH];
if Length(sl2) = infinity then
sl1![SL_LENGTH] := infinity;
else
sl1![SL_LENGTH] := len1 + Length(sl2);
fi;
Append(sl1![SL_VALS], ss[3]);
poss := sl1![SL_POSS];
# This ShallowCopy is needed in the case where sl1 and sl2 are identical
poss2 := ShallowCopy(ss[2]);
for i in poss2 do
Add(poss, i + len1);
od;
SortParallel( poss, sl1![SL_VALS]);
if not IsHomogeneousList(sl2) then
ResetFilterObj(sl1, IsHomogeneousList);
ResetFilterObj(sl1, IsCollection);
fi;
if sl1![SL_LENGTH] > MAX_SIZE_LIST_INTERNAL then
ResetFilterObj(sl1, IsSmallList);
SetFilterObj(sl1, HasIsSmallList);
fi;
end);
InstallMethod(Permuted, "sparse list", [IsSparseListBySortedListRep
and IsSparseList, IsPerm],
function( sl, p)
local poss, vals;
if LargestMovedPointPerm(p) > sl![SL_LENGTH] then
Error("Permuted: Permutation moves too many points");
fi;
poss := OnTuples(sl![SL_POSS], p);
vals := ShallowCopy(sl![SL_VALS]);
SortParallel( poss, vals);
return SparseListBySortedListNC( poss, vals, sl![SL_LENGTH], sl![SL_DEFAULT]);
end);
InstallMethod( FilteredOp, "sparse list", [IsSparseListBySortedListRep
and IsSparseList, IsFunction],
function(sl, filt)
local skipped, iposs, oposs, ivals, ovals, i, newlen;
if filt(sl![SL_DEFAULT]) then
skipped := 0;
iposs := sl![SL_POSS];
oposs := [];
ivals := sl![SL_VALS];
ovals := [];
for i in [1..Length(iposs)] do
if filt(ivals[i]) then
Add(oposs,iposs[i]-skipped);
Add(ovals,ivals[i]);
else
skipped := skipped+1;
fi;
od;
if sl![SL_LENGTH] = infinity then
newlen := infinity;
else
newlen := sl![SL_LENGTH] - skipped;
fi;
return SparseListBySortedListNC( oposs, ovals,
sl![SL_DEFAULT], newlen);
else
return Filtered(sl![SL_VALS], filt);
fi;
end);
InstallMethod( ELMS_LIST, "sparse list", [IsSparseListBySortedListRep
and IsSparseList, IsDenseList],
function( sl, poss )
local iposs, oposs, ivals, ovals, i, pos;
Assert(1,IsPositionsList(poss));
iposs := sl![SL_POSS];
oposs := [];
ivals := sl![SL_VALS];
ovals := [];
for i in [1..Length(poss)] do
pos := PositionSet( iposs, poss[i]);
if pos <> fail then
Add(oposs, i);
Add(ovals, ivals[pos]);
fi;
od;
return SparseListBySortedListNC( oposs, ovals, Length(poss), sl![SL_DEFAULT]);
end);
InstallMethod(PositionNot, [IsSparseListBySortedListRep and
IsSparseList, IsObject, IsInt],
function(sl, obj, from)
local poss,p,l, isdef,vals;
poss := sl![SL_POSS];
vals := sl![SL_VALS];
p := PositionSorted(poss, from + 1);
l := Length(poss);
isdef := sl![SL_DEFAULT] = obj;
if p <= l and poss[p] > from + 1 then
if not isdef then
return from+1;
fi;
fi;
while p <= l do
if vals[p] <> obj then
return poss[p];
fi;
if isdef or p+1 < l and poss[p+1] = poss[p]+1 then
p := p+1;
else
return poss[p]+1;
fi;
od;
if not isdef then
if l = 0 then
return 1;
else
return poss[l]+1;
fi;
else
return sl![SL_LENGTH]+1;
fi;
end);
|