blitz  Version 0.9
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
listinit.h
Go to the documentation of this file.
1 /***************************************************************************
2  * blitz/listinit.h Classes for initialization lists
3  *
4  * $Id: listinit.h,v 1.4 2003/12/11 03:44:22 julianc Exp $
5  *
6  * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * Suggestions: blitz-dev@oonumerics.org
19  * Bugs: blitz-bugs@oonumerics.org
20  *
21  * For more information, please see the Blitz++ Home Page:
22  * http://oonumerics.org/blitz/
23  *
24  ***************************************************************************/
25 
26 /*
27  * Initialization lists provide a convenient way to set the elements
28  * of an array. For example,
29  *
30  * Array<int,2> A(3,3);
31  * A = 1, 0, 0,
32  * 0, 1, 0,
33  * 0, 0, 1;
34  */
35 
36 #ifndef BZ_LISTINIT_H
37 #define BZ_LISTINIT_H
38 
39 BZ_NAMESPACE(blitz)
40 
41 template<typename T_numtype, typename T_iterator>
43 
44 public:
45  ListInitializer(T_iterator iter)
46  : iter_(iter)
47  {
48  }
49 
51  {
52  *iter_ = x;
54  }
55 
56 private:
57  ListInitializer();
58 
59 protected:
60  T_iterator iter_;
61 };
62 
63 template<typename T_array, typename T_iterator = _bz_typename T_array::T_numtype*>
65 
66 public:
67  typedef _bz_typename T_array::T_numtype T_numtype;
68 
70  : array_(lis.array_), value_(lis.value_),
72  {
73  lis.disable();
74  }
75 
76  ListInitializationSwitch(T_array& array, T_numtype value)
77  : array_(array), value_(value), wipeOnDestruct_(true)
78  { }
79 
81  {
82  if (wipeOnDestruct_)
83  array_.initialize(value_);
84  }
85 
87  {
88  wipeOnDestruct_ = false;
89  T_iterator iter = array_.getInitializationIterator();
90  *iter = value_;
91  T_iterator iter2 = iter + 1;
92  *iter2 = x;
94  }
95 
96  void disable() const
97  {
98  wipeOnDestruct_ = false;
99  }
100 
101 private:
103 
104 protected:
105  T_array& array_;
107  mutable bool wipeOnDestruct_;
108 };
109 
111 
112 #endif // BZ_LISTINIT_H
113