Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
fib.C
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
251# include <stdlib.h>
252# include <iostream>
253# include <tpl_arrayStack.H>
254
255using namespace std;
256using namespace Aleph;
257
258int fib_it(int n)
259{
260 if (n <= 1)
261 return 1;
262
263 int fi_1 = 1, fi_2 = 1, fi = 0, i;
264
265 for (i = 2; i <= n; i++, fi_2 = fi_1, fi_1 = fi)
266 fi = fi_1 + fi_2;
267
268 return fi;
269}
270
271int fib_rec(int n)
272{
273 if (n <= 1)
274 return 1;
275
276 int f1 = fib_rec(n - 1);
277 int f2 = fib_rec(n - 2);
278 return f1 + f2;
279}
280
281
282# define P0 0
283# define P1 1
284# define P2 2
285
286struct Item
287{
288 int n;
289 int f1;
292};
293
294# define N (stack.top().n)
295# define F1 (stack.top().f1)
296# define RESULT (stack.top().result)
297# define RETURN_POINT (stack.top().return_point)
298
299int fib_st(int n)
300{
301 ArrayStack<Item> stack;
304
306
308
309 while (1)
310 {
311 if (N <= 1)
312 {
316 goto exit_from_fib;
317 }
318
319 { /* Esta es la llamada a fib(n - 1) */
322 new_activation_register.return_point = P1;
324 continue;
325 }
326
327 p1: /* Esta es la salida de fib(n - 1) */
329 current_activation_register.f1 = current_activation_register.result; /* int f1 = fib(n - 1) */
331
332 { /* Esta es la llamada a fib(n - 2) */
335 new_activation_register.return_point = P2;
337 continue;
338 }
339
340 p2: /* Esta es la salida de fib(n - 2) */
345
347 if (stack.size() == 1)
348 return RESULT;
349
351 char return_point = current_activation_register.return_point;
352
353 // Propagate result to caller
354 int res = current_activation_register.result;
355 Item caller = stack.pop();
356 caller.result = res;
357 stack.push(caller);
358
359 switch (return_point)
360 {
361 case P1: goto p1;
362 case P2: goto p2;
363 }
364 }
365}
366
367
368
369int main(int argn, char * argv[])
370{
371 if (argn != 2) {
372 cout << "usage: " << argv[0] << " n" << endl;
373 return 1;
374 }
375 int n = atoi(argv[1]);
376
377 cout << "fib(" << n << ") = " << fib_rec(n) << " = " << fib_it(n)
378 << " = " << fib_st(n) << endl;
379}
int main()
Stack implemented with simple dynamic array and with bounds verification.
size_t size() const noexcept
Return the number of elements stored in the stack.
T pop()
Extract the last more recently inserted element.
T & push(const T &data)
Push into stack a copy of data
#define N
Definition fib.C:294
#define RESULT
Definition fib.C:296
int fib_rec(int n)
Definition fib.C:271
#define P1
Definition fib.C:283
int fib_st(int n)
Definition fib.C:299
int fib_it(int n)
Definition fib.C:258
#define P2
Definition fib.C:284
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Definition fib.C:287
int result
Definition fib.C:290
int f1
Definition fib.C:289
char return_point
Definition fib.C:291
int n
Definition fib.C:288
Stack implementations backed by dynamic or fixed arrays.