Skip to content

Commit e8aec4e

Browse files
committed
Add size(BasicList) to get capacity
1 parent bd50b14 commit e8aec4e

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

M2/Macaulay2/d/actors5.d

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,13 @@ examine(e:Expr):Expr := (
528528
else WrongArg("(), a function, a symbol, or a basic list"));
529529
setupfun("examine",examine);
530530

531+
size(e:Expr):Expr := (
532+
when e
533+
is x:Sequence do toExpr(length(x))
534+
is x:List do toExpr(x.capacity)
535+
else WrongArg("a list or sequence"));
536+
setupfun("size0", size);
537+
531538
numparms(e:Expr):Expr := (
532539
when e
533540
is fc:FunctionClosure do toExpr(if fc.model.desc.restargs then -1 else fc.model.desc.numparms)

M2/Macaulay2/m2/lists.m2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ needs "methods.m2"
1111
MutableList.synonym = "mutable list"
1212
AngleBarList.synonym = "angle bar list"
1313

14+
size BasicList := ZZ => size0
15+
1416
List ? List := (s,t) -> if class s === class t then toSequence s ? toSequence t else (class s) ? (class t)
1517
Option ? Option := (s,t) -> toSequence s ? toSequence t
1618

M2/Macaulay2/packages/Macaulay2Doc/doc_rings.m2

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -294,16 +294,36 @@ document {
294294
SeeAlso => {IndexedVariable}
295295
}
296296

297-
document {
298-
Key => {size, (size, RingElement)},
299-
Headline => "the size of an object",
300-
TT "size x", " -- returns the size of ", TT "x", " which usually gives
301-
a rough indication of memory space required to store the object ", TT "x", ".",
302-
PARA{},
303-
"For a polynomial, the size is the number of terms.",
304-
PARA{},
305-
"This function should be replaced by something more generally useful."
306-
}
297+
doc ///
298+
Key
299+
size
300+
(size, RingElement)
301+
(size, BasicList)
302+
Usage
303+
size x
304+
Headline
305+
the size of an object
306+
Description
307+
Text
308+
@M2CODE "size x"@ returns the size of @VAR "x"@, which usually gives
309+
a rough indication of memory space required to store it.
310+
311+
For a polynomial, the size is the number of terms.
312+
Example
313+
R = QQ[x,y,z,w]
314+
f = z^2 - y*w
315+
size f
316+
Text
317+
For lists, @M2CODE "size"@ reports the capacity of the underlying
318+
sequence, which may exceed the number of elements when the list is
319+
mutable. This reflects the amount of memory currently allocated to allow
320+
efficient growth without reallocating on every insertion.
321+
Example
322+
x = new MutableList
323+
x#5 = 2;
324+
#x
325+
size x
326+
///
307327

308328
document {
309329
Key => {

0 commit comments

Comments
 (0)