/usr/include/cln/ring.h is in libcln-dev 1.3.3-1ubuntu1.
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 462 463 464 | // Ring operations.
#ifndef _CL_RING_H
#define _CL_RING_H
#include "cln/object.h"
#include "cln/malloc.h"
#include "cln/proplist.h"
#include "cln/number.h"
#include "cln/exception.h"
#include "cln/io.h"
namespace cln {
class cl_I;
// This file defines the general layout of rings, ring elements, and
// operations available on ring elements. Any subclass of `cl_ring'
// must implement these operations, with the same memory layout.
// (Because generic packages like the polynomial rings access the base
// ring's operation vectors through inline functions defined in this file.)
class cl_heap_ring;
// Rings are reference counted, but not freed immediately when they aren't
// used any more. Hence they inherit from `cl_rcpointer'.
// Vectors of function pointers are more efficient than virtual member
// functions. But it constrains us not to use multiple or virtual inheritance.
//
// Note! We are passing raw `cl_heap_ring*' pointers to the operations
// for efficiency (compared to passing `const cl_ring&', we save a memory
// access, and it is easier to cast to a `cl_heap_ring_specialized*').
// These raw pointers are meant to be used downward (in the dynamic extent
// of the call) only. If you need to save them in a data structure, cast
// to `cl_ring'; this will correctly increment the reference count.
// (This technique is safe because the inline wrapper functions make sure
// that we have a `cl_ring' somewhere containing the pointer, so there
// is no danger of dangling pointers.)
//
// Note! Because the `cl_heap_ring*' -> `cl_ring' conversion increments
// the reference count, you have to use the `cl_private_thing' -> `cl_ring'
// conversion if the reference count is already incremented.
class cl_ring : public cl_rcpointer {
public:
// Constructor. Takes a cl_heap_ring*, increments its refcount.
cl_ring (cl_heap_ring* r);
// Private constructor. Doesn't increment the refcount.
cl_ring (cl_private_thing);
// Copy constructor.
cl_ring (const cl_ring&);
// Assignment operator.
cl_ring& operator= (const cl_ring&);
// Default constructor.
cl_ring ();
// Automatic dereferencing.
cl_heap_ring* operator-> () const
{ return (cl_heap_ring*)heappointer; }
};
CL_DEFINE_COPY_CONSTRUCTOR2(cl_ring,cl_rcpointer)
CL_DEFINE_ASSIGNMENT_OPERATOR(cl_ring,cl_ring)
// Normal constructor for `cl_ring'.
inline cl_ring::cl_ring (cl_heap_ring* r)
{ cl_inc_pointer_refcount((cl_heap*)r); pointer = r; }
// Private constructor for `cl_ring'.
inline cl_ring::cl_ring (cl_private_thing p)
{ pointer = p; }
inline bool operator== (const cl_ring& R1, const cl_ring& R2)
{ return (R1.pointer == R2.pointer); }
inline bool operator!= (const cl_ring& R1, const cl_ring& R2)
{ return (R1.pointer != R2.pointer); }
inline bool operator== (const cl_ring& R1, cl_heap_ring* R2)
{ return (R1.pointer == R2); }
inline bool operator!= (const cl_ring& R1, cl_heap_ring* R2)
{ return (R1.pointer != R2); }
// Representation of an element of a ring.
//
// In order to support true polymorphism (without C++ templates), all
// ring elements share the same basic layout:
// cl_ring ring; // the ring
// cl_gcobject rep; // representation of the element
// The representation of the element depends on the ring, of course,
// but we constrain it to be a single pointer into the heap or an immediate
// value.
//
// Any arithmetic operation on a ring R (like +, -, *) must return a value
// with ring = R. This is
// a. necessary if the computation is to proceed correctly (e.g. in cl_RA,
// ((3/4)*4 mod 3) is 0, simplifying it to ((cl_I)4 mod (cl_I)3) = 1
// wouldn't be correct),
// b. possible even if R is an extension ring of some ring R1 (e.g. cl_N
// being an extension ring of cl_R). Automatic retraction from R to R1
// can be done through dynamic typing: An element of R which happens
// to lie in R1 is stored using the internal representation of R1,
// but with ring = R. Elements of R1 and R\R1 can be distinguished
// through rep's type.
// c. an advantage for the implementation of polynomials and other
// entities which contain many elements of the same ring. They need
// to store only the elements' representations, and a single pointer
// to the ring.
//
// The ring operations exist in two versions:
// - Low-level version, which only operates on the representation.
// - High-level version, which operates on full cl_ring_elements.
// We make this distinction for performance: Multiplication of polynomials
// over Z/nZ, operating on the high-level operations, spends 40% of its
// computing time with packing and unpacking of cl_ring_elements.
// The low-level versions have an underscore prepended and are unsafe.
class _cl_ring_element {
public:
cl_gcobject rep; // representation of the element
// Default constructor.
_cl_ring_element ();
public: /* ugh */
// Constructor.
_cl_ring_element (const cl_heap_ring* R, const cl_gcobject& r) : rep (as_cl_private_thing(r)) { (void)R; }
_cl_ring_element (const cl_ring& R, const cl_gcobject& r) : rep (as_cl_private_thing(r)) { (void)R; }
public: // Ability to place an object at a given address.
void* operator new (size_t size) { return malloc_hook(size); }
void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
void operator delete (void* ptr) { free_hook(ptr); }
};
class cl_ring_element : public _cl_ring_element {
protected:
cl_ring _ring; // ring
public:
const cl_ring& ring () const { return _ring; }
// Default constructor.
cl_ring_element ();
public: /* ugh */
// Constructor.
cl_ring_element (const cl_ring& R, const cl_gcobject& r) : _cl_ring_element (R,r), _ring (R) {}
cl_ring_element (const cl_ring& R, const _cl_ring_element& r) : _cl_ring_element (r), _ring (R) {}
public: // Debugging output.
void debug_print () const;
// Ability to place an object at a given address.
void* operator new (size_t size) { return malloc_hook(size); }
void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
void operator delete (void* ptr) { free_hook(ptr); }
};
// The ring operations are encoded as vectors of function pointers. You
// can add more operations to the end of each vector or add new vectors,
// but you must not reorder the operations nor reorder the vectors nor
// change the functions' signatures incompatibly.
// There should ideally be a template class for each vector, but unfortunately
// you lose the ability to initialize the vector using "= { ... }" syntax
// when you subclass it.
struct _cl_ring_setops {
// print
void (* fprint) (cl_heap_ring* R, std::ostream& stream, const _cl_ring_element& x);
// equality
bool (* equal) (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y);
// ...
};
struct _cl_ring_addops {
// 0
const _cl_ring_element (* zero) (cl_heap_ring* R);
bool (* zerop) (cl_heap_ring* R, const _cl_ring_element& x);
// x+y
const _cl_ring_element (* plus) (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y);
// x-y
const _cl_ring_element (* minus) (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y);
// -x
const _cl_ring_element (* uminus) (cl_heap_ring* R, const _cl_ring_element& x);
// ...
};
struct _cl_ring_mulops {
// 1
const _cl_ring_element (* one) (cl_heap_ring* R);
// canonical homomorphism
const _cl_ring_element (* canonhom) (cl_heap_ring* R, const cl_I& x);
// x*y
const _cl_ring_element (* mul) (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y);
// x^2
const _cl_ring_element (* square) (cl_heap_ring* R, const _cl_ring_element& x);
// x^y, y Integer >0
const _cl_ring_element (* expt_pos) (cl_heap_ring* R, const _cl_ring_element& x, const cl_I& y);
// ...
};
typedef const _cl_ring_setops cl_ring_setops;
typedef const _cl_ring_addops cl_ring_addops;
typedef const _cl_ring_mulops cl_ring_mulops;
// Representation of a ring in memory.
class cl_heap_ring : public cl_heap {
public:
// Allocation.
void* operator new (size_t size) { return malloc_hook(size); }
// Deallocation.
void operator delete (void* ptr) { free_hook(ptr); }
private:
cl_property_list properties;
protected:
cl_ring_setops* setops;
cl_ring_addops* addops;
cl_ring_mulops* mulops;
public:
// More information comes here.
// ...
public:
// Low-level operations.
void _fprint (std::ostream& stream, const _cl_ring_element& x)
{ setops->fprint(this,stream,x); }
bool _equal (const _cl_ring_element& x, const _cl_ring_element& y)
{ return setops->equal(this,x,y); }
const _cl_ring_element _zero ()
{ return addops->zero(this); }
bool _zerop (const _cl_ring_element& x)
{ return addops->zerop(this,x); }
const _cl_ring_element _plus (const _cl_ring_element& x, const _cl_ring_element& y)
{ return addops->plus(this,x,y); }
const _cl_ring_element _minus (const _cl_ring_element& x, const _cl_ring_element& y)
{ return addops->minus(this,x,y); }
const _cl_ring_element _uminus (const _cl_ring_element& x)
{ return addops->uminus(this,x); }
const _cl_ring_element _one ()
{ return mulops->one(this); }
const _cl_ring_element _canonhom (const cl_I& x)
{ return mulops->canonhom(this,x); }
const _cl_ring_element _mul (const _cl_ring_element& x, const _cl_ring_element& y)
{ return mulops->mul(this,x,y); }
const _cl_ring_element _square (const _cl_ring_element& x)
{ return mulops->square(this,x); }
const _cl_ring_element _expt_pos (const _cl_ring_element& x, const cl_I& y)
{ return mulops->expt_pos(this,x,y); }
// High-level operations.
void fprint (std::ostream& stream, const cl_ring_element& x)
{
if (!(x.ring() == this)) throw runtime_exception();
_fprint(stream,x);
}
bool equal (const cl_ring_element& x, const cl_ring_element& y)
{
if (!(x.ring() == this)) throw runtime_exception();
if (!(y.ring() == this)) throw runtime_exception();
return _equal(x,y);
}
const cl_ring_element zero ()
{
return cl_ring_element(this,_zero());
}
bool zerop (const cl_ring_element& x)
{
if (!(x.ring() == this)) throw runtime_exception();
return _zerop(x);
}
const cl_ring_element plus (const cl_ring_element& x, const cl_ring_element& y)
{
if (!(x.ring() == this)) throw runtime_exception();
if (!(y.ring() == this)) throw runtime_exception();
return cl_ring_element(this,_plus(x,y));
}
const cl_ring_element minus (const cl_ring_element& x, const cl_ring_element& y)
{
if (!(x.ring() == this)) throw runtime_exception();
if (!(y.ring() == this)) throw runtime_exception();
return cl_ring_element(this,_minus(x,y));
}
const cl_ring_element uminus (const cl_ring_element& x)
{
if (!(x.ring() == this)) throw runtime_exception();
return cl_ring_element(this,_uminus(x));
}
const cl_ring_element one ()
{
return cl_ring_element(this,_one());
}
const cl_ring_element canonhom (const cl_I& x)
{
return cl_ring_element(this,_canonhom(x));
}
const cl_ring_element mul (const cl_ring_element& x, const cl_ring_element& y)
{
if (!(x.ring() == this)) throw runtime_exception();
if (!(y.ring() == this)) throw runtime_exception();
return cl_ring_element(this,_mul(x,y));
}
const cl_ring_element square (const cl_ring_element& x)
{
if (!(x.ring() == this)) throw runtime_exception();
return cl_ring_element(this,_square(x));
}
const cl_ring_element expt_pos (const cl_ring_element& x, const cl_I& y)
{
if (!(x.ring() == this)) throw runtime_exception();
return cl_ring_element(this,_expt_pos(x,y));
}
// Property operations.
cl_property* get_property (const cl_symbol& key)
{ return properties.get_property(key); }
void add_property (cl_property* new_property)
{ properties.add_property(new_property); }
// Constructor.
cl_heap_ring (cl_ring_setops* setopv, cl_ring_addops* addopv, cl_ring_mulops* mulopv)
: setops (setopv), addops (addopv), mulops (mulopv)
{ refcount = 0; } // will be incremented by the `cl_ring' constructor
};
#define SUBCLASS_cl_heap_ring() \
public: \
/* Allocation. */ \
void* operator new (size_t size) { return malloc_hook(size); } \
/* Deallocation. */ \
void operator delete (void* ptr) { free_hook(ptr); }
// Operations on ring elements.
// Output.
inline void fprint (std::ostream& stream, const cl_ring_element& x)
{ x.ring()->fprint(stream,x); }
CL_DEFINE_PRINT_OPERATOR(cl_ring_element)
// Add.
inline const cl_ring_element operator+ (const cl_ring_element& x, const cl_ring_element& y)
{ return x.ring()->plus(x,y); }
// Negate.
inline const cl_ring_element operator- (const cl_ring_element& x)
{ return x.ring()->uminus(x); }
// Subtract.
inline const cl_ring_element operator- (const cl_ring_element& x, const cl_ring_element& y)
{ return x.ring()->minus(x,y); }
// Equality.
inline bool operator== (const cl_ring_element& x, const cl_ring_element& y)
{ return x.ring()->equal(x,y); }
inline bool operator!= (const cl_ring_element& x, const cl_ring_element& y)
{ return !x.ring()->equal(x,y); }
// Compare against 0.
inline bool zerop (const cl_ring_element& x)
{ return x.ring()->zerop(x); }
// Multiply.
inline const cl_ring_element operator* (const cl_ring_element& x, const cl_ring_element& y)
{ return x.ring()->mul(x,y); }
// Squaring.
inline const cl_ring_element square (const cl_ring_element& x)
{ return x.ring()->square(x); }
// Exponentiation x^y, where y > 0.
inline const cl_ring_element expt_pos (const cl_ring_element& x, const cl_I& y)
{ return x.ring()->expt_pos(x,y); }
// Scalar multiplication.
// [Is this operation worth being specially optimized for the case of
// polynomials?? Polynomials have a faster scalar multiplication.
// We should use it.??]
inline const cl_ring_element operator* (const cl_I& x, const cl_ring_element& y)
{ return y.ring()->mul(y.ring()->canonhom(x),y); }
inline const cl_ring_element operator* (const cl_ring_element& x, const cl_I& y)
{ return x.ring()->mul(x.ring()->canonhom(y),x); }
// Ring of uninitialized elements.
// Any operation results in an exception being thrown.
// Thrown when an attempt is made to perform an operation on an uninitialized ring.
class uninitialized_ring_exception : public runtime_exception {
public:
uninitialized_ring_exception ();
};
// Thrown when a ring element is uninitialized.
class uninitialized_exception : public runtime_exception {
public:
explicit uninitialized_exception (const _cl_ring_element& obj);
uninitialized_exception (const _cl_ring_element& obj_x, const _cl_ring_element& obj_y);
};
extern const cl_ring cl_no_ring;
extern cl_class cl_class_no_ring;
class cl_no_ring_init_helper
{
static int count;
public:
cl_no_ring_init_helper();
~cl_no_ring_init_helper();
};
static cl_no_ring_init_helper cl_no_ring_init_helper_instance;
inline cl_ring::cl_ring ()
: cl_rcpointer (as_cl_private_thing(cl_no_ring)) {}
inline _cl_ring_element::_cl_ring_element ()
: rep ((cl_private_thing) cl_combine(cl_FN_tag,0)) {}
inline cl_ring_element::cl_ring_element ()
: _cl_ring_element (), _ring () {}
// Support for built-in number rings.
// Beware, they are not optimally efficient.
template <class T>
struct cl_number_ring_ops {
bool (* contains) (const cl_number&);
bool (* equal) (const T&, const T&);
bool (* zerop) (const T&);
const T (* plus) (const T&, const T&);
const T (* minus) (const T&, const T&);
const T (* uminus) (const T&);
const T (* mul) (const T&, const T&);
const T (* square) (const T&);
const T (* expt_pos) (const T&, const cl_I&);
};
class cl_heap_number_ring : public cl_heap_ring {
public:
cl_number_ring_ops<cl_number>* ops;
// Constructor.
cl_heap_number_ring (cl_ring_setops* setopv, cl_ring_addops* addopv, cl_ring_mulops* mulopv, cl_number_ring_ops<cl_number>* opv)
: cl_heap_ring (setopv,addopv,mulopv), ops (opv) {}
};
class cl_number_ring : public cl_ring {
public:
cl_number_ring (cl_heap_number_ring* r)
: cl_ring (r) {}
};
template <class T>
class cl_specialized_number_ring : public cl_number_ring {
public:
cl_specialized_number_ring ();
};
// Type test.
inline bool instanceof (const cl_number& x, const cl_number_ring& R)
{
return ((cl_heap_number_ring*) R.heappointer)->ops->contains(x);
}
// Hack section.
// Conversions to subtypes without checking:
// The2(cl_MI)(x) converts x to a cl_MI, without change of representation!
#define The(type) *(const type *) & cl_identity
#define The2(type) *(const type *) & cl_identity2
// This inline function is for type checking purposes only.
inline const cl_ring& cl_identity (const cl_ring& r) { return r; }
inline const cl_ring_element& cl_identity2 (const cl_ring_element& x) { return x; }
inline const cl_gcobject& cl_identity (const _cl_ring_element& x) { return x.rep; }
// Debugging support.
#ifdef CL_DEBUG
extern int cl_ring_debug_module;
CL_FORCE_LINK(cl_ring_debug_dummy, cl_ring_debug_module)
#endif
} // namespace cln
#endif /* _CL_RING_H */
|