source: XIOS/dev/dev_olga/src/extern/blitz/include/blitz/listinit.h @ 1022

Last change on this file since 1022 was 1022, checked in by mhnguyen, 7 years ago
File size: 3.0 KB
Line 
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
45BZ_NAMESPACE(blitz)
46
47template<typename T_numtype, typename T_iterator>
48class ListInitializer {
49
50public:
51    ListInitializer(T_iterator iter)
52        : iter_(iter)
53    {
54    }
55
56    ListInitializer<T_numtype, T_iterator> operator,(T_numtype x)
57    {
58        *iter_ = x;
59        ++iter_;
60        return ListInitializer<T_numtype, T_iterator>(iter_);
61    }
62
63private:
64    ListInitializer();
65
66protected:
67    T_iterator iter_;
68};
69
70template<typename T_array, typename T_iterator = _bz_typename T_array::T_numtype*>
71class ListInitializationSwitch {
72
73public:
74    typedef _bz_typename T_array::T_numtype T_numtype;
75
76    ListInitializationSwitch(const ListInitializationSwitch<T_array>& lis)
77        : array_(lis.array_), value_(lis.value_), 
78          wipeOnDestruct_(true)
79    {
80        lis.disable();
81    }
82
83    ListInitializationSwitch(T_array& array, T_numtype value)
84        : array_(array), value_(value), wipeOnDestruct_(true)
85    { }
86
87    ~ListInitializationSwitch()
88    {
89        if (wipeOnDestruct_)
90            array_.initialize(value_);
91    }
92
93    ListInitializer<T_numtype, T_iterator> operator,(T_numtype x)
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;
102        return ListInitializer<T_numtype, T_iterator>(iter);
103    }
104
105    void disable() const
106    {
107        wipeOnDestruct_ = false;
108    }
109
110private:
111    ListInitializationSwitch();
112
113protected:
114    T_array& array_;
115    T_numtype value_;
116    mutable bool wipeOnDestruct_;
117};
118
119BZ_NAMESPACE_END
120
121#endif // BZ_LISTINIT_H
122
Note: See TracBrowser for help on using the repository browser.