1. dict
Creates a new empty dictionary with specified initial capacity.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
0 |
|
1.3. Description
dict creates an empty dictionary with an initial capacity of int elements and pushes it onto the operand stack.
Level 1: The dictionary has a fixed maximum capacity of int elements. Attempting to exceed this causes [dictfull] error.
Level 2: The int operand specifies only the initial capacity; the dictionary can grow automatically beyond that if needed.
The dictionary is allocated in local or global VM according to the current VM allocation mode.
1.5. Examples
5 dict % Create dictionary with capacity for 5 entries
10 dict begin
/name (PostScript) def
/version 2 def
/active true def
end
/config 20 dict def
config /fontSize 12 put
config /fontName /Helvetica put
1.6. Common Use Cases
1.7. Common Pitfalls
Size Matters in Level 1 - In Level 1, choose int large enough for all entries. Exceeding capacity causes [dictfull].
|
% Level 1
2 dict begin
/key1 1 def
/key2 2 def
/key3 3 def % Error: dictfull
end
| Growing Costs in Level 2 - Even in Level 2, growing a dictionary consumes additional VM and has performance costs. |
| Choose Appropriate Size - Estimate needed size to avoid expensive expansion: * Small dictionaries: 5-10 entries * Medium dictionaries: 20-50 entries * Large dictionaries: 100+ entries |
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
int exceeds implementation limits |
[ |
No operand on stack |
[ |
Operand is not an integer |
[ |
Insufficient VM to allocate dictionary |
1.9. Implementation Notes
Level 1: * Fixed capacity set at creation * Cannot exceed int entries * Simple hash table implementation
Level 2: * Initial capacity of int * Auto-expands when needed * Expansion in chunks for efficiency * May contain unused space after expansion
1.10. Performance Considerations
-
Choose initial size wisely to avoid expansion
-
Expansion is expensive (rehashing required)
-
Small initial size OK if final size unknown
-
Large initial size wastes VM if unused