Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/queue-definition.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / queue-definition.wiki`
137 lines ยท 2408 bytes Raw Blame History
1 =={{header|Omegese}}==
2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 text tpush "push "
10 text tpop "pop "
11 text tempty "empty: "
12 text tyes "yes"
13 text tno "no"
14 text terr "pop from an empty queue: error"
15 global ring long 8
16 global head ulong 1
17 global cnt ulong 1
18 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
19 fn emit a view uchar * to void
20 o = ulong 0
21 while lt o len a bound 4096
22 k = write 1 a o sub len a o
23 if le k 0
24 exit 1
25 end
26 o = add o ulong k
27 end
28 end
29 note emitu writes n in decimal; 20 digits hold every ulong
30 fn emitu n ulong to void
31 d = new uchar 20 0
32 i = ulong 20
33 v = n
34 while true bound 20
35 i = sub i 1
36 put d i uchar add rem v 10 48
37 v = div v 10
38 if eq v 0
39 break
40 end
41 end
42 emit part d i sub 20 i
43 end
44 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
45 fn emiti n long to void
46 if lt n 0
47 emit minus
48 emitu add bitcast ulong not n 1
49 else
50 emitu ulong n
51 end
52 end
53 note a FIFO queue in a ring of 8 slots: cnt elements starting at ring[head]; push writes behind the
54 note last one, pop takes ring[head]; a full ring stops the program (exit 2)
55 fn push v long to void
56 c = get cnt 0
57 if eq c 8
58 exit 2
59 end
60 put ring rem add get head 0 c 8 v
61 put cnt 0 add c 1
62 emit tpush
63 emiti v
64 emit nl
65 end
66 note pop returns false, and prints the error, when the queue is empty
67 fn pop to bool
68 c = get cnt 0
69 if eq c 0
70 emit terr
71 emit nl
72 ret false
73 end
74 h = get head 0
75 emit tpop
76 emiti get ring h
77 emit nl
78 put head 0 rem add h 1 8
79 put cnt 0 sub c 1
80 ret true
81 end
82 fn empty to void
83 emit tempty
84 if eq get cnt 0 0
85 emit tyes
86 else
87 emit tno
88 end
89 emit nl
90 end
91 note the ring wraps: 6 pushes, 4 pops and 5 more pushes cross slot 7 back to slot 0
92 fn main to int
93 empty
94 for i ulong 6
95 push mul long add i 1 11
96 end
97 for i ulong 4
98 ok = pop
99 end
100 for i ulong 5
101 push mul long add i 7 -11
102 end
103 empty
104 note 7 elements remain, so the 8th pop finds the queue empty and ends the loop
105 while pop bound 9
106 end
107 empty
108 ret 0
109 end
110
111 {{out}}
112
113 empty: yes
114 push 11
115 push 22
116 push 33
117 push 44
118 push 55
119 push 66
120 pop 11
121 pop 22
122 pop 33
123 pop 44
124 push -77
125 push -88
126 push -99
127 push -110
128 ...
129 pop 66
130 pop -77
131 pop -88
132 pop -99
133 pop -110
134 pop -121
135 pop from an empty queue: error
136 empty: yes
137