Math-SimpleHisto-XS

 view release on metacpan or  search on metacpan

XS.xs  view on Meta::CPAN

overflow(self)
    simple_histo_1d* self
  CODE:
    RETVAL = self->overflow;
  OUTPUT: RETVAL

double
underflow(self)
    simple_histo_1d* self
  CODE:
    RETVAL = self->underflow;
  OUTPUT: RETVAL

double
total(self)
    simple_histo_1d* self
  CODE:
    RETVAL = self->total;
  OUTPUT: RETVAL

unsigned int
nbins(self)
    simple_histo_1d* self
  CODE:
    RETVAL = self->nbins;
  OUTPUT: RETVAL

unsigned int
highest_bin(self)
    simple_histo_1d* self
  CODE:
    /* I know. Trivial, but convienient! */
    RETVAL = self->nbins-1;
  OUTPUT: RETVAL

double
binsize(self, ibin = 0)
    simple_histo_1d* self
    unsigned int ibin
  CODE:
    HS_ASSERT_BIN_RANGE(self, ibin);
    if (self->bins == NULL)
      RETVAL = self->binsize;
    else
      RETVAL = self->bins[ibin+1] - self->bins[ibin];
  OUTPUT: RETVAL

unsigned int
nfills(self)
    simple_histo_1d* self
  CODE:
    RETVAL = self->nfills;
  OUTPUT: RETVAL


void
all_bin_contents(self)
    simple_histo_1d* self
  PREINIT:
    SV* rv;
  PPCODE:
    rv = histo_data_av(aTHX_ self);
    XPUSHs(sv_2mortal(rv));

void
set_all_bin_contents(self, new_data)
    simple_histo_1d* self
    AV* new_data
  PREINIT:
    unsigned int n, i;
    double* data;
    SV** elem;
  CODE:
    /* While this would be nicer in the histogram API, it will be much faster
     * to access the AV* on the fly instead of doing blanket conversion to remove
     * dependence on perl data structures, so this stays here for the time being. */
    HS_INVALIDATE_CUMULATIVE(self);
    n = self->nbins;
    if ((unsigned int)(av_len(new_data)+1) != n) {
      croak("Length of new data is %u, size of histogram is %u. That doesn't work.", (unsigned int)(av_len(new_data)+1), n);
    }
    data = self->data;
    for (i = 0; i < n; ++i) {
      elem = av_fetch(new_data, i, 0);
      if (elem == NULL) {
        croak("Shouldn't happen");
      }
      self->total -= data[i];
      data[i] = SvNV(*elem);
      self->total += data[i];
    }

double
bin_content(self, ibin)
    simple_histo_1d* self
    unsigned int ibin
  CODE:
    HS_ASSERT_BIN_RANGE(self, ibin);
    RETVAL = self->data[ibin];
  OUTPUT: RETVAL

void
set_bin_content(self, ibin, content)
    simple_histo_1d* self
    unsigned int ibin
    double content
  PPCODE:
    /* Would be nicer in the API, but again, this is faster. */
    HS_ASSERT_BIN_RANGE(self, ibin);
    HS_INVALIDATE_CUMULATIVE(self);
    self->total += content - self->data[ibin];
    self->data[ibin] = content;

void
set_underflow(self, content)
    simple_histo_1d* self
    double content
  PPCODE:
    /* This doesn't invalidate the INTERNAL cumulative histo */
    self->underflow = content;

void
set_overflow(self, content)
    simple_histo_1d* self
    double content
  PPCODE:
    /* This doesn't invalidate the INTERNAL cumulative histo */
    self->overflow = content;


void
set_nfills(self, nfills)
    simple_histo_1d* self
    unsigned int nfills
  PPCODE:
    /* This doesn't invalidate the INTERNAL cumulative histo */
    self->nfills = nfills;


