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