2008-01-11 13:32:00 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2008-01-11 13:32:00 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
2010-10-31 07:23:53 +00:00
|
|
|
#ifndef D_ARRAY_FUN_H
|
|
|
|
#define D_ARRAY_FUN_H
|
2008-01-11 13:32:00 +00:00
|
|
|
|
2012-04-07 12:26:33 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
2008-03-15 04:19:46 +00:00
|
|
|
#include <cstdlib>
|
2008-01-11 13:32:00 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2008-03-15 04:19:46 +00:00
|
|
|
template<typename T, size_t N>
|
2014-08-29 14:37:31 +00:00
|
|
|
constexpr size_t arraySize(T (&)[N])
|
|
|
|
{
|
|
|
|
return N;
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-02-12 13:44:34 +00:00
|
|
|
template<typename T, size_t N>
|
|
|
|
class array_wrapper {
|
|
|
|
private:
|
2010-06-21 13:51:56 +00:00
|
|
|
T array_[N];
|
2009-02-12 13:44:34 +00:00
|
|
|
public:
|
|
|
|
array_wrapper() {}
|
|
|
|
|
|
|
|
operator T*()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return array_;
|
2009-02-12 13:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
operator const T*() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return array_;
|
2009-02-12 13:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-16 13:43:23 +00:00
|
|
|
// Expression Template for array
|
|
|
|
|
|
|
|
namespace expr {
|
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
template<typename L, typename R, typename Op>
|
2009-04-25 17:04:32 +00:00
|
|
|
struct BinExpr {
|
2014-09-16 12:53:35 +00:00
|
|
|
typedef typename Op::result_type value_type;
|
2009-04-25 17:04:32 +00:00
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
BinExpr(L lhs, R rhs, Op op)
|
|
|
|
: lhs(std::move(lhs)), rhs(std::move(rhs)), op(std::move(op))
|
|
|
|
{}
|
2009-04-16 13:43:23 +00:00
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
value_type operator[](size_t i) const
|
2009-04-16 13:43:23 +00:00
|
|
|
{
|
2014-09-16 12:53:35 +00:00
|
|
|
return op(lhs[i], rhs[i]);
|
2009-04-16 13:43:23 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
L lhs;
|
|
|
|
R rhs;
|
|
|
|
Op op;
|
2009-04-16 13:43:23 +00:00
|
|
|
};
|
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
template<typename L, typename R,
|
|
|
|
typename Op = std::bit_and<typename L::value_type>>
|
|
|
|
BinExpr<L, R, Op> operator&(L lhs, R rhs)
|
|
|
|
{
|
|
|
|
return BinExpr<L, R, Op>(std::forward<L>(lhs), std::forward<R>(rhs), Op());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename L, typename R,
|
|
|
|
typename Op = std::bit_or<typename L::value_type>>
|
|
|
|
BinExpr<L, R, Op> operator|(L lhs, R rhs)
|
|
|
|
{
|
|
|
|
return BinExpr<L, R, Op>(std::forward<L>(lhs), std::forward<R>(rhs), Op());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Arg, typename Op>
|
2009-04-25 17:04:32 +00:00
|
|
|
struct UnExpr {
|
2014-09-16 12:53:35 +00:00
|
|
|
typedef typename Op::result_type value_type;
|
2009-04-25 17:04:32 +00:00
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
UnExpr(Arg arg, Op op)
|
|
|
|
: arg(std::move(arg)), op(std::move(op))
|
|
|
|
{}
|
2009-04-25 17:04:32 +00:00
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
value_type operator[](size_t i) const
|
2009-04-25 17:04:32 +00:00
|
|
|
{
|
2014-09-16 12:53:35 +00:00
|
|
|
return op(arg[i]);
|
2009-04-25 17:04:32 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
Arg arg;
|
|
|
|
Op op;
|
2009-04-16 13:43:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2014-09-16 12:53:35 +00:00
|
|
|
struct bit_neg : std::function<T(T)> {
|
|
|
|
T operator()(T t) const
|
|
|
|
{
|
|
|
|
return ~t;
|
|
|
|
}
|
2009-06-23 15:35:45 +00:00
|
|
|
};
|
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
template<typename Arg, typename Op = bit_neg<typename Arg::value_type>>
|
|
|
|
UnExpr<Arg, Op> operator~(Arg arg)
|
2009-04-16 13:43:23 +00:00
|
|
|
{
|
2014-09-16 12:53:35 +00:00
|
|
|
return UnExpr<Arg, Op>(std::forward<Arg>(arg), Op());
|
|
|
|
}
|
2009-04-16 13:43:23 +00:00
|
|
|
|
2009-04-25 17:04:32 +00:00
|
|
|
template<typename T>
|
2014-09-16 12:53:35 +00:00
|
|
|
struct Array {
|
|
|
|
typedef T value_type;
|
2009-04-16 13:43:23 +00:00
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
Array(T* t)
|
|
|
|
: t(t)
|
|
|
|
{}
|
2009-04-16 13:43:23 +00:00
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
T operator[](size_t i) const
|
|
|
|
{
|
|
|
|
return t[i];
|
|
|
|
}
|
2009-04-16 13:43:23 +00:00
|
|
|
|
2014-09-16 12:53:35 +00:00
|
|
|
T* t;
|
2009-04-16 13:43:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2014-09-16 12:53:35 +00:00
|
|
|
Array<T> array(T *t)
|
2009-04-16 13:43:23 +00:00
|
|
|
{
|
2014-09-16 12:53:35 +00:00
|
|
|
return Array<T>(t);
|
2009-04-16 13:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace expr
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|
|
|
|
|
2010-10-31 07:23:53 +00:00
|
|
|
#endif // D_ARRAY_FUN_H
|