#void
#binary_dump(self)
#    simple_histo_1d* self
#  PREINIT:
#    char* out;
#    SV* outSv;
#    double* tmp;
#    unsigned int size;
#  PPCODE:
#    size = sizeof(simple_histo_1d) + sizeof(double)*self->nbins;
#    outSv = newSVpvs("");
#    SvGROW(outSv, size+1);
#    printf("   %u\n", SvLEN(outSv));
#    out = SvPVX(outSv);
#    SvLEN_set(outSv, size);
#    printf("%u\n", SvLEN(outSv));
#    /*Newx(out, size+1, char);*/
#    tmp = self->data;
#    self->data = NULL;
#    Copy(self, out, sizeof(simple_histo_1d), char);
#    Copy(tmp, out+sizeof(simple_histo_1d), sizeof(double)*self->nbins, char);
#    out[size] = '\0';
#    printf("%u\n", SvLEN(outSv));
#    self->data = tmp;
#    XPUSHs(sv_2mortal(outSv));


void
rand(self, ...)
    simple_histo_1d* self
  PREINIT:
    double rndval;
    double retval;
    SV* rngsv;
    Math__SimpleHisto__XS__RNG rng;
    unsigned int ibin;
  PREINIT:
    simple_histo_1d* cum_hist;
  PPCODE:
    if (items > 1) {
      rngsv = ST(1);
    }
    else {
      rngsv = get_sv("Math::SimpleHisto::XS::RNG::Gen", 0);
      if (rngsv == 0) {
        croak("Cannot find default random number generator!");
      }
    }
    if (sv_derived_from(rngsv, "Math::SimpleHisto::XS::RNG")) {
      IV tmp = SvIV((SV*)SvRV(rngsv));
      rng = INT2PTR(Math__SimpleHisto__XS__RNG, tmp);
    }
    else
      Perl_croak(aTHX_ "%s: %s is not of type %s",
                  "Math::SimpleHisto::XS::rand",
                  "rng", "Math::SimpleHisto::XS::RNG");
    rndval = mt_genrand(rng);

    /* Get the properly normalized internal cumulative */
    HS_ASSERT_CUMULATIVE(self);
    cum_hist = self->cumulative_hist;

    /* This all operates on the cumulative histogram */
    ibin = rndval < cum_hist->data[0] ? 0 : find_bin_nonconstant(rndval, cum_hist->nbins, cum_hist->data);
    if (cum_hist->bins == 0) { /* constant bin size */
      retval = cum_hist->min + cum_hist->binsize * (double)(ibin+1);
      if (rndval > cum_hist->data[ibin]) {
        retval += cum_hist->binsize * (rndval - cum_hist->data[ibin])
                                      / (cum_hist->data[ibin+1] - cum_hist->data[ibin]);
      }
    }
    else { /* variable bin size */
      retval = cum_hist->bins[ibin+1];
      if (rndval > cum_hist->data[ibin]) {
        retval += (cum_hist->bins[ibin+1] - cum_hist->bins[ibin])
                  * (rndval - cum_hist->data[ibin])
                  / (cum_hist->data[ibin+1] - cum_hist->data[ibin]);
      }
    }
    XPUSHs(sv_2mortal(newSVnv(retval)));


void
_get_info(self)
    simple_histo_1d* self
  PREINIT:
    SV* data_ary;
    SV* bins_ary;
  PPCODE:
    /* min, max, nbins, nfills, overflow, underflow, dataref, binsref*/
    EXTEND(SP, 8);
    mPUSHn(self->min);
    mPUSHn(self->max);
    mPUSHu(self->nbins);
    mPUSHu(self->nfills);
    mPUSHn(self->overflow);
    mPUSHn(self->underflow);
    data_ary = histo_data_av(aTHX_ self);
    XPUSHs(sv_2mortal(data_ary));
    if (self->bins == NULL)
      bins_ary = &PL_sv_undef;
    else
      bins_ary = sv_2mortal(histo_bins_av(aTHX_ self));
    XPUSHs(bins_ary);



( run in 1.152 second using v1.01-cache-2.11-cpan-71847e10f99 )