예제 #1
0
  Object* LookupTable::remove(STATE, Object* key, bool* removed) {
    hashval bin;
    LookupTableBucket* entry;
    LookupTableBucket* last = NULL;

    size_t num_entries = entries_->to_native();
    size_t num_bins = bins_->to_native();

    if(min_density_p(num_entries, num_bins) && (num_bins >> 1) >= LOOKUPTABLE_MIN_SIZE) {
      redistribute(state, num_bins >>= 1);
    }

    key_to_sym(key);
    bin = find_bin(key_hash(key), num_bins);
    entry = try_as<LookupTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->key() == key) {
        Object *val = entry->value();
        if(last) {
          last->next(state, entry->next());
        } else {
          values_->put(state, bin, entry->next());
        }
        entries(state, Fixnum::from(entries_->to_native() - 1));
        if(removed) *removed = true;
        return val;
      }

      last = entry;
      entry = try_as<LookupTableBucket>(entry->next());
    }

    return Qnil;
  }
예제 #2
0
  Object* LookupTable::store(STATE, Object* key, Object* val) {
    unsigned int num_entries, num_bins, bin;
    LookupTableBucket* entry;
    LookupTableBucket* last = NULL;

    num_entries = entries_->to_native();
    num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    key_to_sym(key);
    bin = find_bin(key_hash(key), num_bins);
    entry = try_as<LookupTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->key() == key) {
        entry->value(state, val);
        return val;
      }
      last = entry;
      entry = try_as<LookupTableBucket>(entry->next());
    }

    if(last) {
      last->next(state, LookupTableBucket::create(state, key, val));
    } else {
      values_->put(state, bin, LookupTableBucket::create(state, key, val));
    }

    entries(state, Fixnum::from(num_entries + 1));
    return val;
  }
예제 #3
0
  void MethodTable::redistribute(STATE, size_t size) {
    size_t num = bins_->to_native();
    Tuple* new_values = Tuple::create(state, size);

    for(size_t i = 0; i < num; i++) {
      MethodTableBucket* entry = try_as<MethodTableBucket>(values_->at(state, i));

      while(entry) {
        MethodTableBucket* link = try_as<MethodTableBucket>(entry->next());
        entry->next(state, nil<MethodTableBucket>());

        size_t bin = find_bin(key_hash(entry->name()), size);
        MethodTableBucket* slot = try_as<MethodTableBucket>(new_values->at(state, bin));

        if(slot) {
          slot->append(state, entry);
        } else {
          new_values->put(state, bin, entry);
        }

        entry = link;
      }
    }

    values(state, new_values);
    bins(state, Fixnum::from(size));
  }
예제 #4
0
static int init_client_data (Isis_Rmf_t *rmf) /*{{{*/
{
   Client_Data_t *cl = (Client_Data_t *) rmf->client_data;
   Isis_Rmf_Grid_Type *arf = cl->arf;
   Isis_Rmf_Grid_Type *det = cl->ebounds;
   Shape_Fun_Type *shape_fun = cl->shape_fun;
   float threshold = cl->threshold;
   Elem_Type *elem;
   unsigned int i, na, nd;
   double *alo, *ahi, *dlo, *dhi;

   if (NULL == (elem = (Elem_Type *) MALLOC (arf->nbins * sizeof (*elem))))
     return -1;
   memset ((char *)elem, 0, arf->nbins * sizeof (*elem));

   cl->elem = elem;
   cl->num_arf = arf->nbins;

   na = arf->nbins;
   alo = arf->bin_lo;
   ahi = arf->bin_hi;

   nd = det->nbins;
   dlo = det->bin_lo;
   dhi = det->bin_hi;

   for (i = 0; i < na; i++)
     {
        double lam = 0.5 * (alo[i] + ahi[i]);
        unsigned int ch;
        float f;
        int ch0;

        if ((ch0 = find_bin (lam, dlo, dhi, nd)) < 0)
          continue;

        ch = ch0;
        while (ch-- > 0)
          {
             if (-1 == (*shape_fun) (cl, lam, dlo[ch], dhi[ch], &f))
               return -1;
             if (f < threshold)
               break;
             if (-1 == record_response (&elem[i], f, ch))
               return -1;
          }

        for (ch = ch0; ch < nd; ch++)
          {
             if (-1 == (*shape_fun) (cl, lam, dlo[ch], dhi[ch], &f))
               return -1;
             if (f < threshold)
               break;
             if (-1 == record_response (&elem[i], f, ch))
               return -1;
          }
     }

   return 0;
}
예제 #5
0
  Executable* MethodTable::remove(STATE, Symbol* name) {
    hashval bin;
    MethodTableBucket* entry;
    MethodTableBucket* last = NULL;

    size_t num_entries = entries_->to_native();
    size_t num_bins = bins_->to_native();

    if(min_density_p(num_entries, num_bins) && (num_bins >> 1) >= METHODTABLE_MIN_SIZE) {
      redistribute(state, num_bins >>= 1);
    }

    bin = find_bin(key_hash(name), num_bins);
    entry = try_as<MethodTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->name() == name) {
        Executable* val = entry->method();
        if(last) {
          last->next(state, entry->next());
        } else {
          values_->put(state, bin, entry->next());
        }
        entries(state, Fixnum::from(entries_->to_native() - 1));
        return val;
      }

      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    return reinterpret_cast<Executable*>(Qnil);
  }
