Back to index

texmacs  1.0.7.15
properties.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : properties.cpp
00004 * DESCRIPTION: properties
00005 * COPYRIGHT  : (C) 2007  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "tmfs.hpp"
00013 
00014 /******************************************************************************
00015 * Building arrays of strings
00016 ******************************************************************************/
00017 
00018 strings
00019 seq (string s1) {
00020   strings a; a << s1; return a;
00021 }
00022 
00023 strings
00024 seq (string s1, string s2) {
00025   strings a; a << s1 << s2; return a;
00026 }
00027 
00028 strings
00029 seq (string s1, string s2, string s3) {
00030   strings a; a << s1 << s2 << s3; return a;
00031 }
00032 
00033 strings
00034 seq (string s1, string s2, string s3, string s4) {
00035   strings a; a << s1 << s2 << s3 << s4; return a;
00036 }
00037 
00038 /******************************************************************************
00039 * Encoding and decoding of properties
00040 ******************************************************************************/
00041 
00042 property
00043 property_quote (property p) {
00044   property q;
00045   int i, n= N(p);
00046   for (i=0; i<n; i++) {
00047     string s= p[i], r;
00048     for (int j=0; j<N(s); j++)
00049       if (s[j] == ',' || s[j] == '*' || s[j] == '\\') r << '\\' << s[j];
00050       else r << s[j];
00051     q << r;
00052   }
00053   return q;
00054 }
00055 
00056 property
00057 property_unquote (property p) {
00058   property q;
00059   int i, n= N(p);
00060   for (i=0; i<n; i++) {
00061     string s= p[i], r;
00062     for (int j=0; j<N(s); j++)
00063       if (s[j] == '\\' && (j+1 < N(s))) { j++; r << s[j]; }
00064       else r << s[j];
00065     q << r;
00066   }
00067   return q;
00068 }
00069 
00070 string
00071 property_append (property p) {
00072   if (N(p) == 0) return "";
00073   string r= copy (p[0]);
00074   for (int i=1; i<N(p); i++) r << ',' << p[i];
00075   return r;
00076 }
00077 
00078 property
00079 property_unappend (string s) {
00080   property p;
00081   int i, n= N(s);
00082   for (i=0; i<n; i++) {
00083     int start= i;
00084     while (i<n && s[i] != ',') {
00085       if (s[i] == '\\' && (i+1<n)) i+=2;
00086       else i++;
00087     }
00088     p << s (start, i);
00089   }
00090   return p;
00091 }
00092 
00093 string
00094 properties_encode (properties ps) {
00095   string r;
00096   for (int i=0; i<N(ps); i++)
00097     r << property_append (property_quote (ps[i])) << '\n';
00098   return r;
00099 }
00100 
00101 properties
00102 properties_decode (string s) {
00103   properties ps;
00104   for (int i=0; i<N(s); i++) {
00105     int start= i;
00106     while (i<N(s) && s[i] != '\n') i++;
00107     ps << property_unquote (property_unappend (s (start, i)));
00108   }
00109   return ps;
00110 }
00111 
00112 /******************************************************************************
00113 * Matching queries
00114 ******************************************************************************/
00115 
00116 bool
00117 matches (property p, property q) {
00118   if (N(p) != N(q)) return false;
00119   solution sol;
00120   for (int i=0; i<N(p); i++)
00121     if (is_unknown (q[i])) {
00122       if (!sol->contains (q[i])) sol(q[i])= p[i];
00123       else if (sol[q[i]] != p[i]) return false;
00124     }
00125     else if (q[i] != p[i]) return false;
00126   return true;
00127 }
00128 
00129 properties
00130 reset (properties ps, property p) {
00131   properties r;
00132   for (int i=0; i<N(ps); i++)
00133     if (!matches (ps[i], p))
00134       r << ps[i];
00135   return r;
00136 }
00137 
00138 properties
00139 reset (properties ps, properties qs) {
00140   for (int i=0; i<N(qs); i++)
00141     ps= reset (ps, qs[i]);
00142   return ps;
00143 }
00144 
00145 /******************************************************************************
00146 * Substitutions
00147 ******************************************************************************/
00148 
00149 property
00150 substitute (property p, string what, string by) {
00151   property q= copy (p);
00152   for (int i=0; i<N(p); i++)
00153     if (p[i] == what) q[i]= by;
00154   return q;
00155 }
00156 
00157 properties
00158 substitute (properties ps, string what, string by) {
00159   properties qs;
00160   for (int i=0; i<N(ps); i++)
00161     qs << substitute (ps[i], what, by);
00162   return qs;
00163 }
00164 
00165 property
00166 substitute (property p, solution sol) {
00167   property r= copy (p);
00168   for (int i=0; i<N(p); i++)
00169     if (sol->contains (p[i]))
00170       r[i]= sol[p[i]];
00171   return r;
00172 }
00173 
00174 properties
00175 substitute (property p, solutions sols) {
00176   properties r;
00177   for (int i=0; i<N(sols); i++)
00178     r << substitute (p, sols[i]);
00179   return r;
00180 }
00181 
00182 /******************************************************************************
00183 * Other routines
00184 ******************************************************************************/
00185 
00186 properties
00187 exclude_types (properties ps, collection c) {
00188   properties r;
00189   for (int i=0; i<N(ps); i++)
00190     if (N(ps[i])>0 && !c->contains (ps[i][0]))
00191       r << ps[i];
00192   return r;
00193 }
00194 
00195 properties
00196 widen (properties ps) {
00197   properties r;
00198   for (int i=0; i<N(ps); i++) {
00199     property c= copy (ps[i]);
00200     if (N(c) > 0) c[N(c)-1]= "?any";
00201     r << c;
00202   }
00203   return r;
00204 }