blitz Version 0.10
|
00001 // -*- C++ -*- 00002 /*************************************************************************** 00003 * blitz/listinit.h Classes for initialization lists 00004 * 00005 * $Id: listinit.h,v 1.5 2011/03/25 22:41:16 julianc Exp $ 00006 * 00007 * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> 00008 * 00009 * This file is a part of Blitz. 00010 * 00011 * Blitz is free software: you can redistribute it and/or modify 00012 * it under the terms of the GNU Lesser General Public License 00013 * as published by the Free Software Foundation, either version 3 00014 * of the License, or (at your option) any later version. 00015 * 00016 * Blitz is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. 00023 * 00024 * Suggestions: blitz-devel@lists.sourceforge.net 00025 * Bugs: blitz-support@lists.sourceforge.net 00026 * 00027 * For more information, please see the Blitz++ Home Page: 00028 * https://sourceforge.net/projects/blitz/ 00029 * 00030 ***************************************************************************/ 00031 00032 /* 00033 * Initialization lists provide a convenient way to set the elements 00034 * of an array. For example, 00035 * 00036 * Array<int,2> A(3,3); 00037 * A = 1, 0, 0, 00038 * 0, 1, 0, 00039 * 0, 0, 1; 00040 */ 00041 00042 #ifndef BZ_LISTINIT_H 00043 #define BZ_LISTINIT_H 00044 00045 BZ_NAMESPACE(blitz) 00046 00047 template<typename T_numtype, typename T_iterator> 00048 class ListInitializer { 00049 00050 public: 00051 ListInitializer(T_iterator iter) 00052 : iter_(iter) 00053 { 00054 } 00055 00056 ListInitializer<T_numtype, T_iterator> operator,(T_numtype x) 00057 { 00058 *iter_ = x; 00059 return ListInitializer<T_numtype, T_iterator>(iter_ + 1); 00060 } 00061 00062 private: 00063 ListInitializer(); 00064 00065 protected: 00066 T_iterator iter_; 00067 }; 00068 00069 template<typename T_array, typename T_iterator = _bz_typename T_array::T_numtype*> 00070 class ListInitializationSwitch { 00071 00072 public: 00073 typedef _bz_typename T_array::T_numtype T_numtype; 00074 00075 ListInitializationSwitch(const ListInitializationSwitch<T_array>& lis) 00076 : array_(lis.array_), value_(lis.value_), 00077 wipeOnDestruct_(true) 00078 { 00079 lis.disable(); 00080 } 00081 00082 ListInitializationSwitch(T_array& array, T_numtype value) 00083 : array_(array), value_(value), wipeOnDestruct_(true) 00084 { } 00085 00086 ~ListInitializationSwitch() 00087 { 00088 if (wipeOnDestruct_) 00089 array_.initialize(value_); 00090 } 00091 00092 ListInitializer<T_numtype, T_iterator> operator,(T_numtype x) 00093 { 00094 wipeOnDestruct_ = false; 00095 T_iterator iter = array_.getInitializationIterator(); 00096 *iter = value_; 00097 T_iterator iter2 = iter + 1; 00098 *iter2 = x; 00099 return ListInitializer<T_numtype, T_iterator>(iter2 + 1); 00100 } 00101 00102 void disable() const 00103 { 00104 wipeOnDestruct_ = false; 00105 } 00106 00107 private: 00108 ListInitializationSwitch(); 00109 00110 protected: 00111 T_array& array_; 00112 T_numtype value_; 00113 mutable bool wipeOnDestruct_; 00114 }; 00115 00116 BZ_NAMESPACE_END 00117 00118 #endif // BZ_LISTINIT_H 00119