LFL and LFU — LIFO Stack
LFL and LFU in rungs.dev build a last-in first-out stack in an array — push a value on top, take the newest one back off. Use them for undo histories and nesting.
LFL
Source
source
Array
array
Control
control
Length
length
Position
position
Puts a value on top of a stack on each rising edge, and stops once the stack holds .LEN of them.
LFU
Array
array
Destination
destination
Control
control
Length
length
Position
position
Takes the newest value off the top of a stack on each rising edge, leaving the others where they are.
What a LIFO is
LIFO means last in, first out — a stack, like a pile of plates. The last value you put on is the first one you take back off.
Reach for a stack when the most recent thing matters most: the last alarm raised, the step you were on before this one, an undo history.
LFL and LFU are the FFL and FFU of a stack. Everything about them is the
same — the operands, the rising edge, the shared CONTROL
tag, .DN for full and .EM for empty — except which element the unload takes.
Operands
LFL
| Name | Type | Notes |
|---|---|---|
source | numeric | The value to push on. A tag or a number |
array | numeric[] | Where the stack starts — Buf or Buf[2] |
control | CONTROL | The stack's state |
length | number | How many slots the stack has |
position | number | How many are in use at start-up. Usually 0 |
LFU
| Name | Type | Notes |
|---|---|---|
array | numeric[] | Where the stack starts |
destination | numeric | Where the popped value goes |
control | CONTROL | The same control tag the LFL uses |
length | number | Same as the LFL's |
position | number | Same as the LFL's |
The array can be DINT, INT, SINT or REAL, and a value of a different type converts on
the way in and out.
How It Works
LFL is FFL exactly: it writes at .POS and moves .POS up one.
LFU differs in one line. Where FFU takes the element at the start and shifts everything
down, LFU takes the element at .POS - 1 — the top of the stack — zeroes that slot, and
leaves every other element where it is.
| Instruction | Takes the element at | Shifts the others? |
|---|---|---|
FFU | the start | Yes, down one |
LFU | .POS - 1 | No |
That is the whole difference, and it is why LFU is the faster of the two on a long buffer:
it moves one element instead of all of them.
Example — Last Alarm First
Every alarm is pushed on; Acknowledge clears the newest one first.
0
AlarmRaised
LFL
Source
AlarmCode
Array
Stack
Control
SCtl
Length
8
Position
0
1
Acknowledge
LFU
Array
Stack
Destination
TopAlarm
Control
SCtl
Length
8
Position
0
2
SCtl.EM
AllClear
Common Mistakes
- Expecting
LFUto shift the array. It does not — only the element it took is cleared. - Mixing a
LFLwith anFFUon one control tag. That is a queue and a stack disagreeing about which end is which. - Everything on the
FFLandFFUlist applies here too.
Related
- FFL and FFU — a queue instead of a stack
- Array instructions overview — the
CONTROLmembers - RES — empties the stack by clearing
.POS
FFL and FFU — FIFO Queue
FFL and FFU in rungs.dev build a first-in first-out queue in an array — load a value at one end, take the oldest out at the other. Use them to track parts waiting their turn.
COP and CPS — Copy
COP and CPS in rungs.dev copy a run of elements from one array into another. Use them to snapshot a buffer, replicate a value, or move a batch of readings in one rung.