| 1 | /***************************************************************************
 | 
|---|
| 2 |  * blitz/listinit.h      Classes for initialization lists
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  * $Id: listinit.h,v 1.1.1.1 1999-11-26 16:37:03 ansari Exp $
 | 
|---|
| 5 |  *
 | 
|---|
| 6 |  * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
 | 
|---|
| 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-suggest@cybervision.com
 | 
|---|
| 19 |  * Bugs:                 blitz-bugs@cybervision.com
 | 
|---|
| 20 |  *
 | 
|---|
| 21 |  * For more information, please see the Blitz++ Home Page:
 | 
|---|
| 22 |  *    http://seurat.uwaterloo.ca/blitz/
 | 
|---|
| 23 |  *
 | 
|---|
| 24 |  ***************************************************************************
 | 
|---|
| 25 |  * $Log: not supported by cvs2svn $
 | 
|---|
| 26 |  * Revision 1.1.1.1  1999/04/09  17:59:02  ansari
 | 
|---|
| 27 |  * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99
 | 
|---|
| 28 |  *
 | 
|---|
| 29 |  * Revision 1.2  1998/03/14 00:04:47  tveldhui
 | 
|---|
| 30 |  * 0.2-alpha-05
 | 
|---|
| 31 |  *
 | 
|---|
| 32 |  * Revision 1.1  1997/07/16 14:51:20  tveldhui
 | 
|---|
| 33 |  * Update: Alpha release 0.2 (Arrays)
 | 
|---|
| 34 |  *
 | 
|---|
| 35 |  */
 | 
|---|
| 36 | 
 | 
|---|
| 37 | /*
 | 
|---|
| 38 |  * Initialization lists provide a convenient way to set the elements
 | 
|---|
| 39 |  * of an array.  For example,
 | 
|---|
| 40 |  *
 | 
|---|
| 41 |  * Array<int,2> A(3,3);
 | 
|---|
| 42 |  * A = 1, 0, 0,
 | 
|---|
| 43 |  *     0, 1, 0,
 | 
|---|
| 44 |  *     0, 0, 1;
 | 
|---|
| 45 |  */
 | 
|---|
| 46 | 
 | 
|---|
| 47 | #ifndef BZ_LISTINIT_H
 | 
|---|
| 48 | #define BZ_LISTINIT_H
 | 
|---|
| 49 | 
 | 
|---|
| 50 | BZ_NAMESPACE(blitz)
 | 
|---|
| 51 | 
 | 
|---|
| 52 | template<class T_numtype, class T_iterator>
 | 
|---|
| 53 | class ListInitializer {
 | 
|---|
| 54 | 
 | 
|---|
| 55 | public:
 | 
|---|
| 56 |     ListInitializer(T_iterator iter)
 | 
|---|
| 57 |         : iter_(iter)
 | 
|---|
| 58 |     {
 | 
|---|
| 59 |     }
 | 
|---|
| 60 | 
 | 
|---|
| 61 |     ListInitializer<T_numtype, T_iterator> operator,(T_numtype x)
 | 
|---|
| 62 |     {
 | 
|---|
| 63 |         *iter_ = x;
 | 
|---|
| 64 |         return ListInitializer(iter_ + 1);
 | 
|---|
| 65 |     }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | private:
 | 
|---|
| 68 |     ListInitializer();
 | 
|---|
| 69 | 
 | 
|---|
| 70 | protected:
 | 
|---|
| 71 |     T_iterator iter_;
 | 
|---|
| 72 | };
 | 
|---|
| 73 | 
 | 
|---|
| 74 | template<class T_array, class T_iterator = _bz_typename T_array::T_numtype*>
 | 
|---|
| 75 | class ListInitializationSwitch {
 | 
|---|
| 76 | 
 | 
|---|
| 77 | public:
 | 
|---|
| 78 |     typedef _bz_typename T_array::T_numtype T_numtype;
 | 
|---|
| 79 | 
 | 
|---|
| 80 |     ListInitializationSwitch(const ListInitializationSwitch<T_array>& lis)
 | 
|---|
| 81 |         : array_(lis.array_), value_(lis.value_), 
 | 
|---|
| 82 |           wipeOnDestruct_(_bz_true)
 | 
|---|
| 83 |     {
 | 
|---|
| 84 |         lis.disable();
 | 
|---|
| 85 |     }
 | 
|---|
| 86 | 
 | 
|---|
| 87 |     ListInitializationSwitch(T_array& array, T_numtype value)
 | 
|---|
| 88 |         : array_(array), value_(value), wipeOnDestruct_(_bz_true)
 | 
|---|
| 89 |     { }
 | 
|---|
| 90 | 
 | 
|---|
| 91 |     ~ListInitializationSwitch()
 | 
|---|
| 92 |     {
 | 
|---|
| 93 |         if (wipeOnDestruct_)
 | 
|---|
| 94 |             array_.initialize(value_);
 | 
|---|
| 95 |     }
 | 
|---|
| 96 | 
 | 
|---|
| 97 |     ListInitializer<T_numtype, T_iterator> operator,(T_numtype x)
 | 
|---|
| 98 |     {
 | 
|---|
| 99 |         wipeOnDestruct_ = _bz_false;
 | 
|---|
| 100 |         T_iterator iter = array_.getInitializationIterator();
 | 
|---|
| 101 |         *iter = value_;
 | 
|---|
| 102 |         T_iterator iter2 = iter + 1;
 | 
|---|
| 103 |         *iter2 = x;
 | 
|---|
| 104 |         return ListInitializer<T_numtype, T_iterator>(iter2 + 1);
 | 
|---|
| 105 |     }
 | 
|---|
| 106 | 
 | 
|---|
| 107 |     void disable() const
 | 
|---|
| 108 |     {
 | 
|---|
| 109 |         wipeOnDestruct_ = _bz_false;
 | 
|---|
| 110 |     }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | private:
 | 
|---|
| 113 |     ListInitializationSwitch();
 | 
|---|
| 114 | 
 | 
|---|
| 115 | protected:
 | 
|---|
| 116 |     T_array& array_;
 | 
|---|
| 117 |     T_numtype value_;
 | 
|---|
| 118 |     mutable _bz_bool wipeOnDestruct_;
 | 
|---|
| 119 | };
 | 
|---|
| 120 | 
 | 
|---|
| 121 | BZ_NAMESPACE_END
 | 
|---|
| 122 | 
 | 
|---|
| 123 | #endif // BZ_LISTINIT_H
 | 
|---|
| 124 | 
 | 
|---|