Domi
Multi-dimensional, distributed data structures
 All Classes Files Functions Variables Typedefs Friends
Domi_Slice.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Domi: Multi-dimensional Distributed Linear Algebra Services
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia
8 // Corporation, the U.S. Government retains certain rights in this
9 // software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact William F. Spotz (wfspotz@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef DOMI_SLICE_HPP
44 #define DOMI_SLICE_HPP
45 
51 // Teuchos includes
52 #include "Teuchos_Assert.hpp"
53 
54 // Domi includes
55 #include "Domi_ConfigDefs.hpp"
56 #include "Domi_Exceptions.hpp"
57 #include "Domi_Utils.hpp"
58 
59 namespace Domi
60 {
61 
137 struct Slice
138 {
139 public:
140 
144  typedef Domi::dim_type dim_type;
145 
155  static const dim_type Default;
156 
160 
165  inline Slice();
166 
174  inline Slice(dim_type stopVal);
175 
192  inline Slice(dim_type startVal, dim_type stopVal, dim_type stepVal=1);
193 
196  inline Slice(const Slice & source);
197 
200  virtual ~Slice() { }
201 
203 
207 
219  inline const dim_type start() const;
220 
232  inline const dim_type stop() const;
233 
241  inline const dim_type step() const;
242 
244 
247 
250  inline Slice & operator=(const Slice & source);
251 
254  inline bool operator==(const Slice & slice) const;
255 
258  inline bool operator!=(const Slice & slice) const;
259 
261 
287  virtual Slice bounds(dim_type size) const;
288 
297  std::string toString() const;
298 
303  friend std::ostream & operator<<(std::ostream & os,
304  const Slice & slice);
305 
306 private:
307 
308  // Start index
309  dim_type _start;
310 
311  // Stop index
312  dim_type _stop;
313 
314  // Step interval
315  dim_type _step;
316 
317  // Boolean flag indicating whether the step is positive and the stop
318  // index is concrete (i.e. it is not Default and not negative)
319  bool _bounded_pos;
320 
321  // Boolean flag indicating whether the step is negative and the
322  // start index is concrete (i.e. it is not Default and not negative)
323  bool _bounded_neg;
324 
325 };
326 
340 struct ConcreteSlice : public Slice
341 {
342 public:
350  ConcreteSlice(dim_type stopVal);
351 
363  ConcreteSlice(dim_type startVal, dim_type stopVal, dim_type stepVal=1);
364 
367  virtual ~ConcreteSlice() { }
368 
371  inline Slice bounds(dim_type size) const { return *this; }
372 
373 private:
374 
375  // Private and not implemented
376  ConcreteSlice();
377 };
378 
380 // Inline implementations
382 
384  _start(0),
385  _stop(Slice::Default),
386  _step(1),
387  _bounded_pos(false),
388  _bounded_neg(false)
389 {
390 }
391 
393 
395  _start(0),
396  _stop(stopVal),
397  _step(1),
398  _bounded_pos((_stop >= 0 && _stop != Slice::Default)),
399  _bounded_neg(false)
400 {
401 }
402 
404 
405 Slice::Slice(dim_type startVal, dim_type stopVal, dim_type stepVal) :
406  _start(((startVal==Slice::Default) && (stepVal > 0)) ? 0 : startVal),
407  _stop( ((stopVal ==Slice::Default) && (stepVal < 0)) ? 0 : stopVal ),
408  _step( (stepVal ==Slice::Default) ? 1 : stepVal),
409  _bounded_pos(((_step > 0) && (_stop >= 0 && _stop != Slice::Default))),
410  _bounded_neg(((_step < 0) && (_start >= 0 && _start != Slice::Default)))
411 {
412  TEUCHOS_TEST_FOR_EXCEPTION(
413  (_step == 0),
414  InvalidArgument, "Slice step interval cannot be zero"
415  );
416 }
417 
419 
420 Slice::Slice(const Slice & source) :
421  _start(source._start),
422  _stop(source._stop),
423  _step(source._step),
424  _bounded_pos(source._bounded_pos),
425  _bounded_neg(source._bounded_neg)
426 {
427 }
428 
430 
431 const dim_type Slice::start() const
432 {
433  return _start;
434 }
435 
437 
438 const dim_type Slice::stop() const
439 {
440  return _stop;
441 }
442 
444 
445 const dim_type Slice::step() const
446 {
447  return _step;
448 }
449 
451 
452 Slice & Slice::operator=(const Slice & slice)
453 {
454  _start = slice._start ;
455  _stop = slice._stop ;
456  _step = slice._step ;
457  _bounded_pos = slice._bounded_pos;
458  _bounded_neg = slice._bounded_neg;
459  return *this;
460 }
461 
463 
464 bool Slice::operator==(const Slice & slice) const
465 {
466  return ((_start == slice._start) &&
467  (_stop == slice._stop ) &&
468  (_step == slice._step ) );
469 }
470 
472 
473 bool Slice::operator!=(const Slice & slice) const
474 {
475  return (not operator==(slice));
476 }
477 
478 } // namespace Domi
479 
480 #endif
Slice()
Default constructor.
Definition: Domi_Slice.hpp:383
virtual ~ConcreteSlice()
Destructor.
Definition: Domi_Slice.hpp:367
Slice bounds(dim_type size) const
Simply return this ConcreteSlice.
Definition: Domi_Slice.hpp:371
Domi::dim_type dim_type
The type for start indexes, stop indexes, and step intervals.
Definition: Domi_Slice.hpp:144
bool operator!=(const Slice &slice) const
Inequality operator.
Definition: Domi_Slice.hpp:473
const dim_type stop() const
Stop index.
Definition: Domi_Slice.hpp:438
virtual Slice bounds(dim_type size) const
Return a Slice with concrete start and stop values.
bool operator==(const Slice &slice) const
Equals operator.
Definition: Domi_Slice.hpp:464
A ConcreteSlice is a Slice that does not accept Default or negative start or stop values...
Definition: Domi_Slice.hpp:340
Slice & operator=(const Slice &source)
Assignment operator.
Definition: Domi_Slice.hpp:452
A Slice contains a start, stop, and step index, describing a subset of an ordered container...
Definition: Domi_Slice.hpp:137
virtual ~Slice()
Destructor.
Definition: Domi_Slice.hpp:200
const dim_type step() const
Step interval.
Definition: Domi_Slice.hpp:445
static const dim_type Default
Default value for Slice constructors.
Definition: Domi_Slice.hpp:155
friend std::ostream & operator<<(std::ostream &os, const Slice &slice)
Streaming output operator.
const dim_type start() const
Start index.
Definition: Domi_Slice.hpp:431
std::string toString() const
Return a string representation of the Slice.
Invalid argument exception type.
Definition: Domi_Exceptions.hpp:53

Generated on Fri Jun 5 2020 10:07:58 for Domi by doxygen 1.8.5