예제 #6
0
  void LookupTable::redistribute(STATE, size_t size) {
    size_t num = bins_->to_native();
    Tuple* new_values = Tuple::create(state, size);

    for(size_t i = 0; i < num; i++) {
      Tuple* entry = try_as<Tuple>(values_->at(state, i));

      while(entry) {
        Tuple* link = try_as<Tuple>(entry->at(state, 2));
        entry->put(state, 2, Qnil);

        size_t bin = find_bin(key_hash(entry->at(state, 0)), size);
        Tuple* slot = try_as<Tuple>(new_values->at(state, bin));

        if(!slot) {
          new_values->put(state, bin, entry);
        } else {
          entry_append(state, slot, entry);
        }

        entry = link;
      }
    }

    values(state, new_values);
    bins(state, Fixnum::from(size));
  }
예제 #7
0
  Object* MethodTable::alias(STATE, Symbol* name, Symbol* vis,
                             Symbol* orig_name, Object* orig_method,
                             Module* orig_mod)
  {
    check_frozen(state);

    utilities::thread::SpinLock::LockGuard lg(lock_);

    Executable* orig_exec;

    if(Alias* alias = try_as<Alias>(orig_method)) {
      orig_exec = alias->original_exec();
      orig_mod = alias->original_module();
      orig_name = alias->original_name();
    } else if(orig_method->nil_p()) {
      orig_exec = nil<Executable>();
    } else {
      orig_exec = as<Executable>(orig_method);
    }

    Alias* method = Alias::create(state, orig_name, orig_mod, orig_exec);

    native_int num_entries = entries_->to_native();
    native_int num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    native_int bin = find_bin(key_hash(name), num_bins);

    MethodTableBucket* entry = try_as<MethodTableBucket>(values_->at(state, bin));
    MethodTableBucket* last = NULL;

    while(entry) {
      if(entry->name() == name) {
        entry->method_id(state, nil<String>());
        entry->method(state, method);
        entry->scope(state, cNil);
        entry->serial(state, Fixnum::from(0));
        entry->visibility(state, vis);
        return name;
      }

      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    if(last) {
      last->next(state, MethodTableBucket::create(
            state, name, nil<String>(), method, cNil, Fixnum::from(0), vis));
    } else {
      values_->put(state, bin, MethodTableBucket::create(
            state, name, nil<String>(), method, cNil, Fixnum::from(0), vis));
    }

    entries(state, Fixnum::from(num_entries + 1));
    return name;
  }
예제 #8
0
  Object* MethodTable::store(STATE, Symbol* name, Object* exec, Symbol* vis)
  {
    check_frozen(state);

    utilities::thread::SpinLock::LockGuard lg(lock_);

    Executable* method;
    if(exec->nil_p()) {
      method = nil<Executable>();
    } else {
      if(Alias* stored_alias = try_as<Alias>(exec)) {
        lock_.unlock();
        Object* res = alias(state, name, vis,
                           stored_alias->original_name(),
                           stored_alias->original_exec(),
                           stored_alias->original_module());
        lock_.lock();
        return res;
      } else {
        method = as<Executable>(exec);
      }
    }

    native_int num_entries = entries_->to_native();
    native_int num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    native_int bin = find_bin(key_hash(name), num_bins);

    MethodTableBucket* entry = try_as<MethodTableBucket>(values_->at(state, bin));
    MethodTableBucket* last = NULL;

    while(entry) {
      if(entry->name() == name) {
        entry->method(state, method);
        entry->visibility(state, vis);
        return name;
      }

      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    if(last) {
      last->next(state, MethodTableBucket::create(state, name, method, vis));
    } else {
      values_->put(state, bin,
                         MethodTableBucket::create(state, name, method, vis));
    }

    entries(state, Fixnum::from(num_entries + 1));

    return name;
  }
예제 #9
0
/* Infer a meaning for a specified signal */
meaning_t infer_meaning(grammar_t g, signal_t s, gsl_rng* rng) {
	/* Construct the cumulative distribution of meaning inferences */
	double Z = 0.0;
	for(meaning_t m=0; m<meanings; m++) {
		Z = _gws.m[m] = Z + g->msp[m*signals+s];	
	}
	/* Sample from this distribution */
	return (meaning_t)find_bin(_gws.m, meanings, Z*gsl_rng_uniform(rng));
}
예제 #10
0
/* Produce a signal for a specified meaning. Again use pseudo-association */
signal_t produce_signal(grammar_t g, meaning_t m, gsl_rng* rng) {
	/* Construct the cumulative distribution of signals */
	double Z = 0.0;
	for(signal_t s=0; s<signals; s++) {
		Z = _gws.s[s] = Z + g->msp[m*signals+s];	
	}
	/* Sample from this distribution */
	return (signal_t)find_bin(_gws.s, signals, Z*gsl_rng_uniform(rng));
}
예제 #11
0
void Histogram::add(double value, int rank) {  
  assert(rank>=0);
  if(value < 0){
    if(value >= -DOUBLE_EPSILON){
      cout << "warning: negative double value " << value << " changed to 0.0f" << endl;
      value = 0.0f;
    } else {
      cerr << "error: rank " << my_rank << ", add a negative value " << value << " into histogram" << endl;
      cerr << toString() << endl;
      exit(0);
    }
  }

  if (num_elems == 0) {
    double bin_size = (value != 0.0) ? (2.0 * value)/num_bins : 1.0;

    for (int i=0; i < num_bins; i++) {
      HistoBin *bin = new HistoBin(i*bin_size, (i+1) * bin_size);
      bins.push_back(bin);
    }
  }

  num_elems++;

  // maintain min/max values.
  update_min(value, rank);
  update_max(value, rank);
  update_avg(value, 1);

  // put the value in the histogram
  int binIdx = find_bin(value);
  if(binIdx != -1){
    bins[binIdx]->add(value);
  } else {
    double tail_start = bins[num_bins-1]->getEnd();
    double tail_end = 2 * value - tail_start;
    /*if(tail_end < tail_start){
			cout << "tail_end: " << tail_end << endl;
			cout << "tail_start: " << tail_start << endl;
			cout << "value: " << value << endl;
			cout << toString() << endl;
			while(1);
		}*/
    assert(tail_end >= tail_start);
    HistoBin *tail = new HistoBin(tail_start, tail_end);
    tail->add(value);
    bins.push_back(tail);
    smooth();
  }

}
예제 #12
0
  MethodTableBucket* MethodTable::find_entry(Symbol* name) {
    unsigned int bin;

    bin = find_bin(key_hash(name), bins_->to_native());
    MethodTableBucket *entry = try_as<MethodTableBucket>(values_->at(bin));

    while(entry) {
      if(entry->name() == name) {
        return entry;
      }
      entry = try_as<MethodTableBucket>(entry->next());
    }

    return 0;
  }
예제 #13
0
  LookupTableBucket* LookupTable::find_entry(STATE, Object* key) {
    unsigned int bin;

    key_to_sym(key);
    bin = find_bin(key_hash(key), bins_->to_native());
    LookupTableBucket *entry = try_as<LookupTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->key() == key) {
        return entry;
      }
      entry = try_as<LookupTableBucket>(entry->next());
    }
    return reinterpret_cast<LookupTableBucket *>(Qnil);
  }
