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

Last change on this file since 1022 was 1022, checked in by mhnguyen, 7 years ago
File size: 5.0 KB
Line 
1// -*- C++ -*-
2/***************************************************************************
3 * blitz/array/domain.h  Declaration of the RectDomain class
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#ifndef BZ_DOMAIN_H
32#define BZ_DOMAIN_H
33
34#include <blitz/blitz.h>
35#include <blitz/et-forward.h>
36#include <blitz/range.h>
37
38/*
39 * Portions of this class were inspired by the "RectDomain" class
40 * provided by the Titanium language (UC Berkeley).
41 */
42
43BZ_NAMESPACE(blitz)
44
45template<int N_rank>
46class RectDomain {
47
48    typedef TinyVector<int,N_rank> Bounds;
49
50public:
51
52    RectDomain() { }
53    RectDomain(const Bounds& lbound,const Bounds& ubound): lbound_(lbound),ubound_(ubound) { }
54    RectDomain(const TinyVector<Range,N_rank>& bndrange): lbound_(),ubound_() {
55        for (int i=0;i<N_rank;++i) {
56            lbound_(i) = bndrange(i).first();
57            ubound_(i) = bndrange(i).last();
58        }
59    }
60
61    // NEEDS_WORK: better constructors
62    // RectDomain(Range, Range, ...)
63    // RectDomain with any combination of Range and int
64
65          Bounds& lbound()       { return lbound_; }
66          Bounds& ubound()       { return ubound_; }
67    const Bounds& lbound() const { return lbound_; }
68    const Bounds& ubound() const { return ubound_; }
69
70    int& lbound(const int i)       { return lbound_(i); }
71    int& ubound(const int i)       { return ubound_(i); }
72    int  lbound(const int i) const { return lbound_(i); }
73    int  ubound(const int i) const { return ubound_(i); }
74
75    Range operator[](const int rank) const { return Range(lbound_(rank), ubound_(rank)); }
76
77    void shrink(const int amount) {
78        lbound_ += amount;
79        ubound_ -= amount;
80    }
81
82    void shrink(const int dim,const int amount) {
83        lbound_(dim) += amount;
84        ubound_(dim) -= amount;
85    }
86
87    void expand(const int amount) {
88        lbound_ -= amount;
89        ubound_ += amount;
90    }
91
92    void expand(const int dim,const int amount) {
93        lbound_(dim) -= amount;
94        ubound_(dim) += amount;
95    }
96
97private:
98
99    Bounds lbound_;
100    Bounds ubound_;
101};
102
103/*
104 * StridedDomain added by Julian Cummings
105 */
106
107template<int N_rank>
108class StridedDomain {
109
110    typedef TinyVector<int,N_rank> Bounds;
111    typedef TinyVector<diffType,N_rank> Strides;
112
113public:
114
115    StridedDomain(const Bounds& lbound,const Bounds& ubound,const Strides& stride):
116        lbound_(lbound),ubound_(ubound),stride_(stride) { }
117
118    // NEEDS_WORK: better constructors
119    // StridedDomain(Range, Range, ...)
120    // StridedDomain with any combination of Range and int
121
122    const Bounds&  lbound() const { return lbound_; }
123    const Bounds&  ubound() const { return ubound_; }
124    const Strides& stride() const { return stride_; }
125
126    int lbound(const int i) const { return lbound_(i); }
127    int ubound(const int i) const { return ubound_(i); }
128  diffType stride(const int i) const { return stride_(i); }
129
130    Range operator[](const int rank) const { return Range(lbound_(rank),ubound_(rank),stride_(rank)); }
131
132    void shrink(const int amount) {
133        lbound_ += amount*stride_;
134        ubound_ -= amount*stride_;
135    }
136
137    void shrink(const int dim,const int amount) {
138        lbound_(dim) += amount*stride_(dim);
139        ubound_(dim) -= amount*stride_(dim);
140    }
141
142    void expand(const int amount) {
143        lbound_ -= amount*stride_;
144        ubound_ += amount*stride_;
145    }
146
147    void expand(const int dim,const int amount) {
148        lbound_(dim) -= amount*stride_(dim);
149        ubound_(dim) += amount*stride_(dim);
150    }
151
152private:
153
154    Bounds  lbound_;
155    Bounds  ubound_;
156    Strides stride_;
157};
158
159
160template<int N_rank>
161inline RectDomain<N_rank>
162strip(const TinyVector<int,N_rank>& startPosition,const int stripDimension,const int ubound) {
163    BZPRECONDITION((stripDimension >= 0) && (stripDimension < N_rank));
164    BZPRECONDITION(ubound >= startPosition(stripDimension));
165
166    TinyVector<int,N_rank> endPosition = startPosition;
167    endPosition(stripDimension) = ubound;
168    return RectDomain<N_rank>(startPosition, endPosition);
169}
170
171BZ_NAMESPACE_END
172
173#endif // BZ_DOMAIN_H
Note: See TracBrowser for help on using the repository browser.