/usr/include/polymake/next/SparseVector.h is in libpolymake-dev-common 3.2r2-3.
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 | /* Copyright (c) 1997-2018
Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
http://www.polymake.org
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, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
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.
--------------------------------------------------------------------------------
*/
/** @file SparseVector.h
@brief Implementation of pm::SparseVector class
*/
#ifndef POLYMAKE_SPARSE_VECTOR_H
#define POLYMAKE_SPARSE_VECTOR_H
#include "polymake/GenericVector.h"
#include "polymake/internal/tree_containers.h"
#include "polymake/internal/sparse.h"
#include "polymake/internal/shared_object.h"
#include "polymake/internal/AVL.h"
namespace pm {
template <typename NodeRef>
class sparse_vector_accessor : public AVL::node_accessor<NodeRef> {
typedef AVL::node_accessor<NodeRef> _super;
public:
typedef typename inherit_ref<typename deref<NodeRef>::type::mapped_type, typename _super::argument_type>::type result_type;
result_type operator() (typename _super::argument_type n) const { return n.key_and_data.second; }
};
template <typename NodeRef>
struct sparse_vector_index_accessor {
typedef NodeRef argument_type;
typedef const int result_type;
result_type operator() (NodeRef n) const { return n.key_and_data.first; }
};
/** \ref vector_sec "Vector type" class which is an associative container with
element indices (coordinates) as keys; elements equal to the default value
(@c ElementType(), which is 0 for most numerical types) are not stored,
but implicitly encoded by the gaps in the key set. It is based on an AVL
tree.
*/
template <typename E>
class SparseVector
: public modified_tree< SparseVector<E>,
mlist< ContainerTag< AVL::tree< AVL::traits<int, E, operations::cmp> > >,
OperationTag< pair< BuildUnary<sparse_vector_accessor>,
BuildUnary<sparse_vector_index_accessor> > > > >
, public GenericVector<SparseVector<E>, E> {
typedef modified_tree<SparseVector> base_t;
protected:
typedef AVL::tree< AVL::traits<int, E, operations::cmp> > tree_type;
struct impl {
tree_type tree;
int d;
impl() : d(0) {}
void clear() { d=0; tree.clear(); }
};
shared_object<impl, AliasHandlerTag<shared_alias_handler>> data;
friend SparseVector& make_mutable_alias(SparseVector& alias, SparseVector& owner)
{
alias.data.make_mutable_alias(owner.data);
return alias;
}
typedef sparse_proxy_base<SparseVector> proxy_base;
template <typename Iterator>
void init(Iterator&& src, int dim_arg)
{
data.get()->d=dim_arg;
data.get()->tree.assign(std::forward<Iterator>(src));
}
public:
using typename GenericVector<SparseVector>::generic_type;
typedef random_access_iterator_tag container_category;
typedef sparse_elem_proxy<proxy_base> reference;
tree_type& get_container() { return data->tree; }
const tree_type& get_container() const { return data->tree; }
/// tell the current vector dimension, i.e., the number of non-zero elements (may differ from size)
int dim() const { return data->d; }
/// create as empty
SparseVector() {}
/// create vector of length n, (implicitly) initialize all elements with 0
explicit SparseVector(int dim_arg) { data.get()->d=dim_arg; }
/** Create a vector of dimension n, initialize the elements from a data sequence.
Iterator can be either indexed, or supply index-value pairs, e.g. std::pair<int,ElementType> or a plain sequence of data items. In the latter case zero elements are filtered out.*/
template <typename Iterator, bool enabled=construct_sparse_iterator<Iterator, E>::enabled>
SparseVector(int dim_arg, Iterator&& src)
{
init(construct_sparse_iterator<Iterator, E>()(std::forward<Iterator>(src), dim_arg), dim_arg);
}
/// Copy of a disguised SparseVector object.
SparseVector(const GenericVector<SparseVector>& v)
: data(v.top().data) {}
/// Create a vector as a copy of another vector of the same element type.
template <typename Vector2>
SparseVector(const GenericVector<Vector2, E>& v)
{
init(ensure(v.top(), (pure_sparse*)0).begin(), v.dim());
}
/// Create a vector as a copy of another vector with a different element type.
template <typename Vector2, typename E2>
explicit SparseVector(const GenericVector<Vector2, E2>& v,
typename std::enable_if<can_initialize<E2, E>::value, void**>::type=nullptr)
{
init(make_converting_iterator<E>(ensure(v.top(), (pure_sparse*)0).begin()), v.dim());
}
/// Create a vector from a list of values.
/// Zeroes are filtered out automatically.
template <typename E2, typename=typename std::enable_if<can_initialize<E2, E>::value>::type>
SparseVector(std::initializer_list<E2> l)
{
init(make_converting_iterator<E>(ensure(l, (pure_sparse*)0).begin()), l.size());
}
/// Create a vector from a list of (index, value) pairs.
/// Parameter @a d specifies the dimension.
/// Indices don't have to come in ascending order; zero values are not detected.
SparseVector(int d, std::initializer_list<std::pair<int, E>> l)
{
init(entire(l), d);
}
/// truncate to zero size
void clear() { data.apply(shared_clear()); }
/// change the size, initialize appended elements with default constructor
void resize(int n)
{
if (n < data->d) {
typename base_t::reverse_iterator i=this->rbegin();
while (!i.at_end() && i.index()>=n) this->erase(i++);
}
data->d=n;
}
SparseVector& operator= (const SparseVector& other) { assign(other); return *this; }
using generic_type::operator=;
void swap(SparseVector& v) { data.swap(v.data); }
friend void relocate(SparseVector* from, SparseVector* to)
{
relocate(&from->data, &to->data);
}
/// random access, may cost O(log(n)) time; \ref vector_performance
reference operator[] (int i)
{
if (POLYMAKE_DEBUG) {
if (i<0 || i>=dim())
throw std::runtime_error("SparseVector::operator[] - index out of range");
}
return proxy_base(*this,i);
}
/// constant random access, may cost O(log(n)) time; \ref vector_performance
const E& operator[] (int i) const
{
if (POLYMAKE_DEBUG) {
if (i<0 || i>=dim())
throw std::runtime_error("SparseVector::operator[] - index out of range");
}
return deref_sparse_iterator(this->find(i));
}
/// append a GenericVector
template <typename Vector2, typename E2,
typename=typename std::enable_if<can_initialize<E2, E>::value>::type>
SparseVector& operator|= (const GenericVector<Vector2, E2>& v)
{
append(v.dim(), make_converting_iterator<E2>(ensure(v.top(), (pure_sparse*)0).begin()));
return *this;
}
/// append an element
template <typename E2,
typename=typename std::enable_if<can_initialize<E2, E>::value>::type>
SparseVector& operator|= (E2&& r)
{
if (!is_zero(r)) data->tree.push_back(data->d, std::forward<E2>(r));
++(data->d);
return *this;
}
/// append a list of elements
/// zeroes are filtered out automatically
template <typename E2,
typename=typename std::enable_if<can_initialize<E2, E>::value>::type>
SparseVector& operator|= (std::initializer_list<E2> l)
{
append(l.size(), make_converting_iterator<E2>(ensure(l, (pure_sparse*)0).begin()));
return *this;
}
#if POLYMAKE_DEBUG
void check() const { data->tree.check(""); }
void tree_dump() const { data->tree.dump(); }
#endif
protected:
template <typename, typename> friend class GenericVector;
void assign(const SparseVector& v) { data=v.data; }
template <typename Vector2>
void assign(const Vector2& v)
{
if (data.is_shared()) {
*this=SparseVector(v);
} else {
data.get()->tree.assign(make_converting_iterator<E>(ensure(v, (pure_sparse*)0).begin()));
data.get()->d=get_dim(v);
}
}
template <typename Operation>
void assign_op(const Operation& op)
{
if (data.is_shared())
*this=SparseVector(LazyVector1<const SparseVector&, Operation>(*this, op));
else
generic_type::assign_op(op);
}
template <typename Vector2, typename Operation>
void assign_op(const Vector2& v, const Operation& op)
{
if (data.is_shared())
*this=SparseVector(LazyVector2<const SparseVector&, const Vector2&, Operation>(*this, v, op));
else
generic_type::assign_op(v,op);
}
template <typename Iterator>
void append(int added, Iterator&& src)
{
const int d=data->d;
tree_type& t=data->tree;
for (; !src.at_end(); ++src)
t.push_back(src.index()+d, *src);
data->d += added;
}
template <typename E2>
void fill_impl(const E2& x, pure_sparse)
{
data->tree.clear();
if (!is_zero(x)) {
tree_type& t=data.get()->tree;
const int d=data.get()->d;
for (int i=0; i<d; ++i) t.push_back(i,x);
}
}
};
template <typename E>
struct check_container_feature<SparseVector<E>, pure_sparse> : std::true_type {};
template <typename TVector, typename E, typename Permutation> inline
typename std::enable_if<TVector::is_sparse, SparseVector<E>>::type
permuted(const GenericVector<TVector, E>& v, const Permutation& perm)
{
if (POLYMAKE_DEBUG || !Unwary<TVector>::value) {
if (v.dim() != perm.size())
throw std::runtime_error("permuted - dimension mismatch");
}
SparseVector<E> result(v.dim());
for (auto p=ensure(perm, (cons<end_sensitive,indexed>*)0).begin(); !p.at_end(); ++p) {
auto e=v.top().find(*p);
if (!e.at_end()) result.push_back(p.index(), *e);
}
return result;
}
template <typename TVector, typename E, typename Permutation> inline
typename std::enable_if<TVector::is_sparse, SparseVector<E>>::type
permuted_inv(const GenericVector<TVector, E>& v, const Permutation& perm)
{
if (POLYMAKE_DEBUG || !Unwary<TVector>::value) {
if (v.dim() != perm.size())
throw std::runtime_error("permuted_inv - dimension mismatch");
}
SparseVector<E> result(v.dim());
int pos=0;
auto p=perm.begin();
for (auto e=entire(v.top()); !e.at_end(); ++e) {
std::advance(p, e.index()-pos);
pos=e.index();
result.insert(*p,*e);
}
return result;
}
} // end namespace pm
namespace polymake {
using pm::SparseVector;
}
namespace std {
template <typename E> inline
void swap(pm::SparseVector<E>& v1, pm::SparseVector<E>& v2) { v1.swap(v2); }
}
#endif // POLYMAKE_SPARSE_VECTOR_H
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|