1. maxlength
Returns the maximum capacity of a dictionary.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
0 |
|
1.3. Description
maxlength returns the capacity of dict - the maximum number of key-value pairs it can hold using the VM currently allocated to it.
Level 1: Returns the int operand originally passed to dict - the dictionary’s fixed maximum capacity.
Level 2: Returns the current capacity, which may be larger than the original size if the dictionary has grown.
1.5. Examples
/mydict 5 dict def
mydict length % Returns 0 (empty)
mydict maxlength % Returns 5 (capacity)
2 dict begin
/k1 1 def
/k2 2 def
/k3 3 def % Auto-expands in Level 2
currentdict maxlength % Returns > 2
end
mydict dup maxlength
exch length sub % Available slots
1.6. Common Use Cases
1.6.1. Pre-allocation Check
dict dup maxlength exch length sub
requiredSlots lt {
% Need bigger dictionary
dup length requiredSlots add dict
copy
} if
1.7. Common Pitfalls
5 dict
dup length % 0 (no entries yet)
maxlength % 5 (capacity)
| Level Differences - Behavior differs between Level 1 and 2: |
% Level 1: Fixed at creation
% Level 2: Can grow (returns current capacity)
| Don’t Rely on Exact Values - In Level 2, maxlength may increase unpredictably due to automatic expansion. |
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Dictionary has no-access attribute |
[ |
No operand on stack |
[ |
Operand is not a dictionary |
1.9. Implementation Notes
Level 1: * Returns fixed capacity set at creation * Never changes * Simple field lookup
Level 2: * Returns current allocated capacity * May be larger than original size * Capacity increases in chunks when dictionary grows