예제 #14
0
  MethodTableBucket* MethodTable::find_entry(Symbol* name) {
    unsigned int bin;
    utilities::thread::SpinLock::LockGuard lg(lock_);

    bin = find_bin(key_hash(name), bins_->to_native());
    MethodTableBucket *entry = try_as<MethodTableBucket>(values_->at(bin));

    while(entry) {
      if(entry->name() == name) {
        return entry;
      }
      entry = try_as<MethodTableBucket>(entry->next());
    }

    return 0;
  }
예제 #15
0
  Object* MethodTable::store(STATE, Symbol* name, Object* exec, Symbol* vis) {
    unsigned int num_entries, num_bins, bin;
    MethodTableBucket* entry;
    MethodTableBucket* last = NULL;

    Executable* method;
    if(exec->nil_p()) {
      method = reinterpret_cast<Executable*>(Qnil);
    } else {
      if(Alias* alias = try_as<Alias>(exec)) {
        method = alias->original_exec();
      } else {
        method = as<Executable>(exec);
      }
    }

    num_entries = entries_->to_native();
    num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    bin = find_bin(key_hash(name), num_bins);
    entry = try_as<MethodTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->name() == name) {
        entry->method(state, method);
        entry->visibility(state, vis);
        return name;
      }
      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    if(last) {
      last->next(state, MethodTableBucket::create(state, name, method, vis));
    } else {
      values_->put(state, bin, MethodTableBucket::create(state, name, method, vis));
    }

    entries(state, Fixnum::from(num_entries + 1));
    return name;
  }
