insert mpfr_t object as value in a map

173 views Asked by At

I am experiencing troubles to inserting an element of the type std::pair , mpfr_t> into the map. The std::make_pair function calls the error

incompatible types in assignment of ‘__mpfr_struct*’ to ‘__mpfr_struct [1]’

Since I am only passing a pointer to the mpfr_t object to the rec_func function I thought that I could dereference it using the * operator and save the resulting mpfr_t as value in a map.

Background information and structure of the code: rec_func is a recursive function. It is supposed to count certain instances. Because the number of instances is extremely large (10^50 or more) I am using the data type mpfr_t. In order to avoid the recursive function to be called with the same arguments more than once I would like to use dynamic programming (aka. Memoization). For this purpose I am using a map with a vector with three integers as a key and the mpfr_t as the value.

The main function initializes a map and an object of the type mpfr_t. It then calls rec_func and hands on the map and a pointer to the mpfr_t object. Because mpfr_t is actually an array type it cannot be returned and thus I am just passing on a pointer to it.

The gmp and mfpr libraries need to be installed first. sudo apt-get install libgmp3-dev sudo apt-get install libmpfr-dev libmpfr-doc libmpfr4 libmpfr4-dbg

Advice would be very much appreciated.

#include <map>
#include <vector>
#include <iostream>
#include <gmp.h>
#include <mpfr.h>
using namespace std;

void rec_func(std::map <std::vector<int>, mpfr_t>& my_map, mpfr_t*    big_int)
{

  int arr[3] = {1, 2, 3}; 
  std::vector<int> itm(arr, arr+3);

  std::pair <std::vector<int>, mpfr_t> intr;
  intr = std::make_pair(itm, *big_int);
  //my_map.insert(intr);

  //my_map.insert ( std::make_pair(itm, *big_int) ); 

}

int main()
{
  mpfr_t big_int; // initialize
  mpfr_init2(big_int, 200);
  mpfr_set_d(big_int, 1, MPFR_RNDN); // assign value

  std::map <std::vector<int>, mpfr_t> my_map;

  rec_func(my_map, &big_int);

  mpfr_clear(big_int); // clear the big int
  my_map.clear(); // delete the map

}
1

There are 1 answers

0
Edoardo Morassutto On

The mpfr_t type is not a simple struct, in fact, as mpfr.h header, it's an array of size one:

typedef __mpfr_struct mpfr_t[1];

This means that you cannot simply copy it using = (as it does in std::make_pair).

The simplest solution is to use a pointer to mpfr_t and store it in the std::pair (and std::map).