[1022] | 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 | BZ_NAMESPACE(blitz) |
---|
| 46 | |
---|
| 47 | template<typename T_numtype, typename T_iterator> |
---|
| 48 | class ListInitializer { |
---|
| 49 | |
---|
| 50 | public: |
---|
| 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 | |
---|
| 63 | private: |
---|
| 64 | ListInitializer(); |
---|
| 65 | |
---|
| 66 | protected: |
---|
| 67 | T_iterator iter_; |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | template<typename T_array, typename T_iterator = _bz_typename T_array::T_numtype*> |
---|
| 71 | class ListInitializationSwitch { |
---|
| 72 | |
---|
| 73 | public: |
---|
| 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 | |
---|
| 110 | private: |
---|
| 111 | ListInitializationSwitch(); |
---|
| 112 | |
---|
| 113 | protected: |
---|
| 114 | T_array& array_; |
---|
| 115 | T_numtype value_; |
---|
| 116 | mutable bool wipeOnDestruct_; |
---|
| 117 | }; |
---|
| 118 | |
---|
| 119 | BZ_NAMESPACE_END |
---|
| 120 | |
---|
| 121 | #endif // BZ_LISTINIT_H |
---|
| 122 | |
---|