Old snapshot `BigIntegerLibrary-2005.01.17'; see the ChangeLog file.
[bigint/bigint.git] / BigIntegerUtils.hh
CommitLineData
05780f4b
MM
1/*
2* Matt McCutchen's Big Integer Library
3* http://mysite.verizon.net/mccutchen/bigint/
4*/
5
6#ifndef BIGINTEGERUTILS
7#define BIGINTEGERUTILS
8
9#include "BigInteger.hh"
10#include <string>
11#include <iostream>
12
13/*
14* This file includes:
15* (1) `std::string <=> BigUnsigned/BigInteger' conversion routines easier than `BigUnsignedInABase'
16* (2) << and >> operators for BigUnsigned/BigInteger, std::istream/std::ostream
17*/
18
19// Conversion routines. Base 10 only.
20std::string easyBUtoString(const BigUnsigned &x);
21std::string easyBItoString(const BigInteger &x);
22BigUnsigned easyStringToBU(const std::string &s);
23BigInteger easyStringToBI(const std::string &s);
24
a8b42b68
MM
25// Creates a BigInteger from data such as `char's; read below for details.
26template <class T>
27BigInteger easyDataToBI(const T* data, BigInteger::Index length, BigInteger::Sign sign);
28
05780f4b
MM
29// Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
30std::ostream &operator <<(std::ostream &os, const BigUnsigned &x);
31
32// Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
33// My somewhat arbitrary policy: a negative sign comes before a base indicator (like -0xFF).
34std::ostream &operator <<(std::ostream &os, const BigInteger &x);
35
a8b42b68
MM
36/*
37* =================================
38* BELOW THIS POINT are template definitions; above are declarations. See `NumberlikeArray.hh'.
39*/
40
41/*
42* Converts binary data to a BigInteger.
43* Pass an array `data', its length, and the desired sign.
44*
45* Elements of `data' may be of any type `T' that has the following
46* two properties (this includes almost all integral types):
47*
48* (1) `sizeof(T)' correctly gives the amount of binary data in one
49* value of `T' and is a factor of `sizeof(Blk)'.
50*
51* (2) When a value of `T' is casted to a `Blk', the low bytes of
52* the result contain the desired binary data.
53*/
54template <class T>
55BigInteger easyDataToBI(const T* data, BigInteger::Index length, BigInteger::Sign sign) {
56 // really ceiling(numBytes / sizeof(BigInteger::Blk))
57 unsigned int pieceSizeInBits = 8 * sizeof(T);
58 unsigned int piecesPerBlock = sizeof(BigInteger::Blk) / sizeof(T);
59 unsigned int numBlocks = (length + piecesPerBlock - 1) / piecesPerBlock;
60 std::cout << pieceSizeInBits << ' ' << piecesPerBlock << ' ' << numBlocks << std::endl;
61
62 // Allocate our block array
63 BigInteger::Blk *blocks = new BigInteger::Blk[numBlocks];
64
65 BigInteger::Index blockNum, pieceNum, pieceNumHere;
66
67 // Convert
68 for (BigInteger::Index blockNum = 0, pieceNum = 0; blockNum < numBlocks; blockNum++) {
69 BigInteger::Blk curBlock = 0;
70 for (unsigned int pieceNumHere = 0; pieceNumHere < piecesPerBlock && pieceNum < length;
71 pieceNumHere++, pieceNum++)
72 curBlock |= (BigInteger::Blk(data[pieceNum]) << (pieceSizeInBits * pieceNumHere));
73 blocks[blockNum] = curBlock;
74 std::cout << curBlock << std::endl;
75 }
76
77 // Create the BigInteger.
78 BigInteger x(blocks, numBlocks, sign);
79
80 delete blocks;
81 return x;
82}
83
05780f4b 84#endif