예제 #16
0
  Object* MethodTable::alias(STATE, Symbol* name, Symbol* vis,
                             Symbol* orig_name, Executable* orig_method,
                             Module* orig_mod)
  {
    unsigned int num_entries, num_bins, bin;
    MethodTableBucket* entry;
    MethodTableBucket* last = NULL;

    if(Alias* alias = try_as<Alias>(orig_method)) {
      orig_method = alias->original_exec();
      orig_mod = alias->original_module();
      orig_name = alias->original_name();
    }

    Alias* method = Alias::create(state, orig_name, orig_mod, orig_method);

    num_entries = entries_->to_native();
    num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    bin = find_bin(key_hash(name), num_bins);
    entry = try_as<MethodTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->name() == name) {
        entry->method(state, method);
        entry->visibility(state, vis);
        return name;
      }
      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    if(last) {
      last->next(state, MethodTableBucket::create(state, name, method, vis));
    } else {
      values_->put(state, bin, MethodTableBucket::create(state, name, method, vis));
    }

    entries(state, Fixnum::from(num_entries + 1));
    return name;
  }
예제 #17
0
  Object* LookupTable::remove(STATE, Object* key) {
    hashval bin;
    Object* val;
    Tuple* entry;
    Tuple* lst;

    key_to_sym(key);

    size_t num_entries = entries_->to_native();
    size_t num_bins = bins_->to_native();

    if(min_density_p(num_entries, num_bins) && (num_bins >> 1) >= LOOKUPTABLE_MIN_SIZE) {
      redistribute(state, num_bins >>= 1);
    }

    bin = find_bin(key_hash(key), num_bins);
    entry = try_as<Tuple>(values_->at(state, bin));

    lst = NULL;

    while(entry) {
      Object* link = entry->at(state, 2);

      if(entry->at(state, 0) == key) {
        val = entry->at(state, 1);
        if(lst) {
          lst->put(state, 2, link);
        } else {
          values_->put(state, bin, link);
        }
        entries(state, Fixnum::from(entries_->to_native() - 1));
        return val;
      }

      lst = entry;
      entry = try_as<Tuple>(link);
    }

    return Qnil;
  }
