File size: 13,708 Bytes
9375c9a |
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 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
// Copyright (C) 2003 Davis E. King ([email protected])
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_BIGINT_KERNEl_1_
#define DLIB_BIGINT_KERNEl_1_
#include "bigint_kernel_abstract.h"
#include "../algs.h"
#include "../serialize.h"
#include "../uintn.h"
#include <iosfwd>
namespace dlib
{
class bigint_kernel_1
{
/*!
INITIAL VALUE
slack == 25
data->number[0] == 0
data->size == slack
data->references == 1
data->digits_used == 1
CONVENTION
slack == the number of extra digits placed into the number when it is
created. the slack value should never be less than 1
data->number == pointer to an array of data->size uint16s.
data represents a string of base 65535 numbers with data[0] being
the least significant bit and data[data->digits_used-1] being the most
significant
NOTE: In the comments I will consider a word to be a 16 bit value
data->digits_used == the number of significant digits in the number.
data->digits_used tells us the number of used elements in the
data->number array so everything beyond data->number[data->digits_used-1]
is undefined
data->references == the number of bigint_kernel_1 objects which refer
to this data_record
!*/
struct data_record
{
explicit data_record(
uint32 size_
) :
size(size_),
number(new uint16[size_]),
references(1),
digits_used(1)
{*number = 0;}
/*!
ensures
- initializes *this to represent zero
!*/
data_record(
const data_record& item,
uint32 additional_size
) :
size(item.digits_used + additional_size),
number(new uint16[size]),
references(1),
digits_used(item.digits_used)
{
uint16* source = item.number;
uint16* dest = number;
uint16* end = source + digits_used;
while (source != end)
{
*dest = *source;
++dest;
++source;
}
}
/*!
ensures
- *this is a copy of item except with
size == item.digits_used + additional_size
!*/
~data_record(
)
{
delete [] number;
}
const uint32 size;
uint16* number;
uint32 references;
uint32 digits_used;
private:
// no copy constructor
data_record ( data_record&);
};
// note that the second parameter is just there
// to resolve the ambiguity between this constructor and
// bigint_kernel_1(uint32)
explicit bigint_kernel_1 (
data_record* data_, int
): slack(25),data(data_) {}
/*!
ensures
- *this is initialized with data_ as its data member
!*/
public:
bigint_kernel_1 (
);
bigint_kernel_1 (
uint32 value
);
bigint_kernel_1 (
const bigint_kernel_1& item
);
virtual ~bigint_kernel_1 (
);
const bigint_kernel_1 operator+ (
const bigint_kernel_1& rhs
) const;
bigint_kernel_1& operator+= (
const bigint_kernel_1& rhs
);
const bigint_kernel_1 operator- (
const bigint_kernel_1& rhs
) const;
bigint_kernel_1& operator-= (
const bigint_kernel_1& rhs
);
const bigint_kernel_1 operator* (
const bigint_kernel_1& rhs
) const;
bigint_kernel_1& operator*= (
const bigint_kernel_1& rhs
);
const bigint_kernel_1 operator/ (
const bigint_kernel_1& rhs
) const;
bigint_kernel_1& operator/= (
const bigint_kernel_1& rhs
);
const bigint_kernel_1 operator% (
const bigint_kernel_1& rhs
) const;
bigint_kernel_1& operator%= (
const bigint_kernel_1& rhs
);
bool operator < (
const bigint_kernel_1& rhs
) const;
bool operator == (
const bigint_kernel_1& rhs
) const;
bigint_kernel_1& operator= (
const bigint_kernel_1& rhs
);
friend std::ostream& operator<< (
std::ostream& out,
const bigint_kernel_1& rhs
);
friend std::istream& operator>> (
std::istream& in,
bigint_kernel_1& rhs
);
bigint_kernel_1& operator++ (
);
const bigint_kernel_1 operator++ (
int
);
bigint_kernel_1& operator-- (
);
const bigint_kernel_1 operator-- (
int
);
friend const bigint_kernel_1 operator+ (
uint16 lhs,
const bigint_kernel_1& rhs
);
friend const bigint_kernel_1 operator+ (
const bigint_kernel_1& lhs,
uint16 rhs
);
bigint_kernel_1& operator+= (
uint16 rhs
);
friend const bigint_kernel_1 operator- (
uint16 lhs,
const bigint_kernel_1& rhs
);
friend const bigint_kernel_1 operator- (
const bigint_kernel_1& lhs,
uint16 rhs
);
bigint_kernel_1& operator-= (
uint16 rhs
);
friend const bigint_kernel_1 operator* (
uint16 lhs,
const bigint_kernel_1& rhs
);
friend const bigint_kernel_1 operator* (
const bigint_kernel_1& lhs,
uint16 rhs
);
bigint_kernel_1& operator*= (
uint16 rhs
);
friend const bigint_kernel_1 operator/ (
uint16 lhs,
const bigint_kernel_1& rhs
);
friend const bigint_kernel_1 operator/ (
const bigint_kernel_1& lhs,
uint16 rhs
);
bigint_kernel_1& operator/= (
uint16 rhs
);
friend const bigint_kernel_1 operator% (
uint16 lhs,
const bigint_kernel_1& rhs
);
friend const bigint_kernel_1 operator% (
const bigint_kernel_1& lhs,
uint16 rhs
);
bigint_kernel_1& operator%= (
uint16 rhs
);
friend bool operator < (
uint16 lhs,
const bigint_kernel_1& rhs
);
friend bool operator < (
const bigint_kernel_1& lhs,
uint16 rhs
);
friend bool operator == (
const bigint_kernel_1& lhs,
uint16 rhs
);
friend bool operator == (
uint16 lhs,
const bigint_kernel_1& rhs
);
bigint_kernel_1& operator= (
uint16 rhs
);
void swap (
bigint_kernel_1& item
) { data_record* temp = data; data = item.data; item.data = temp; }
private:
void long_add (
const data_record* lhs,
const data_record* rhs,
data_record* result
) const;
/*!
requires
- result->size >= max(lhs->digits_used,rhs->digits_used) + 1
ensures
- result == lhs + rhs
!*/
void long_sub (
const data_record* lhs,
const data_record* rhs,
data_record* result
) const;
/*!
requires
- lhs >= rhs
- result->size >= lhs->digits_used
ensures
- result == lhs - rhs
!*/
void long_div (
const data_record* lhs,
const data_record* rhs,
data_record* result,
data_record* remainder
) const;
/*!
requires
- rhs != 0
- result->size >= lhs->digits_used
- remainder->size >= lhs->digits_used
- each parameter is unique (i.e. lhs != result, lhs != remainder, etc.)
ensures
- result == lhs / rhs
- remainder == lhs % rhs
!*/
void long_mul (
const data_record* lhs,
const data_record* rhs,
data_record* result
) const;
/*!
requires
- result->size >= lhs->digits_used + rhs->digits_used
- result != lhs
- result != rhs
ensures
- result == lhs * rhs
!*/
void short_add (
const data_record* data,
uint16 value,
data_record* result
) const;
/*!
requires
- result->size >= data->size + 1
ensures
- result == data + value
!*/
void short_sub (
const data_record* data,
uint16 value,
data_record* result
) const;
/*!
requires
- data >= value
- result->size >= data->digits_used
ensures
- result == data - value
!*/
void short_mul (
const data_record* data,
uint16 value,
data_record* result
) const;
/*!
requires
- result->size >= data->digits_used + 1
ensures
- result == data * value
!*/
void short_div (
const data_record* data,
uint16 value,
data_record* result,
uint16& remainder
) const;
/*!
requires
- value != 0
- result->size >= data->digits_used
ensures
- result = data*value
- remainder = data%value
!*/
void shift_left (
const data_record* data,
data_record* result,
uint32 shift_amount
) const;
/*!
requires
- result->size >= data->digits_used + shift_amount/8 + 1
ensures
- result == data << shift_amount
!*/
void shift_right (
const data_record* data,
data_record* result
) const;
/*!
requires
- result->size >= data->digits_used
ensures
- result == data >> 1
!*/
bool is_less_than (
const data_record* lhs,
const data_record* rhs
) const;
/*!
ensures
- returns true if lhs < rhs
- returns false otherwise
!*/
bool is_equal_to (
const data_record* lhs,
const data_record* rhs
) const;
/*!
ensures
- returns true if lhs == rhs
- returns false otherwise
!*/
void increment (
const data_record* source,
data_record* dest
) const;
/*!
requires
- dest->size >= source->digits_used + 1
ensures
- dest = source + 1
!*/
void decrement (
const data_record* source,
data_record* dest
) const;
/*!
requires
source != 0
ensuers
dest = source - 1
!*/
// member data
const uint32 slack;
data_record* data;
};
inline void swap (
bigint_kernel_1& a,
bigint_kernel_1& b
) { a.swap(b); }
inline void serialize (
const bigint_kernel_1& item,
std::ostream& out
)
{
std::ios::fmtflags oldflags = out.flags();
out << item << ' ';
out.flags(oldflags);
if (!out) throw serialization_error("Error serializing object of type bigint_kernel_c");
}
inline void deserialize (
bigint_kernel_1& item,
std::istream& in
)
{
std::ios::fmtflags oldflags = in.flags();
in >> item;
in.flags(oldflags);
if (in.get() != ' ')
{
item = 0;
throw serialization_error("Error deserializing object of type bigint_kernel_c");
}
}
inline bool operator> (const bigint_kernel_1& a, const bigint_kernel_1& b) { return b < a; }
inline bool operator!= (const bigint_kernel_1& a, const bigint_kernel_1& b) { return !(a == b); }
inline bool operator<= (const bigint_kernel_1& a, const bigint_kernel_1& b) { return !(b < a); }
inline bool operator>= (const bigint_kernel_1& a, const bigint_kernel_1& b) { return !(a < b); }
}
#ifdef NO_MAKEFILE
#include "bigint_kernel_1.cpp"
#endif
#endif // DLIB_BIGINT_KERNEl_1_
|