blitz  Version 1.0.2
listinit.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  * blitz/listinit.h Classes for initialization lists
4  *
5  * $Id$
6  *
7  * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
8  *
9  * This file is a part of Blitz.
10  *
11  * Blitz is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation, either version 3
14  * of the License, or (at your option) any later version.
15  *
16  * Blitz is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with Blitz. If not, see <http://www.gnu.org/licenses/>.
23  *
24  * Suggestions: blitz-devel@lists.sourceforge.net
25  * Bugs: blitz-support@lists.sourceforge.net
26  *
27  * For more information, please see the Blitz++ Home Page:
28  * https://sourceforge.net/projects/blitz/
29  *
30  ***************************************************************************/
31 
32 /*
33  * Initialization lists provide a convenient way to set the elements
34  * of an array. For example,
35  *
36  * Array<int,2> A(3,3);
37  * A = 1, 0, 0,
38  * 0, 1, 0,
39  * 0, 0, 1;
40  */
41 
42 #ifndef BZ_LISTINIT_H
43 #define BZ_LISTINIT_H
44 
45 namespace blitz {
46 
47 template<typename T_numtype, typename T_iterator>
49 
50 public:
51  ListInitializer(T_iterator iter)
52  : iter_(iter)
53  {
54  }
55 
57  {
58  *iter_ = x;
59  ++iter_;
61  }
62 
63 private:
65 
66 protected:
67  T_iterator iter_;
68 };
69 
70 template<typename T_array, typename T_iterator = _bz_typename T_array::T_numtype*>
72 
73 public:
74  typedef _bz_typename T_array::T_numtype T_numtype;
75 
77  : array_(lis.array_), value_(lis.value_),
79  {
80  lis.disable();
81  }
82 
83  ListInitializationSwitch(T_array& array, T_numtype value)
84  : array_(array), value_(value), wipeOnDestruct_(true)
85  { }
86 
88  {
89  if (wipeOnDestruct_)
90  array_.initialize(value_);
91  }
92 
94  {
95  wipeOnDestruct_ = false;
96  T_iterator iter = array_.getInitializationIterator();
97  *iter = value_;
98  ++iter;
99  //T_iterator iter2 = iter + 1;
100  *iter = x;
101  ++iter;
103  }
104 
105  void disable() const
106  {
107  wipeOnDestruct_ = false;
108  }
109 
110 private:
112 
113 protected:
114  T_array& array_;
115  T_numtype value_;
116  mutable bool wipeOnDestruct_;
117 };
118 
119 }
120 
121 #endif // BZ_LISTINIT_H
122 
ListInitializer< T_numtype, T_iterator > operator,(T_numtype x)
Definition: listinit.h:56
ListInitializationSwitch(T_array &array, T_numtype value)
Definition: listinit.h:83
Definition: listinit.h:71
T_numtype value_
Definition: listinit.h:115
T_iterator iter_
Definition: listinit.h:67
Definition: array-impl.h:66
Definition: listinit.h:48
ListInitializationSwitch(const ListInitializationSwitch< T_array > &lis)
Definition: listinit.h:76
~ListInitializationSwitch()
Definition: listinit.h:87
ListInitializer< T_numtype, T_iterator > operator,(T_numtype x)
Definition: listinit.h:93
bool wipeOnDestruct_
Definition: listinit.h:116
void disable() const
Definition: listinit.h:105
ListInitializer(T_iterator iter)
Definition: listinit.h:51
#define _bz_typename
Definition: compiler.h:80
T_array & array_
Definition: listinit.h:114
_bz_typename T_array::T_numtype T_numtype
Definition: listinit.h:74
#define true
Definition: compiler.h:101