Back to index

texmacs  1.0.7.15
collection.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : collection.cpp
00004 * DESCRIPTION: collections
00005 * COPYRIGHT  : (C) 2007  Joris van der Hoeven
00006 *******************************************************************************
00007 * A collection is a "change set" of strings, represented by a hashmap
00008 * string -> int. Positive elements correspond to strings which have
00009 * to be inserted, and negative values to strings which have to be removed.
00010 * Furthermore, there are the following types of values:
00011 *   1, -1: regular values
00012 *   2, -2: indicates that the "change set" reduces to a single string
00013 *   3, -3: indicates that the "change set" reduces to a single, large string
00014 *          which should be stored in a separate file on disk.
00015 *******************************************************************************
00016 * This software falls under the GNU general public license version 3 or later.
00017 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00018 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00019 ******************************************************************************/
00020 
00021 #include "tmfs.hpp"
00022 
00023 /******************************************************************************
00024 * Routines for collections
00025 ******************************************************************************/
00026 
00027 collection
00028 singleton (string s, int eps) {
00029   collection c;
00030   c (s)= eps;
00031   return c;
00032 }
00033 
00034 string
00035 first (collection c) {
00036   iterator<string> it= iterate (c);
00037   return it->next ();
00038 }
00039 
00040 collection
00041 filter (collection c, bool file_flag) {
00042   collection r;
00043   iterator<string> it= iterate (c);
00044   while (it->busy ()) {
00045     string s= it->next ();
00046     int eps= c[s];
00047     if ((eps > -3 && eps < 3) ^ file_flag) r(s)= eps;
00048   }
00049   return r;
00050 }
00051 
00052 collection
00053 simplify (collection c) {
00054   collection r;
00055   iterator<string> it= iterate (c);
00056   while (it->busy ()) {
00057     string s= it->next ();
00058     int eps= c[s];
00059     if (eps > 0) r(s)= eps;
00060   }
00061   return r;
00062 }
00063 
00064 collection
00065 invert (collection c) {
00066   collection r;
00067   iterator<string> it= iterate (c);
00068   while (it->busy ()) {
00069     string s= it->next ();
00070     r(s)= -c[s];
00071   }
00072   return r;
00073 }
00074 
00075 void
00076 merge (collection& c1, collection c2) {
00077   //cout << "Merge " << c1 << ", " << c2 << "\n";
00078   iterator<string> it= iterate (c2);
00079   while (it->busy ()) {
00080     string s= it->next ();
00081     int eps= c2[s];
00082     if (eps < -1 || eps > 1) c1= collection ();
00083     c1(s)= c2[s];
00084   }
00085 }
00086 
00087 collection
00088 operator * (collection c1, collection c2) {
00089   collection r;
00090   merge (r, c1);
00091   merge (r, c2);
00092   return r;
00093 }
00094 
00095 int
00096 total_size (collection c) {
00097   int sz= 1;
00098   iterator<string> it= iterate (c);
00099   while (it->busy ()) sz += (N (it->next ()) + 1);
00100   return sz;
00101 }
00102 
00103 /******************************************************************************
00104 * Basic conversion routines
00105 ******************************************************************************/
00106 
00107 collection
00108 as_collection (strings a) {
00109   collection s;
00110   for (int i=0; i<N(a); i++)
00111     s(a[i])= 1;
00112   return s;
00113 }
00114 
00115 strings
00116 as_strings (collection c) {
00117   strings a;
00118   iterator<string> it= iterate (c);
00119   while (it->busy ()) {
00120     string s= it->next ();
00121     if (c[s] > 0) a << s;
00122   }
00123   return a;
00124 }