int next_number(GEOSCommand *command, double *value) { int type; int size; char *buffer = command->param_bytes; int *index = &command->index; int ivalue; if(ei_get_type(buffer, index, &type, &size)) { return -1; } int ret = 0; switch(type) { case ERL_SMALL_INTEGER_EXT: case ERL_INTEGER_EXT: if(next_int(command, &ivalue)) { ret = -1; } else *value = ivalue; break; case ERL_FLOAT_EXT: ret = next_double(command, value); break; default: ret = -1; } return ret; }
T next(const T min, const T max) { return static_cast<T>(next_double() * (max - min) + min); }
/* communicate objects using point to point communication */ int COMOBJS (MPI_Comm comm, int tag, OBJ_Pack pack, void *data, OBJ_Unpack unpack, COMOBJ *send, int nsend, COMOBJ **recv, int *nrecv) /* recv is contiguous => free (*recv) releases all memory */ { COMDATA *send_data, *recv_data, *cd, *cc; int recv_count, i, n, ret; COMOBJ *co; MAP *map; MEM mem; ERRMEM (send_data = malloc (nsend * sizeof (COMDATA))); MEM_Init (&mem, sizeof (MAP), MAX (nsend, 64)); /* pack objects */ for (i = 0, cd = send_data, co = send, map = NULL; i < nsend; i ++, cd ++, co ++) { int isize = 0, dsize = 0; cd->rank = co->rank; if ((cc = MAP_Find (map, co->o, NULL))) /* same object was already packed */ { cd->ints = cc->ints; cd->doubles = cc->doubles; cd->i = cc->i; cd->d = cc->d; } else { cd->ints = 0; cd->doubles = 0; cd->i = NULL; cd->d = NULL; pack (co->o, &dsize, &cd->d, &cd->doubles, &isize, &cd->i, &cd->ints); MAP_Insert (&mem, &map, co->o, cd, NULL); } } /* send and receive packed data */ if (tag == INT_MIN) ret = COMALL (comm, send_data, nsend, &recv_data, &recv_count); /* all to all */ else ret = COM (comm, tag, send_data, nsend, &recv_data, &recv_count); /* point to point */ #if PARDEBUG { int debug_send_count, *ip, *jp, ii [2], jj [2]; COMDATA *debug_send_data; double *qq, *pp; /* send backwards */ if (tag == INT_MIN) COMALL (comm, recv_data, recv_count, &debug_send_data, &debug_send_count); else COM (comm, tag, recv_data, recv_count, &debug_send_data, &debug_send_count); ii[0] = 0, ii[1] = 0; jj[0] = 0, jj[1] = 0; do { ip = next_int (send_data, nsend, ii); jp = next_int (debug_send_data, debug_send_count, jj); if (ip && jp) { ASSERT_DEBUG (*ip == *jp, "Integer values mismatch"); } else { ASSERT_DEBUG (!ip && !jp, "Integer count mismatch"); } } while (ip && jp); ii[0] = 0, ii[1] = 0; jj[0] = 0, jj[1] = 0; do { qq = next_double (send_data, nsend, ii); pp = next_double (debug_send_data, debug_send_count, jj); if (qq && pp) { ASSERT_DEBUG (*qq == *pp, "Double values mismatch"); } else { ASSERT_DEBUG (!qq && !pp, "Double count mismatch"); } } while (qq && pp); free (debug_send_data); } #endif if (recv_count) { *nrecv = recv_count; ERRMEM (*recv = malloc ((*nrecv) * sizeof (COMOBJ))); /* unpack received objects */ for (n = i = 0, cd = recv_data, co = *recv; i < recv_count; i ++, cd ++) { int ipos = 0, dpos = 0; do { if (n == *nrecv) { *nrecv *= 2; /* resize the receive buffer */ ERRMEM (*recv = realloc (*recv, (*nrecv) * sizeof (COMOBJ))); co = *recv + n; /* and reset the current pointer */ } co->rank = cd->rank; co->o = unpack (data, &dpos, cd->d, cd->doubles, &ipos, cd->i, cd->ints); co ++; n ++; } while (ipos < cd->ints || dpos < cd->doubles); /* while something is left to unpack */ } /* truncate output */ if (n) ERRMEM (*recv = realloc (*recv, n * sizeof (COMOBJ))); *nrecv = n; } else { *recv = NULL; *nrecv = 0; } /* cleanup */ for (MAP *item = MAP_First (map); item; item = MAP_Next (item)) { cd = item->data; free (cd->i); free (cd->d); } MEM_Release (&mem); free (send_data); free (recv_data); /* contiguous */ return ret; }
/// /// Returns a random non negative integer, in the [0, max) range. v8_uint32_t next(const v8_uint32_t max) { return static_cast<v8_uint32_t>(next_double() * static_cast<double>(max)); }