예제 #18
0
  Tuple* LookupTable::find_entry(STATE, Object* key) {
    unsigned int bin;
    Tuple* entry;

    key_to_sym(key);
    bin = find_bin(key_hash(key), bins_->to_native());

    /* HACK: This should be fixed by not storing NULLs */
    Object* data = values_->at(state, bin);

    if (!data) return NULL;

    entry = try_as<Tuple>(data);

    while(entry) {
      if(entry->at(state, 0) == key) {
        return entry;
      }
      entry = try_as<Tuple>(entry->at(state, 2));
    }
    return NULL;
  }
예제 #19
0
  Executable* MethodTable::remove(STATE, Symbol* name) {
    check_frozen(state);

    utilities::thread::SpinLock::LockGuard lg(lock_);

    native_int num_entries = entries_->to_native();
    native_int num_bins = bins_->to_native();

    if(min_density_p(num_entries, num_bins) &&
         (num_bins >> 1) >= METHODTABLE_MIN_SIZE) {
      redistribute(state, num_bins >>= 1);
    }

    native_int bin = find_bin(key_hash(name), num_bins);
    MethodTableBucket* entry = try_as<MethodTableBucket>(values_->at(state, bin));
    MethodTableBucket* last = NULL;

    while(entry) {
      if(entry->name() == name) {
        Executable* val = entry->method();
        if(last) {
          last->next(state, entry->next());
        } else {
          values_->put(state, bin, entry->next());
        }

        entries(state, Fixnum::from(entries_->to_native() - 1));
        return val;
      }

      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    return nil<Executable>();
  }
예제 #20
0
  Object* LookupTable::store(STATE, Object* key, Object* val) {
    unsigned int num_entries, num_bins, bin;
    Object* new_ent;
    Tuple* cur;
    Tuple* entry;

    key_to_sym(key);
    num_entries = entries_->to_native();
    num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    bin = find_bin(key_hash(key), num_bins);
    cur = entry = try_as<Tuple>(values_->at(state, bin));

    while(entry) {
      if(entry->at(state, 0) == key) {
        entry->put(state, 1, val);
        return val;
      }
      cur = entry;
      entry = try_as<Tuple>(entry->at(state, 2));
    }

    new_ent = entry_new(state, key, val);
    if(cur) {
      cur->put(state, 2, new_ent);
    } else {
      values_->put(state, bin, new_ent);
    }

    entries(state, Fixnum::from(num_entries + 1));
    return val;
  }
예제 #21
0
파일: math.c 프로젝트: hankem/ISIS
static void make_2d_histogram (int *reverse) /*{{{*/
{
   SLang_Array_Type *grid_x, *grid_y, *sl_x, *sl_y, *b;
   SLang_Array_Type *rev;
   double *x, *y, *bx, *by;
   double xmax, ymax;
   SLindex_Type *num;
   SLindex_Type dims[2];
   SLindex_Type i, n, nx, ny, nbins;
   SLindex_Type *r = NULL;

   grid_x = grid_y = sl_x = sl_y = b = rev = NULL;

   if (-1 == pop_two_darrays (&grid_x, &grid_y))
     goto push_result;

   /* need at least 1 point */
   if ((-1 == pop_two_darrays (&sl_x, &sl_y))
       || (sl_x->num_elements != sl_y->num_elements)
       || (sl_x->num_elements < 1))
     goto push_result;

   n = sl_x->num_elements;
   nx = grid_x->num_elements;
   ny = grid_y->num_elements;

   if (*reverse == 0)
     r = NULL;
   else
     {
        if (NULL == (r = (SLindex_Type *) ISIS_MALLOC (n * sizeof (SLindex_Type))))
          {
             isis_throw_exception (Isis_Error);
             goto push_result;
          }
        for (i = 0; i < n; i++)
          {
             r[i] = -1;
          }
     }

   dims[0] = nx;
   dims[1] = ny;
   nbins = dims[0] * dims[1];
   if (NULL == (b = SLang_create_array (SLANG_INT_TYPE, 0, NULL, dims, 2)))
     {
        isis_throw_exception (Isis_Error);
        goto push_result;
     }

   num = (SLindex_Type *)b->data;
   memset ((char *)num, 0, nbins * sizeof(SLindex_Type));

   bx = (double *)sl_x->data;
   by = (double *)sl_y->data;
   x = (double *)grid_x->data;
   y = (double *)grid_y->data;

   xmax = x[nx-1];
   ymax = y[ny-1];

   for (i = 0; i < n; i++)
     {
        double b_x = bx[i];
        double b_y = by[i];
        SLindex_Type ix, iy, k;

        if (b_x >= xmax)
          ix = nx-1;
        else if ((ix = find_bin (b_x, x, x+1, nx-1)) < 0)
          continue;

        if (b_y >= ymax)
          iy = ny-1;
        else if ((iy = find_bin (b_y, y, y+1, ny-1)) < 0)
          continue;

        k = iy + ny * ix;

        num[k] += 1;
        if (r != NULL) r[i] = k;
     }

   if ((r != NULL)
       && (NULL == (rev = convert_reverse_indices (r, n, nx*ny))))
     goto push_result;

   push_result:

   SLang_free_array (sl_x);
   SLang_free_array (sl_y);
   SLang_free_array (grid_x);
   SLang_free_array (grid_y);

   ISIS_FREE(r);

   SLang_push_array (b, 1);
   SLang_push_array (rev, 1);
}
예제 #22
0
파일: math.c 프로젝트: hankem/ISIS
static void make_1d_histogram (int *reverse) /*{{{*/
{
   SLang_Array_Type *v, *lo, *hi, *b, *rev;
   double *xlo, *xhi, *bv;
   unsigned int *num;
   SLindex_Type i, n, nbins;
   SLindex_Type *r = NULL;

   v = lo = hi = b = rev = NULL;

   if ((-1 == pop_two_darrays (&lo, &hi))
       || -1 == SLang_pop_array_of_type (&v, SLANG_DOUBLE_TYPE)
       || (v == NULL))
     goto push_result;

   if (lo->num_elements != hi->num_elements)
     {
        isis_vmesg (INTR, I_ERROR, __FILE__, __LINE__, "inconsistent array sizes");
        goto push_result;
     }

   n = v->num_elements;
   nbins = lo->num_elements;

   if (n < 1 || nbins < 1)
     goto push_result;

   if (*reverse == 0)
     r = NULL;
   else
     {
        if (NULL == (r = (SLindex_Type *) ISIS_MALLOC (n * sizeof(SLindex_Type))))
          {
             isis_throw_exception (Isis_Error);
             goto push_result;
          }
        for (i = 0; i < n; i++)
          r[i] = -1;
     }

   if (NULL == (b = SLang_create_array (SLANG_INT_TYPE, 0, NULL, &nbins, 1)))
     {
        isis_throw_exception (Isis_Error);
        goto push_result;
     }

   num = (unsigned int *)b->data;
   memset ((char *)num, 0, nbins * sizeof(unsigned int));

   bv = (double *)v->data;
   xlo = (double *)lo->data;
   xhi = (double *)hi->data;

   /* If the (lo,hi) grid has holes, this algorithm will
    * give the wrong answer because every item will go
    * into a bin.  But what if the grid has holes by
    * accident because it was poorly constructed?
    * Perhaps that is a strong reason to deprecate this
    * interface.
    */

   for (i = 0; i < n; i++)
     {
        double t = bv[i];
        int k = find_bin (t, xlo, xhi, (int) nbins);
        if (k >= 0)
          {
             num[k] += 1;
             if (r != NULL) r[i] = k;
          }
     }

   if ((r != NULL)
       && (NULL == (rev = convert_reverse_indices (r, n, nbins))))
     goto push_result;

   push_result:

   SLang_free_array (v);
   SLang_free_array (hi);
   SLang_free_array (lo);
   ISIS_FREE(r);

   SLang_push_array (b, 1);
   SLang_push_array (rev, 1);
}
예제 #23
0
파일: strings.c 프로젝트: poldy/brandy
/*
** 'alloc_string' is called to allocate memory for a string. The
** function returns a pointer to the memory allocated. Note that
** requests for zero bytes are allowed, and the address returned
** will point to a valid memory location ('emptystring').
*/
void *alloc_string(int32 size) {
  int32 bin, unused;
  heapblock *p, *last;
  boolean reclaimed;
  if (size==0) return &emptystring;
  basicvars.runflags.has_variables = TRUE;
  bin = find_bin(size);
  reclaimed = FALSE;
  do {
    if (binlists[bin]!=NIL) {	/* Found something usable in a bin */
      p = binlists[bin];
      binlists[bin] = p->blockflink;
      freestrings-=1;
#ifdef DEBUG
      reused[bin]+=1;
      allocations[bin]+=1;
      if (basicvars.debug_flags.strings) fprintf(stderr, "Allocate string at %p, length %d bytes\n", p, binsizes[size]);
#endif
      return p;
    }

/* There was nothing in the bin. Try grabbing more memory from the heap */

    size = binsizes[bin];  	/* Get string size for bin 'bin' */
    p = condalloc(size);
    if (p!=NIL) {		/* Allocated block from heap successfully */
#ifdef DEBUG
      allocated+=size;
      created[bin]+=1;
      allocations[bin]+=1;
      if (basicvars.debug_flags.strings) fprintf(stderr, "Allocate string at %p, length %d bytes\n", p, size);
#endif
      return p;
    }

/* The heap is exhausted. Try the free block list */

    p = freelist;
    last = NIL;
    while (p!=NIL && p->blocksize<size) {	/* Look for first block big enough */
      last = p;
      p = p->blockflink;
    }
    if (p!=NIL) {	/* Found some memory that can be used */
      unused = p->blocksize-size;	/* Find out how much of block will be left */
      if (unused<=SHORTLIMIT) {		/* Remove entire block from the free list */
        if (last==NIL)	/* Block was first in list */
          freelist = p->blockflink;
        else {
          last->blockflink = p->blockflink;
        }
        freestrings-=1;
        if (unused>0) {	/* If anything is left from block, put it in a bin */
          basicstring descriptor;
          descriptor.stringaddr = CAST(p, char *)+size;
          descriptor.stringlen = unused;
          free_string(descriptor);
        }
      }
      else {	/* Use part of block. Return unused portion to free list */
        heapblock *up;
        up = CAST(CAST(p, char *)+size, heapblock *);
        up->blockflink = p->blockflink;
        up->blocksize = unused;
        if (last==NIL)
          freelist = up;
        else {
          last->blockflink = up;
        }
      }
#ifdef DEBUG
      allocations[bin]+=1;
      if (basicvars.debug_flags.strings) fprintf(stderr, "Allocate string at %p, length %d bytes\n", p, size);
#endif
      return p;
    }
int main(int argc, char** argv) {
	// OK, first let's make sure our command line arguments are sane

	if (argc != 5) {
		printf("Usage: %s LUGGAGE_ID FLIGHT_ID DEPARTING ARRIVING\n", argv[0]);
		printf("\tEach argument is a search string for that piece of information\n");
		printf("\tSubstituting a hyphen for a search string acts as a wildcard\n");

		exit(EXIT_FAILURE);
	}

	// OK, our arguments are sane, let's move on and load in all the luggage data

	char buffer[1024];

	memset(&buffer, '\0', 1024);

	bag_carousel *carousel = NULL;

	fflush(stdout);

	while (fgets(buffer, 1024 - 1, stdin) != NULL) {
		if (strlen(buffer) < 2)			// So it will catch new lines
			break;	// Done with stuff

		// Get the bag

		bag_record *bag = read_bag_record(buffer);

		// Store it in the carousel (creating if necessary)

		if (carousel == NULL) {
			// Create the carousel

			carousel = create_carousel(create_bin(bag));
		} else {
			// Make sure we're not replacing something

			bag_bin *bin = find_bin(carousel, bag->luggage_id);

			if (bin == NULL) {
				// Need to add it

				add_bin(carousel, create_bin(bag));
			} else {
				// Need to update it

				update_bin(bin, bag);
			}
		}

		// Reset the buffer, to be safe

		memset(buffer, '\0', 1024);
	}

	// OK, we're done, we'll need to dump bag history, based on our filter

	if (carousel != NULL) {
		print_carousel(carousel, buffer, 1024, argv[1], argv[2], argv[3], argv[4]);

		// Free all the memory we've taken

		cleanup_carousel(carousel);
	}

	exit(EXIT_SUCCESS);
}
예제 #25
0
static int add_spread_lines (double *val, double *wllo, double *wlhi, int nbins, /*{{{*/
                             EM_line_emis_t *t, Model_t *m, Model_Info_Type *info)
{
   Isis_Line_Profile_Type *map_profile;
   double thermal_profile_params[2];
   double *profile_params = NULL;
   int num_profile_params = 0;
   void *profile_options = NULL;
   int k, nlines;
   float atwt;
   Isis_Hist_t g;

   if (NULL == wllo || NULL == wlhi || NULL == val || NULL == t
       || nbins < 1)
     return -1;

   memset ((char *)&g, 0, sizeof g);
   g.bin_lo = wllo;
   g.bin_hi = wlhi;
   g.val = val;
   g.nbins = nbins;

   /* model-specific line profile over-rides the "global" setting */
   if (info->profile)
     {
        SLang_Array_Type *pa = info->profile_params;
        if (pa != NULL)
          {
             profile_params = (double *) pa->data;
             num_profile_params = pa->num_elements;
          }
        map_profile = info->profile;
        profile_options = info->profile_options;
     }
   else
     {
        map_profile = Model_Profile;
        if (map_profile != NULL)
          {
             thermal_profile_params[0] = (double) m->temperature;
             thermal_profile_params[1] = (double) m->vturb;
             profile_params = thermal_profile_params;
             num_profile_params = 2;
          }
        else
          {
             profile_params = NULL;
             num_profile_params = 0;
          }
        profile_options = NULL;
     }

   nlines = EM_get_nlines (t);
   if (-1 == nlines)
     return -1;

   for (k = 0; k < nlines; k++)
     {
        DB_line_t *line;
        double flux, emis;
        float emis_f, wl, flux_f;
        int mid, Z, q;

        if (-1 == _EM_get_line_emis_wl (&line, &emis_f, &wl, k, t))
          return -1;
        emis = (double) emis_f;

        if (wl < wllo[0] || wlhi[nbins-1] < wl)
          continue;

        if (info->line_emis_modifier != NULL)
          {
             if (-1 == apply_line_modifier (m, info, line, &emis))
               return -1;
          }

        if (emis <= 0.0)
          continue;

        mid = find_bin ((double) wl, wllo, wlhi, nbins);
        if (mid < 0)
          continue;

        flux = m->norm * emis;

        if (-1 == DB_get_line_ion (&Z, &q, line))
          return -1;

        flux *= m->rel_abund[Z];

        /* Side-effect: increment line fluxes stored in wavelength tables: */
        line->flux += flux;

        /* each model component remembers its contribution to the line flux */
        flux_f = (float) flux;
        SLang_set_array_element (m->line_flux, &line->indx, &flux_f);

        /* profile = NULL imples a delta function. */
        if (NULL == map_profile)
          {
             val[mid] += flux;
             continue;
          }
        else
          {
             if (-1 == DB_get_atomic_weight_amu (&atwt, line))
               return -1;

             if (-1 == (*map_profile)(&g, flux, (double) wl, (double) atwt, mid,
                                      profile_params, num_profile_params, profile_options))
               return -1;
          }
     }

   return 0;
}