/usr/include/flint/flintxx/vector.h is in libflint-dev 2.4.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | /*=============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2013 Tom Bachmann
******************************************************************************/
// Sketch of a generic vector class.
#ifndef CXX_VECTOR_H
#define CXX_VECTOR_H
#include <string>
#include <sstream>
#include "expression.h"
#include "evaluation_tools.h"
#include "ltuple.h"
#include "mp.h"
namespace flint {
FLINT_DEFINE_BINOP(vector_at)
template<class Underlying_traits, class Operation, class Data>
class vector_expression;
namespace detail {
template<class Traits>
struct vector_wrapper : derived_wrapper2<vector_expression, Traits> { };
template<class Idx, class Operation, class Expr, class Traits>
struct vector_at_traits
{
typedef FLINT_BINOP_ENABLE_RETTYPE(vector_at, Expr, Idx) ref_t;
typedef ref_t cref_t;
static ref_t at(const Expr& v, Idx i)
{return vector_at(v, i);}
};
template<class Idx, class Expr, class Traits>
struct vector_at_traits<Idx, operations::immediate, Expr, Traits>
: Traits { };
}
template<class Underlying_traits, class Operation, class Data>
class vector_expression
: public expression<detail::vector_wrapper<Underlying_traits>, Operation, Data>
{
public:
typedef expression<detail::vector_wrapper<Underlying_traits>,
Operation, Data> base_t;
typedef typename Underlying_traits::ref_t ref_t;
typedef typename Underlying_traits::cref_t cref_t;
typedef typename Underlying_traits::idx_t idx_t;
typedef typename Underlying_traits::underlying_t underlying_t;
typedef typename Underlying_traits::arrayref_t arrayref_t;
typedef typename Underlying_traits::arraysrcref_t arraysrcref_t;
vector_expression() {}
template<class T>
explicit vector_expression(const T& t) : base_t(t) {}
template<class T, class U>
vector_expression(const T& t, const U& u) : base_t(t, u) {}
template<class T, class U, class V>
vector_expression(const T& t, const U& u, const V& v)
: base_t(t, u, v) {}
template<class T>
vector_expression& operator=(const T& t)
{
this->set(t);
return *this;
}
template<class Idx>
typename detail::vector_at_traits<Idx, Operation, vector_expression,
Underlying_traits>::ref_t operator[](Idx idx)
{
return detail::vector_at_traits<Idx, Operation, vector_expression,
Underlying_traits>::at(*this, idx);
}
template<class Idx>
typename detail::vector_at_traits<Idx, Operation, vector_expression,
Underlying_traits>::cref_t operator[](Idx idx) const
{
return detail::vector_at_traits<Idx, Operation, vector_expression,
Underlying_traits>::at(*this, idx);
}
idx_t size() const {return Underlying_traits::size(*this);}
arrayref_t _array() {return Underlying_traits::array(*this);}
arraysrcref_t _array() const {return Underlying_traits::array(*this);}
typename base_t::evaluated_t create_temporary() const
{
return Underlying_traits::create_temporary(*this);
}
protected:
explicit vector_expression(const Data& d) : base_t(d) {}
template<class D, class O, class Da>
friend class expression;
};
namespace vectors {
// Similar to matrices, the size of a vector expression has to be known in
// order to allocate temporary objects. In this case, the generic
// implementation looks for any vector immediate subexpression and returs its
// size. This makes sense since mixing vectors of differing sizes usually makes
// no sense.
// Thus specialisation is usually only necessary in constructor-like operations,
// which do not involve vector immediates.
template<class Operation>
struct outsize
{
template<class Expr>
static unsigned get(const Expr& e)
{
return tools::find_subexpr_T<typename Expr::evaluated_t>(e)._data().size;
}
};
// Hack for ltuple_get, similar to the matrices case.
template<unsigned n>
struct outsize<operations::ltuple_get_op<n> >
{
template<class Expr>
static unsigned get(const Expr& e)
{
return outsize<typename Expr::data_t::head_t::operation_t>::get(
e._data().head);
}
};
}
namespace detail {
template<class T, class Ref, class Cref, class ArrayT>
struct basic_vector_traits
{
typedef unsigned idx_t;
typedef Ref ref_t;
typedef const Cref cref_t;
typedef T underlying_t;
typedef ArrayT* arrayref_t;
typedef const ArrayT* arraysrcref_t;
template<class Expr>
static ref_t at(Expr& e, unsigned i)
{
return e.evaluate()._data().array[i];
}
template<class Expr>
static cref_t at(const Expr& e, unsigned i)
{
return e.evaluate()._data().array[i];
}
template<class Expr>
static arrayref_t array(Expr& e)
{
return e.evaluate()._data().array;
}
template<class Expr>
static arraysrcref_t array(const Expr& e)
{
return e.evaluate()._data().array;
}
};
template<class T, class Ref = T&, class Cref = const T&, class ArrayT = T>
struct rtfixed_size_traits
: basic_vector_traits<T, Ref, Cref, ArrayT>
{
template<class Expr>
static unsigned size(const Expr& e)
{
return vectors::outsize<typename Expr::operation_t>::get(e);
}
template<class Expr>
static typename Expr::evaluated_t create_temporary(const Expr& e)
{
return typename Expr::evaluated_t(e.size());
}
};
template<class T, class Ref = T&, class Cref = const T&, class ArrayT = T>
struct fixed_size_traits
: basic_vector_traits<T, Ref, Cref, ArrayT>
{
template<class Expr>
static unsigned size(const Expr& e)
{
return Expr::evaluated_t::data_t::size;
}
template<class Expr>
static typename Expr::evaluated_t create_temporary(const Expr& e)
{
return typename Expr::evaluated_t();
}
};
template<class T, class Size, class Ref, class Cref, class ArrayT>
struct wrapped_vector_traits
: rtfixed_size_traits<T, Ref, Cref, ArrayT>
{
typedef Size idx_t;
template<class Expr>
static Ref at(Expr& e, idx_t i)
{
return e.evaluate()._data().at(i);
}
template<class Expr>
static Cref at(const Expr& e, idx_t i)
{
return e.evaluate()._data().at(i);
}
};
template<class T>
struct rtfixed_size_data
{
const unsigned size;
T* array;
rtfixed_size_data(unsigned n)
: size(n), array(new T[n]) {}
~rtfixed_size_data() {delete[] array;}
rtfixed_size_data(const rtfixed_size_data& o)
: size(o.size)
{
// TODO this is very non-optimal ... (?)
array = new T[size];
for(unsigned i = 0;i < size;++i)
{
array[i] = o.array[i];
}
}
};
template<class T, unsigned n>
struct fixed_size_data
{
static const unsigned size = n;
T array[n];
};
} // detail
template<class T>
struct make_vector
{
typedef vector_expression<detail::rtfixed_size_traits<T>,
operations::immediate, detail::rtfixed_size_data<T> > type;
};
template<class T, unsigned n>
struct make_vector_n
{
typedef vector_expression<detail::fixed_size_traits<T>,
operations::immediate, detail::fixed_size_data<T, n> > type;
};
template<class Expr>
struct enable_vector_rules : mp::false_ { };
template<class Traits, class Data>
struct enable_vector_rules<
vector_expression<Traits, operations::immediate, Data> >
: mp::true_ { };
namespace rules {
// temporary allocation inside ltuples
template<class Operation, class Data, class U,
class Traits, class Op, class Da>
struct instantiate_temporaries<ltuple_expression<U, Operation, Data>,
vector_expression<Traits, Op, Da> >
{
typedef ltuple_expression<U, Operation, Data> Expr;
typedef vector_expression<Traits, Op, Da> T;
static T get(const Expr& e)
{
return T(vectors::outsize<Operation>::get(e));
}
};
template<class Traits, class Data, class T>
struct binary_expression<vector_expression<Traits, operations::immediate, Data>,
operations::vector_at_op, T>
{
typedef typename Traits::underlying_t return_t;
template<class V>
static void doit(V& to,
const vector_expression<Traits, operations::immediate, Data>& v,
T i)
{
to = Traits::at(v, i);
}
};
template<class Expr>
struct to_string<Expr, typename mp::enable_if<mp::and_<
enable_vector_rules<Expr>,
traits::is_implemented<to_string<typename Expr::underlying_t> > > >::type>
{
static std::string get(const Expr& e, int base)
{
// TODO inefficient
std::string res = "(";
for(typename Expr::idx_t i = 0;i < e.size();++i)
{
res += e[i].to_string();
if(i != e.size() - 1)
res += ", ";
}
res += ")";
return res;
}
};
template<class Expr>
struct equals<Expr, Expr,
typename mp::enable_if<enable_vector_rules<Expr> >::type>
{
static bool get(const Expr& e1, const Expr& e2)
{
if(e1.size() != e2.size())
return false;
for(typename Expr::idx_t i = 0;i < e1.size();++i)
if(e1[i] != e2[i])
return false;
return true;
}
};
namespace rvdetail {
template<class Tuple>
struct translate_data;
template<class Expr, class enable = void>
struct translate_expr
{
typedef translate_data<typename Expr::data_t> trdata_t;
typedef typename Expr::underlying_t ul_t;
typedef typename ul_t::template make_helper<
typename Expr::operation_t, typename trdata_t::type> make_helper;
typedef typename make_helper::type type;
template<class Idx>
static type make(const Expr& e, Idx idx)
{
return make_helper::make(trdata_t::make(e._data(), idx));
}
};
template<class Expr>
struct translate_expr<Expr,
typename mp::enable_if<traits::is_immediate<Expr> >::type>
{
typedef typename Expr::cref_t type;
template<class Idx>
static type make(const Expr& e, Idx idx)
{
return e[idx];
}
};
template<class Head, class Tail>
struct translate_data<tuple<Head, Tail> >
{
typedef translate_expr<typename traits::basetype<Head>::type> trexpr;
typedef translate_data<Tail> trtail;
typedef tuple<typename trexpr::type, typename trtail::type> type;
template<class Idx>
static type make(const tuple<Head, Tail>& e, Idx idx)
{
return type(trexpr::make(e.head, idx), trtail::make(e.tail, idx));
}
};
template<>
struct translate_data<empty_tuple>
{
typedef empty_tuple type;
template<class Idx>
static type make(empty_tuple, Idx) {return empty_tuple();}
};
template<class Data, class Enable = void>
struct enable_evaluation : mp::false_ {typedef void vector_t;};
template<class Data>
struct enable_evaluation<Data,
typename mp::enable_if<traits::is_expression<
typename traits::basetype<Data>::type> >::type>
: enable_vector_rules<typename traits::basetype<Data>::type::evaluated_t>
{
typedef typename traits::basetype<Data>::type::evaluated_t vector_t;
};
template<class Head, class Tail>
struct enable_evaluation<tuple<Head, Tail> >
: mp::and_<enable_evaluation<Head>, enable_evaluation<Tail> >
{
typedef typename enable_evaluation<Head>::vector_t vector_t;
};
template<>
struct enable_evaluation<empty_tuple>
: mp::true_ { };
} //rvdetail
// TODO this is a bit greedy ..
template<class Op, class Data, bool result_is_temporary>
struct evaluation<Op, Data, result_is_temporary, 1,
typename mp::enable_if<rvdetail::enable_evaluation<Data> >::type>
{
typedef rvdetail::translate_data<Data> translator;
typedef typename translator::type trdata_t;
typedef typename mp::find_evaluation<
Op, trdata_t, result_is_temporary>::type rule_t;
typedef typename rvdetail::enable_evaluation<Data>::vector_t vector_t;
typedef typename vector_t::evaluated_t return_t; // TODO
typedef typename rule_t::temporaries_t temporaries_t;
typedef typename rule_t::return_t trreturn_t;
template<class Return>
static void doit(const Data& input, temporaries_t temps, Return* output)
{
for(typename return_t::idx_t i = 0;i < output->size();++i)
{
rule_t::doit(translator::make(input, i), temps, &((*output)[i]));
}
}
};
// TODO scalar multiplication etc
} // rules
} // flint
#endif
|