const_map.hh

00001 // This file is a part of Aurelia.
00002 // Copyright (C) 2010  Valentin David
00003 // Copyright (C) 2010  University of Bergen
00004 //
00005 // This program is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 #ifndef __CONST_MAP_HH
00018 # define __CONST_MAP_HH
00019 
00020 # include "../strategies.hh"
00021 # include <bitset>
00022 
00023 namespace aurelia {
00024 
00025   template <typename S>
00026   struct const_map_strategy {
00027     S s;
00028     const_map_strategy(const S& s): s(s) {
00029     }
00030 
00031     template <int N>
00032     std::bitset<N> operator()(const std::bitset<N>& t) const {
00033       for (unsigned i = 0; i < N; ++i) {
00034       s(t[i]);
00035       }
00036       return t;
00037     }
00038 
00039     template <typename T>
00040     T operator()(const T& t) const {
00041       for (typename T::const_iterator i = t.begin(); i != t.end(); ++i)
00042         s(*i);
00043       return t;
00044     }
00045 
00046     template <typename Pool>
00047     untyped_term<Pool> operator()(const untyped_term<Pool>& t) const {
00048       typedef untyped_term<Pool> T;
00049       typedef untyped_constructor<Pool> C;
00050 
00051       try {
00052         if (t.constructor() != C::AS_EMPTY_LIST)
00053           throw failure();
00054         return t;
00055       } catch (failure) {
00056         if (t.constructor() == C::AS_LIST) {
00057           s(t[0]);
00058           (*this)(t[1]);
00059           return t;
00060         } else
00061           throw failure();
00062       }
00063     }
00064   };
00065 
00070   template <typename S>
00071   const_map_strategy<S> const_map(const S& s) {
00072     return const_map_strategy<S>(s);
00073   }
00074 
00075 }
00076 
00077 #endif