1 /****************************************************************************/
2 /* This file is part of FreeFEM. */
3 /* */
4 /* FreeFEM is free software: you can redistribute it and/or modify */
5 /* it under the terms of the GNU Lesser General Public License as */
6 /* published by the Free Software Foundation, either version 3 of */
7 /* the License, or (at your option) any later version. */
8 /* */
9 /* FreeFEM is distributed in the hope that it will be useful, */
10 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
11 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
12 /* GNU Lesser General Public License for more details. */
13 /* */
14 /* You should have received a copy of the GNU Lesser General Public License */
15 /* along with FreeFEM. If not, see <http://www.gnu.org/licenses/>. */
16 /****************************************************************************/
17 // SUMMARY : ...
18 // LICENSE : LGPLv3
19 // ORG : LJLL Universite Pierre et Marie Curie, Paris, FRANCE
20 // AUTHORS : Frederic Hecht
21 // E-MAIL : frederic.hecht@sorbonne-universite.fr
22
23 #include "array_tlp.hpp"
24 #include "array_init.hpp"
25 const basicForEachType *aatypeknlongp;
26
27 /*
28 void initArrayOperators() {
29 ArrayOperator<double>();
30 ArrayOperator<Complex>();
31 ArrayOperator<long>();
32 }
33
34 void initArrayDCL() {
35 ArrayDCL<double>();
36 ArrayDCL<Complex>();
37 ArrayDCL<long>();
38 }
39 */
40
41 aType aaaa_knlp;
initArrayDCLlong()42 void initArrayDCLlong() {
43 // ArrayOperator<long>();
44 Dcl_Type<Inv_KN_long>(); // Add FH mars 2005
45 ArrayDCL<long>();
46 aaaa_knlp = atype<KN<long>*>();
47 }
48
49 class OneBinaryOperatorInv_KN_long : public OneOperator {
50 public:
OneBinaryOperatorInv_KN_long(basicForEachType * ti)51 OneBinaryOperatorInv_KN_long(basicForEachType * ti) : OneOperator(atype<Inv_KN_long >(), ti ,atype<long>()) {}
code(const basicAC_F0 & args) const52 E_F0 * code(const basicAC_F0 & args) const {
53 Expression p=args[1];
54 if (!p->EvaluableWithOutStack()) {
55 bool bb = p->EvaluableWithOutStack();
56 cout << bb << " " << *p << endl;
57 CompileError("Inverse: int[int] I, array, with I^p, The p must be a constant == -1, sorry");
58 }
59 long pv = GetAny<long>((*p)(NullStack));
60 if (pv !=-1) {
61 char buf[100];
62 sprintf(buf, "Inverse: int[int] I, array, I^%ld, The pow must be == -1, sorry", pv);
63 CompileError(buf);
64 }
65 return new E_F_F0<Inv_KN_long, KN_<long> >(Build<Inv_KN_long, KN_<long> >, to< KN_<long> >(args[0]));
66 }
67 };
68
69 // Add mars 2010
set_init_init(R * const & a,const long & n)70 template<class R> R *set_init_init( R* const & a,const long & n) {
71 SHOWVERB(cout << " set_init " << typeid(R).name() << " " << n << endl);
72 a->init(n);
73 for (int i = 0; i < n; i++)
74 (*a)[i].init();
75 return a;
76 }
77
get_elements(KN<String> * const & a,long const & b)78 inline string **get_elements(KN<String> *const &a, long const &b) {
79 ffassert(a && b >=0 && b < a->size());
80 String &Sret = (*a)[b]; // correction FH feb 2004
81 // delete b; la chaine est detruire automatiquement en fin d'instruction FH jan 2010
82 return Sret.getap();
83 }
84
Destroy_KN(Stack,const AnyType & x)85 template<class A> inline AnyType Destroy_KN(Stack,const AnyType &x) {
86 KN<A> *a = GetAny<KN<A>*>(x);
87 for (int i = 0; i <a->N(); i++)
88 (a)[i].destroy();
89 a->destroy();
90 return Nothing;
91 }
92 // end add
93
94 template<class A,class B>
95 struct set_Inv_KN_long : public binary_function<A,B,A> {
fset_Inv_KN_long96 static A f(const A & a, B const & b) {
97 int n = a.N();
98 KN_<long> I(b.t);
99 for (int i = 0; i < I.N(); ++i) {
100 int j = I[i];
101 if (j >= 0 && j < n)
102 a[j] = i;
103 }
104 return a;
105 }
106 };
107
108 template<class A,class B>
109 struct set_Inv_pKN_longI: public binary_function<A,B,A> {
fset_Inv_pKN_longI110 static A f(const A & a, B const & b) {
111 KN_<long> I(b.t);
112 int n = I.max() + 1;
113 a->init(n);
114 (*a) = -1;
115 for (int i = 0; i < I.N(); ++i) {
116 int j = I[i];
117 if (j >= 0 && j < n)
118 (*a)[j] = i;
119 }
120 return a;
121 }
122 };
123
124
125
initArrayOperatorlong()126 void initArrayOperatorlong()
127 {
128 typedef long K;
129 Dcl_Type< Eye > ();// OK this is the fist array def ..
130 Global.Add("eye","(",new OneOperator1<Eye,long>(fEye));
131 Global.Add("eye","(",new OneOperator2<Eye,long>(fEye));
132 ArrayOperator<long, long>();
133 // to define inverse permutation // Add FH mars 2005
134 TheOperators->Add("^", new OneBinaryOperatorInv_KN_long(atype<KN_<long> >()));
135 //- TheOperators->Add("^", new OneBinaryOperatorInv_KN_long(atype<KN<long> *>()));
136 aatypeknlongp = atype<KN<long>*>(); // for compilation error with g++ 3.2.2
137
138 Add<KN_<long> >("sort", ".", new OneOperator1_<KN_<K>, KN_<K> >(SortKn<K, KN_<K> >));
139 // Add<KN<long> >("sort", ".", new OneOperator1_<KN<K>,KN<K> >(SortKn<K, KN<K> >));
140 Add<KN<long> *>("sort", ".", new OneOperator1_<KN<K>*, KN<K>* >(SortpKn<K>));
141 Global.Add("sort", "(", new OneOperator2_<KN<K>*, KN<K>*, KN<long>* >(SortpKn2<K,long>));
142
143 // ArrayDCL<long>();
144 Dcl_TypeandPtr_<KN_<String>, KN<String> *>(0, 0, 0, ::Destroy<KN<String> >, ::ClearReturnKK_<K,KN<String>, KN_<String> >, ::ClearReturnpKK<String, KN<String> >);
145 atype<KN<String>* >()->Add("[", "", new OneOperator2_<string**, KN<String>*, long >(get_elements));
146 TheOperators->Add("<-", new OneOperator2_<KN<String> *, KN<String> *, long>(&set_init_init), new InitArrayfromArray<string*, KN<String>*, true>);
147 map_type_of_map[make_pair(atype<long>(), atype<string*>())] = atype<KN<String>*>(); // vector
148 Add<KN<String> *>("n", ".", new OneOperator1<long, KN<String> *>(get_n));
149 extern KN<String> *pkarg;
150 Global.New("ARGV", CPValue<KN<String> >(*pkarg));// add FH mars 2010
151 Global.Add("toZarray", "(", new OneOperator_2KN_<long>);
152 TheOperators->Add("=", new OneBinaryOperator<set_Inv_KN_long<KN_<long>, Inv_KN_long> >);
153 TheOperators->Add("<-", new OneBinaryOperator<set_Inv_pKN_longI<KN<long>*, Inv_KN_long> >);
154
155 Add<KN<K> *>("imin", ".", new OneOperator1<long, KN<K> *>(get_imin));
156 Add<KN<K> *>("imax", ".", new OneOperator1<long, KN<K> *>(get_imax));
157 Add<KNM<K> *>("imin", ".", new OneOperator1<long, KNM<K> *>(get_imin)); // Add april 2018 FH
158 Add<KNM<K> *>("imax", ".", new OneOperator1<long, KNM<K> *>(get_imax)); // Add april 2018 FH
159 Add<KNM<K> *>("jmin", ".", new OneOperator1<long, KNM<K> *>(get_jmin)); // Add april 2018 FH
160 Add<KNM<K> *>("jmax", ".", new OneOperator1<long, KNM<K> *>(get_jmax)); // Add april 2018 FH
161 TheOperators->Add("ijmax", new OneOperator3_<NothingType,KNM<K> *, long*, long*>(get_ijmax)); // Add april 2018 FH
162 TheOperators->Add("ijmin", new OneOperator3_<NothingType,KNM<K> *, long*, long*>(get_ijmin)); // Add april 2018 FH
163 // madd FH. march 2015 ...
164 Global.Add("Unique", "(", new Unique<K, K>);
165 Global.Add("Unique", "(", new Unique<K, double>);
166 // convertion double -> long (via lround)
167 Dcl_Type<F_KN_<K, K, double> >();
168 Global.Add("lround", "(", new OneOperator1F_KN_<F_KN_<K, K, double>, K, double, KN_<double> >(lround));
169 TheOperators->Add("=", new OneBinaryOperator<set_eq_array<KN_<K>, F_KN_<K, K,double> > >); // add FH juin 2005
170 TheOperators->Add("+=", new OneBinaryOperator<set_eq_array_add<KN_<K>, F_KN_<K, K, double> > >); // add FH juin 2005
171 TheOperators->Add("-=", new OneBinaryOperator<set_eq_array_sub<KN_<K>, F_KN_<K, K, double> > >); // add FH juin 2005
172 TheOperators->Add("/=", new OneBinaryOperator<set_eq_array_div<KN_<K>, F_KN_<K, K, double> > >); // add FH juin 2005
173 TheOperators->Add("*=", new OneBinaryOperator<set_eq_array_mul<KN_<K>, F_KN_<K, K, double> > >); // add FH juin 2005
174
175 TheOperators->Add("<-", new InitMapfromArray<MyMap<String,String>*, string *, string*, true> );
176
177 TheOperators->Add("<<", new OneBinaryOperator<PrintPnd<KN<String> * > >); // add may 2018 FH
178 }
179
180 // void xxxx() {
181 // }
182