list.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 
00018 #ifndef __LIST_HH
00019 # define __LIST_HH
00020 
00021 # include <memory>
00022 
00023 namespace aurelia {
00024 
00025   struct list_nil {
00026   private:
00027   public:
00028     enum : unsigned { size = 0 };
00029     list_nil() {}
00030     list_nil(const list_nil&) {}
00031     list_nil(list_nil&&) {}
00032   };
00033 
00034   template <typename T, typename Tail>
00035   struct list_cons {
00036   private:
00037     T h;
00038     Tail t;
00039 
00040   public:
00041     enum : unsigned { size = (unsigned)(Tail::size)+1 };
00042 
00043     list_cons(T&& h, const Tail& t):
00044       h(std::move(h)), t(t) {}
00045 
00046     list_cons(T&& h, Tail&& t):
00047       h(std::move(h)), t(std::move(t)) {}
00048 
00049     list_cons(const T& h, Tail&& t):
00050       h(h), t(std::move(t)) {}
00051 
00052     list_cons(const T& h, const Tail& t):
00053       h(h), t(t) {}
00054 
00055     list_cons(const list_cons& other):
00056       h(other.h), t(other.t) {}
00057 
00058     list_cons(list_cons&& other):
00059       h(std::move(other.h)), t(std::move(other.t)) {}
00060 
00061     const T& head() const {
00062       return h;
00063     }
00064 
00065     const Tail& tail() const {
00066       return t;
00067     }
00068   };
00069 
00070   template <typename H, typename T>
00071   H head(const list_cons<H,T>& l) {
00072     return l.head();
00073   }
00074 
00075 
00076   template <typename H, typename T>
00077   T tail(const list_cons<H,T>& l) {
00078     return l.tail();
00079   }
00080 
00081   template <typename H, typename T>
00082   list_cons<H,T> cons(const H& h, const T& t) {
00083     return list_cons<H,T>(h, t);
00084   };
00085 
00086   void head(list_nil);
00087 
00088   list_nil tail(list_nil);
00089 
00090   list_nil cons();
00091 
00092 }
00093 
00094 #endif