source: XMLIO_V2/external/include/blitz/listinit.h @ 73

Last change on this file since 73 was 73, checked in by ymipsl, 14 years ago
  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
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
39BZ_NAMESPACE(blitz)
40
41template<typename T_numtype, typename T_iterator>
42class ListInitializer {
43
44public:
45    ListInitializer(T_iterator iter)
46        : iter_(iter)
47    {
48    }
49
50    ListInitializer<T_numtype, T_iterator> operator,(T_numtype x)
51    {
52        *iter_ = x;
53        return ListInitializer<T_numtype, T_iterator>(iter_ + 1);
54    }
55
56private:
57    ListInitializer();
58
59protected:
60    T_iterator iter_;
61};
62
63template<typename T_array, typename T_iterator = _bz_typename T_array::T_numtype*>
64class ListInitializationSwitch {
65
66public:
67    typedef _bz_typename T_array::T_numtype T_numtype;
68
69    ListInitializationSwitch(const ListInitializationSwitch<T_array>& lis)
70        : array_(lis.array_), value_(lis.value_), 
71          wipeOnDestruct_(true)
72    {
73        lis.disable();
74    }
75
76    ListInitializationSwitch(T_array& array, T_numtype value)
77        : array_(array), value_(value), wipeOnDestruct_(true)
78    { }
79
80    ~ListInitializationSwitch()
81    {
82        if (wipeOnDestruct_)
83            array_.initialize(value_);
84    }
85
86    ListInitializer<T_numtype, T_iterator> operator,(T_numtype x)
87    {
88        wipeOnDestruct_ = false;
89        T_iterator iter = array_.getInitializationIterator();
90        *iter = value_;
91        T_iterator iter2 = iter + 1;
92        *iter2 = x;
93        return ListInitializer<T_numtype, T_iterator>(iter2 + 1);
94    }
95
96    void disable() const
97    {
98        wipeOnDestruct_ = false;
99    }
100
101private:
102    ListInitializationSwitch();
103
104protected:
105    T_array& array_;
106    T_numtype value_;
107    mutable bool wipeOnDestruct_;
108};
109
110BZ_NAMESPACE_END
111
112#endif // BZ_LISTINIT_H
113
Note: See TracBrowser for help on using the repository browser.