TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
MacStack.h
1/****************************************************************************
2* Copyright (c) 2024, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15#ifndef MacStack_H
16#define MacStack_H
17
18#define declare_stack(_TYPE_) \
19 class STACK(_TYPE_) { \
20 public : \
21 STACK(_TYPE_)(const int t=10); \
22 ~STACK(_TYPE_)(); \
23 int push(const _TYPE_&); \
24 _TYPE_& pop(); \
25 _TYPE_& peek(); \
26 int isEmpty() const; \
27 _TYPE_* getBase() { return base; } \
28 private: \
29 _TYPE_ *top ; \
30 _TYPE_ *base ; \
31 int taille ; \
32 };
33
34#define declare_pstack(_TYPE_) \
35 class PSTACK(_TYPE_) { \
36 public : \
37 PSTACK(_TYPE_)(const int t=10); \
38 ~PSTACK(_TYPE_)(); \
39 int push(_TYPE_*&); \
40 _TYPE_*& pop(); \
41 _TYPE_*& peek(); \
42 int isEmpty() const; \
43 _TYPE_** getBase() { return base; } \
44 private: \
45 _TYPE_**top ; \
46 _TYPE_**base ; \
47 int taille ; \
48 };
49
50#define implemente_stack(_TYPE_) \
51 STACK(_TYPE_)::STACK(_TYPE_)(const int t) : taille(t) \
52 { \
53 base = new _TYPE_ [t]; \
54 top = base; \
55 } \
56 STACK(_TYPE_)::~STACK(_TYPE_)() { \
57 delete[] base ; \
58 } \
59 _TYPE_& STACK(_TYPE_)::pop() \
60 { \
61 if (top!=base) return *(--top) ; \
62 return *base; \
63 } \
64 _TYPE_& STACK(_TYPE_)::peek() \
65 { \
66 return *(top-1) ; \
67 } \
68 int STACK(_TYPE_)::push(const _TYPE_& valeur) \
69 { \
70 if ((top-base)==taille) \
71 { \
72 std::cout << "Stack too small : one double the size !!" << std::endl; \
73 _TYPE_* tmp = new _TYPE_[2*taille]; \
74 for (int i=0;i<taille;i++) \
75 tmp[i] = base[i]; \
76 delete [] base; \
77 base = tmp; \
78 top = base+taille; \
79 taille=2*taille; \
80 } \
81 *top++=valeur ; \
82 return 1; \
83 } \
84 int STACK(_TYPE_)::isEmpty() const { return top==base ;}
85
86#define implemente_pstack(_TYPE_) \
87 PSTACK(_TYPE_)::PSTACK(_TYPE_)(const int t) : taille(t) \
88 { \
89 base = new _TYPE_*[t]; \
90 top = base; \
91 } \
92 PSTACK(_TYPE_)::~PSTACK(_TYPE_)() { \
93 delete[] base ; \
94 } \
95 _TYPE_*& PSTACK(_TYPE_)::pop() \
96 { \
97 if (top!=base) return *(--top) ; \
98 return *base; \
99 } \
100 _TYPE_*& PSTACK(_TYPE_)::peek() \
101 { \
102 return *(top-1) ; \
103 } \
104 int PSTACK(_TYPE_)::push(_TYPE_*& valeur) \
105 { \
106 if ((top-base)==taille) \
107 { \
108 std::cout << "Stack too small: one double the size !!" << std::endl; \
109 _TYPE_** tmp = new _TYPE_*[2*taille]; \
110 for (int i=0;i<taille;i++) \
111 tmp[i] = base[i]; \
112 delete [] base; \
113 base = tmp; \
114 top = base+taille; \
115 taille=2*taille; \
116 } \
117 *top++=valeur ; \
118 return 1; \
119 } \
120 int PSTACK(_TYPE_)::isEmpty() const { return top==base ;}
121#endif