shape_doc 0.1
/Users/mourrain/Devel/mmx/shape/include/shape/list.hpp
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * M a t h e m a g i x
00003  *****************************************************************************
00004  * List
00005  * 2008-03-28
00006  * Julien Wintz
00007  *****************************************************************************
00008  *               Copyright (C) 2006 INRIA Sophia-Antipolis
00009  *****************************************************************************
00010  * Comments :
00011  ****************************************************************************/
00012 
00013 # ifndef LIST_H
00014 # define LIST_H
00015 
00016 # include <list>
00017 
00018 using namespace std ;
00019 
00020 namespace mmx {
00021 
00022 namespace shape {
00023 
00024 
00025 template<typename T> T at(list<T> l, int p) ;
00026 template<typename T> void insert(list<T>& l, int p, T t) ;
00027 template<typename T> void replace(list<T>& l, int p, T t) ;
00028 template<typename T> void remove(list<T>& l, int p) ;
00029 template<typename T> void remove(list<T>& l, T t) ;
00030 template<typename T> void operator<<(list<T>& l ,T t) ;
00031 
00032 
00033 template<typename T> int indexof(list<T> l, T t)
00034 {
00035     typename list<T>::iterator it = l.begin() ;
00036     for(int i = 0 ; it != l.end() ; it++, i++)
00037         if(*it == t) return i ;
00038     return -1 ;
00039 }
00040 
00041 template<typename T> T at(list<T> l, int p)
00042 {
00043     typename list<T>::iterator it = l.begin() ;
00044     for(int i = 0 ; i < p ; it++, i++) ;
00045     return *it ;
00046 }
00047 
00048 template<typename T> void insert(list<T>& l, int p, T t)
00049 {
00050     typename list<T>::iterator it = l.begin() ;
00051     for(int i = 1 ; i < p ; i++, it++) ;
00052     l.insert(it, t) ;
00053 }
00054     
00055 template<typename T> void replace(list<T>& l, int p, T t)
00056 {
00057     typename list<T>::iterator it = l.begin() ;
00058     for(int i = 1 ; i < p ; i++, it++) ;
00059     *it = t ;
00060 }
00061 
00062 template<typename T> void remove(list<T>& l, int p)
00063 {
00064     typename list<T>::iterator it = l.begin() ;
00065     for(int i = 1 ; i < p ; i++, it++) ;
00066     l.erase(it) ;
00067 }
00068 
00069 template<typename T> void remove(list<T>& l, T t)
00070 {
00071     remove(l, indexof(l, t)) ;
00072 }
00073 
00074 template<typename T> void operator<<(list<T>& l ,T t)
00075 {
00076     l.push_back(t) ;
00077 }
00078 
00079 } // namespace shape
00080 
00081 } // namespace mmx
00082 
00083 # endif // LIST_H