os/ossrv/stdcpp/tsrc/Boost_test/multi_array/src/dimtest.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/stdcpp/tsrc/Boost_test/multi_array/src/dimtest.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,334 @@
     1.4 +// Copyright 2002 The Trustees of Indiana University.
     1.5 +
     1.6 +// Use, modification and distribution is subject to the Boost Software 
     1.7 +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
     1.8 +// http://www.boost.org/LICENSE_1_0.txt)
     1.9 +
    1.10 +//  Boost.MultiArray Library
    1.11 +//  Authors: Ronald Garcia
    1.12 +//           Jeremy Siek
    1.13 +//           Andrew Lumsdaine
    1.14 +//  See http://www.boost.org/libs/multi_array for documentation.
    1.15 +/*
    1.16 + * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
    1.17 +*/
    1.18 +
    1.19 +//
    1.20 +// Trying to diagnose problems under visual
    1.21 +
    1.22 +#include "boost/config.hpp"
    1.23 +#include "boost/array.hpp"
    1.24 +#include "boost/limits.hpp"
    1.25 +#include <algorithm>
    1.26 +#include <utility>
    1.27 +#include <iostream>
    1.28 +
    1.29 +#ifdef __SYMBIAN32__
    1.30 +#include "std_log_result.h"
    1.31 +#define LOG_FILENAME_LINE __FILE__, __LINE__
    1.32 +#endif
    1.33 +namespace dimtest
    1.34 +{
    1.35 +  typedef int index;		
    1.36 +}
    1.37 +
    1.38 +typedef std::size_t size_type;
    1.39 +
    1.40 +  template <typename Index,typename SizeType>
    1.41 +  class index_range {
    1.42 +  public:
    1.43 +
    1.44 +    index_range()
    1.45 +    {
    1.46 +      start_ = from_start();
    1.47 +      finish_ = to_end();
    1.48 +      stride_ = 1;
    1.49 +      degenerate_ = false;
    1.50 +    }
    1.51 +
    1.52 +    explicit index_range(Index pos)
    1.53 +    {
    1.54 +      start_ = pos;
    1.55 +      finish_ = pos;
    1.56 +      stride_ = 1;
    1.57 +      degenerate_ = true;
    1.58 +    }
    1.59 +
    1.60 +    explicit index_range(Index start, Index finish, Index stride=1)
    1.61 +      : start_(start), finish_(finish), stride_(stride),
    1.62 +        degenerate_(start_ == finish_)
    1.63 +    { }
    1.64 +
    1.65 +
    1.66 +    // These are for chaining assignments to an index_range
    1.67 +    index_range& start(Index s) {
    1.68 +      start_ = s;
    1.69 +      degenerate_ = (start_ == finish_);
    1.70 +      return *this;
    1.71 +    }
    1.72 +
    1.73 +    index_range& finish(Index f) {
    1.74 +      finish_ = f;
    1.75 +      degenerate_ = (start_ == finish_);
    1.76 +      return *this;
    1.77 +    }
    1.78 +
    1.79 +    index_range& stride(Index s) { stride_ = s; return *this; }
    1.80 +
    1.81 +    Index start() const
    1.82 +    { 
    1.83 +      return start_; 
    1.84 +    }
    1.85 +
    1.86 +    Index get_start(Index low_index_range = 0) const
    1.87 +    { 
    1.88 +      if (start_ == from_start())
    1.89 +        return low_index_range;
    1.90 +      return start_; 
    1.91 +    }
    1.92 +
    1.93 +    Index finish() const
    1.94 +    {
    1.95 +      return finish_;
    1.96 +    }
    1.97 +
    1.98 +    Index get_finish(Index high_index_range = 0) const
    1.99 +    {
   1.100 +      if (finish_ == to_end())
   1.101 +        return high_index_range;
   1.102 +      return finish_;
   1.103 +    }
   1.104 +
   1.105 +    unsigned int size(Index recommended_length = 0) const
   1.106 +    {
   1.107 +      if ((start_ == from_start()) || (finish_ == to_end()))
   1.108 +        return recommended_length;
   1.109 +      else 
   1.110 +        return (finish_ - start_) / stride_;
   1.111 +    }
   1.112 +
   1.113 +    Index stride() const { return stride_; }
   1.114 +
   1.115 +    bool is_ascending_contiguous() const
   1.116 +    {
   1.117 +      return (start_ < finish_) && is_unit_stride();
   1.118 +    }
   1.119 +
   1.120 +    void set_index_range(Index start, Index finish, Index stride=1)
   1.121 +    {
   1.122 +      start_ = start;
   1.123 +      finish_ = finish;
   1.124 +      stride_ = stride;
   1.125 +    }
   1.126 +
   1.127 +    static index_range all() 
   1.128 +    { return index_range(from_start(), to_end(), 1); }
   1.129 +
   1.130 +    bool is_unit_stride() const
   1.131 +    { return stride_ == 1; }
   1.132 +
   1.133 +    bool is_degenerate() const { return degenerate_; }
   1.134 +
   1.135 +    index_range operator-(Index shift) const
   1.136 +    { 
   1.137 +      return index_range(start_ - shift, finish_ - shift, stride_); 
   1.138 +    }
   1.139 +
   1.140 +    index_range operator+(Index shift) const
   1.141 +    { 
   1.142 +      return index_range(start_ + shift, finish_ + shift, stride_); 
   1.143 +    }
   1.144 +
   1.145 +    Index operator[](unsigned i) const
   1.146 +    {
   1.147 +      return start_ + i * stride_;
   1.148 +    }
   1.149 +
   1.150 +    Index operator()(unsigned i) const
   1.151 +    {
   1.152 +      return start_ + i * stride_;
   1.153 +    }
   1.154 +
   1.155 +    // add conversion to std::slice?
   1.156 +
   1.157 +  private:
   1.158 +    static Index from_start()
   1.159 +      { return (std::numeric_limits<Index>::min)(); }
   1.160 +
   1.161 +    static Index to_end()
   1.162 +      { return (std::numeric_limits<Index>::max)(); }
   1.163 +  public:
   1.164 +    Index start_, finish_, stride_;
   1.165 +    bool degenerate_;
   1.166 +  };
   1.167 +
   1.168 +  // Express open and closed interval end-points using the comparison
   1.169 +  // operators.
   1.170 +
   1.171 +  // left closed
   1.172 +  template <typename Index, typename SizeType>
   1.173 +  inline index_range<Index,SizeType>
   1.174 +  operator<=(Index s, const index_range<Index,SizeType>& r)
   1.175 +  {
   1.176 +    return index_range<Index,SizeType>(s, r.finish(), r.stride());
   1.177 +  }
   1.178 +
   1.179 +  // left open
   1.180 +  template <typename Index, typename SizeType>
   1.181 +  inline index_range<Index,SizeType>
   1.182 +  operator<(Index s, const index_range<Index,SizeType>& r)
   1.183 +  {
   1.184 +    return index_range<Index,SizeType>(s + 1, r.finish(), r.stride());
   1.185 +  }
   1.186 +
   1.187 +  // right open
   1.188 +  template <typename Index, typename SizeType>
   1.189 +  inline index_range<Index,SizeType>
   1.190 +  operator<(const index_range<Index,SizeType>& r, Index f)
   1.191 +  {
   1.192 +    return index_range<Index,SizeType>(r.start(), f, r.stride());
   1.193 +  }
   1.194 +
   1.195 +  // right closed
   1.196 +  template <typename Index, typename SizeType>
   1.197 +  inline index_range<Index,SizeType>
   1.198 +  operator<=(const index_range<Index,SizeType>& r, Index f)
   1.199 +  {
   1.200 +    return index_range<Index,SizeType>(r.start(), f + 1, r.stride());
   1.201 +  }
   1.202 +
   1.203 +//
   1.204 +// range_list.hpp - helper to build boost::arrays for *_set types
   1.205 +//
   1.206 +
   1.207 +/////////////////////////////////////////////////////////////////////////
   1.208 +// choose range list begins
   1.209 +//
   1.210 +
   1.211 +struct choose_range_list_n {
   1.212 +  template <typename T, std::size_t NumRanges>
   1.213 +  struct bind {
   1.214 +    typedef boost::array<T,NumRanges> type;
   1.215 +  };
   1.216 +};
   1.217 +
   1.218 +struct choose_range_list_zero {
   1.219 +  template <typename T, std::size_t NumRanges>
   1.220 +  struct bind {
   1.221 +    typedef boost::array<T,1> type;
   1.222 +  };
   1.223 +};
   1.224 +
   1.225 +
   1.226 +template <std::size_t NumRanges>
   1.227 +struct range_list_gen_helper {
   1.228 +  typedef choose_range_list_n choice;
   1.229 +};
   1.230 +
   1.231 +template <>
   1.232 +struct range_list_gen_helper<0> {
   1.233 +  typedef choose_range_list_zero choice;
   1.234 +};
   1.235 +
   1.236 +template <typename T, std::size_t NumRanges>
   1.237 +struct range_list_generator {
   1.238 +private:
   1.239 +  typedef typename range_list_gen_helper<NumRanges>::choice Choice;
   1.240 +public:
   1.241 +  typedef typename Choice::template bind<T,NumRanges>::type type;
   1.242 +};
   1.243 +
   1.244 +//
   1.245 +// choose range list ends
   1.246 +/////////////////////////////////////////////////////////////////////////
   1.247 +
   1.248 +//
   1.249 +// Index_gen.hpp stuff
   1.250 +//
   1.251 +
   1.252 +template <int NumRanges, int NumDims>
   1.253 +struct index_gen {
   1.254 +private:
   1.255 +  typedef dimtest::index Index;
   1.256 +  typedef size_type SizeType;
   1.257 +  typedef index_range<Index,SizeType> range;
   1.258 +public:
   1.259 +  typedef typename range_list_generator<range,NumRanges>::type range_list;
   1.260 +  range_list ranges_;
   1.261 +
   1.262 +  index_gen() { }
   1.263 +
   1.264 +  template <int ND>
   1.265 +  explicit index_gen(const index_gen<NumRanges-1,ND>& rhs,
   1.266 +            const index_range<Index,SizeType>& range)
   1.267 +  {
   1.268 +    std::copy(rhs.ranges_.begin(),rhs.ranges_.end(),ranges_.begin());
   1.269 +    *ranges_.rbegin() = range;
   1.270 +  }
   1.271 +
   1.272 +  index_gen<NumRanges+1,NumDims+1>
   1.273 +  operator[](const index_range<Index,SizeType>& range) const
   1.274 +  {
   1.275 +    index_gen<NumRanges+1,NumDims+1> tmp;
   1.276 +    std::copy(ranges_.begin(),ranges_.end(),tmp.ranges_.begin());
   1.277 +    *tmp.ranges_.rbegin() = range;
   1.278 +    return tmp;
   1.279 +  }
   1.280 +
   1.281 +  index_gen<NumRanges+1,NumDims>
   1.282 +  operator[](Index idx) const
   1.283 +  {
   1.284 +    index_gen<NumRanges+1,NumDims> tmp;
   1.285 +    std::copy(ranges_.begin(),ranges_.end(),tmp.ranges_.begin());
   1.286 +    *tmp.ranges_.rbegin() = index_range<Index,SizeType>(idx);
   1.287 +    return tmp;
   1.288 +  }    
   1.289 +};
   1.290 +
   1.291 +
   1.292 +template <int NDims, int NRanges>
   1.293 +void accept_gen(index_gen<NRanges,NDims>& indices) {
   1.294 +  // do nothing
   1.295 +}
   1.296 +
   1.297 +template <typename X, typename Y, int A, int B>
   1.298 +class foo { };
   1.299 +
   1.300 +class boo {
   1.301 +
   1.302 +public:
   1.303 +  template <int NDims, int NRanges>
   1.304 +  void operator[](index_gen<NRanges,NDims>& indices) {
   1.305 +
   1.306 +  }
   1.307 +};
   1.308 +
   1.309 +
   1.310 +template <typename X, typename Y, int A1, int A2>
   1.311 +void take_foo(foo<X,Y,A1,A2>& f) { }
   1.312 +
   1.313 +using namespace std;
   1.314 +int main() {
   1.315 +
   1.316 +  index_gen<0,0> indices;
   1.317 +  typedef index_range<dimtest::index,size_type> range;
   1.318 +
   1.319 +  foo<int,std::size_t,1,2> f;
   1.320 +  take_foo(f);
   1.321 +
   1.322 +  indices[range()][range()][range()];  
   1.323 +  accept_gen(indices);
   1.324 +  index_gen<0,0> index_g;
   1.325 +  accept_gen(index_g);
   1.326 +  index_gen<3,3> indices_3;
   1.327 +  accept_gen(indices_3);
   1.328 +  
   1.329 +  boo b;
   1.330 +  b[indices_3];
   1.331 +  
   1.332 +  #ifdef __SYMBIAN32__
   1.333 +   	testResultXml("dimtest");
   1.334 +	close_log_file();
   1.335 +#endif
   1.336 +  return 0;
   1.337 +}