Beispiel #1
0
/*
 * Add t to the union-find structure:
 * - t must be uninterpreted
 * - this creates a new singleton class with t as root
 *   and rank[t] is 0.
 */
static void partition_add(intern_tbl_t *tbl, term_t t) {
  type_t tau;

  assert(term_kind(tbl->terms, t) == UNINTERPRETED_TERM &&
         ai32_read(&tbl->map, index_of(t)) == NULL_MAP);

  tau = term_type(tbl->terms, t);
  ai32_write(&tbl->type, index_of(t), tau);
}
Beispiel #2
0
 skewed_window() noexcept
 {
     for (auto period = min_period; period < max_period; ++period) {
         cache[index_of(period)].fill(0);
         for (auto l = max_lag_at(period); l >= min_lag_at(period); --l) {
             cache[index_of(period)][l - 1] = window_value(period, l);
         }
     }
 };
Beispiel #3
0
/*
 * Same thing but mark t as frozen
 */
static void partition_add_frozen(intern_tbl_t *tbl, term_t t) {
  type_t tau;

  assert(ai32_read(&tbl->map, index_of(t)) == NULL_MAP);

  tau = term_type(tbl->terms, t);
  ai32_write(&tbl->type, index_of(t), tau);
  au8_write(&tbl->rank, index_of(t), 255);
}
Beispiel #4
0
void MOGA::unassign( individual & ind, int c, int f ) const
{
	if ( ind.chr[ index_of(c, f) ] )
	{
		ind.chr[ index_of(c, f) ] = false;
		for ( int k = 0; k < getNbObjective(); ++k )
		{
			ind.obj[k] -= data_.getAllocationObjCost(k, cust_[c], fac_[f]);
		}
		ind.q[f] += data_.getCustomer(cust_[c]).getDemand();
	}
}
Beispiel #5
0
void destroy(wrtp<edge>& e) {
    edges->erase(e);
    for (int i = 0; i < 2; i++) {
        point* p = e->get_points(i, e);
        wrtp<edge> cw_nbor(e->get_neighbors(i, cw, e));
        wrtp<edge> ccw_nbor(e->get_neighbors(i, ccw, e));
        int cw_index = index_of(cw_nbor, p);
        int ccw_index = index_of(ccw_nbor, p);
        cw_nbor->set_neighbors(cw_index, ccw, ccw_nbor, cw_nbor);
        ccw_nbor->set_neighbors(ccw_index, cw, cw_nbor, ccw_nbor);
        if (p->get_first_edge<wrtp>() == e)
            p->set_first_edge<wrtp>(ccw_nbor);
    }
    tx_delete(e);
}
Beispiel #6
0
bool MOGA::is_feasible( const individual & ind ) const
{
	// For each customer, test if there is one assignement alone or not.
	for ( unsigned int c = 0; c < cust_.size(); ++c )
	{
		// Count assignments for customer c
		int num_assigned = std::count(
			ind.chr.begin() + index_of( c, 0 ),
			ind.chr.begin() + index_of( c+1, 0 ),
			true );

		if ( num_assigned > 1 )
		{
			std::cerr << "Error: too many assignments for a customer, " << num_assigned << " counted" << std::endl;
			return false;
		}
		else if ( num_assigned < 1 )
		{
			std::cerr << "Error: no assignment for a customer, " << num_assigned << " counted" << std::endl;
			return false;
		}
	}

	// Test if demands fit to capacities
	if ( Argument::capacitated )
	{
		for ( unsigned int f = 0; f < fac_.size(); ++f )
		{
			double capacity( data_.getFacility(fac_[f]).getCapacity() ),
			       demand( 0 );

			for ( unsigned int c = 0; c < cust_.size(); ++c )
			{
				if ( ind.chr[ index_of(c, f) ] )
				{
					demand += data_.getCustomer(cust_[c]).getDemand();
				}
			}

			if ( ind.q[f] != ( capacity - demand ) )
				std::cerr << "Error: expected residual capacity: " << (capacity - demand) << ", computed: " << ind.q[f] << std::endl;

			if ( demand > capacity )
				return false;
		}
	}
	return true;
}
void PlaylistTabBar::PlaylistFavoritedSlot(int id, bool favorite) {
  const int index = index_of(id);
  FavoriteWidget* favorite_widget = qobject_cast<FavoriteWidget*>(tabButton(index, QTabBar::LeftSide));
  if (favorite_widget) {
    favorite_widget->SetFavorite(favorite);
  }
}
Beispiel #8
0
int main() {
  Hash_map hash_map;
  int i, correct= 1;
  char chars[NUM_ITEMS]= {'q', 'w', 'e', 'r', 't', 'y', 'u'};
  int offsets[NUM_ITEMS]= {0, 1, 2, 3, 4, 5, 6};

  hash_map= create(char_compare, NULL, int_compare, NULL);

  for (i= 0; i < NUM_ITEMS; i++)
    /* not bothering to check return value */
    put(&hash_map, (Key) (long) chars[i], (Data) (long) offsets[i]);

  /* now look up elements in the order they should be stored in */
  for (i= 0; i < NUM_ITEMS && correct; i++)
    if (index_of(hash_map, (Key) (long) chars[i]) != i)
      correct= 0;

  clear(&hash_map);

  if (correct)
    printf("Elements in Linked_hash_map are in correct order!\n");
  else printf("Order of elements in Linked_hash_map is incorrect. :(\n");

  return 0;
}
Beispiel #9
0
int main() {
  Hash_map hash_map;
  Data data;
  int days[NUM_ITEMS]= {23, 19, 27, 5, 10, 28, 25, 22, 26, 6, 8};
  char letters[NUM_ITEMS]= {'F', 'M', 'T', 'M', 'S', 'W', 'U', 'H', 'M', 'T',
                            'H'};
  int i, size_ans, capacity_ans, correct= 1;
  int sorted[NUM_ITEMS]= {5, 6, 8, 10, 19, 22, 23, 25, 26, 27, 28};

  hash_map= create_example_hash_map(days, letters);

  size_ans= size(hash_map);
  capacity_ans= current_capacity(hash_map);

   /* now look up elements in the order they should be stored in */
  for (i= 0; i < NUM_ITEMS && correct; i++)
    if (index_of(hash_map, (Key) (long) sorted[i]) != i)
      correct= 0;

  /* check that some of the keys are present */
  if (size_ans == 11 && capacity_ans == 20 && correct == 1 &&
      get(&hash_map, (Key) (long) days[1], &data) &&
      get(&hash_map, (Key) (long) days[4], &data) &&
      get(&hash_map, (Key) (long) days[8], &data) &&
      get(&hash_map, (Key) (long) days[9], &data))
    printf("Hash_map data and resizing seem to be correct!\n");
  else printf("Hash_map data or resizing appears to be incorrect.\n");

  clear(&hash_map);

  return 0;
}
int main(){
    int array[4]={1,3,6,2};   
    printf("Array: {1,3,6,2}\n");
    printf("\nUsing function index_of to get index of 3: %d\n",index_of(array,4,3));
    printf("Using function location_of to get index of 3: %p\n",location_of(array,4,3)); 
    printf("Using pointer dereference to get value of pointer: %d\n",*location_of(array,4,3));
}    
Beispiel #11
0
// The strategy here is, though this is n^2, it's a very small n, just
// finely-spaced adjacent cells.
bool SpatialHash::isWithinDistanceOfAnything(PointType const& physPt,
                                             double distance) const
{
   // (save taking a lot of square roots)
   double d2 = distance * distance;

   Index idx = index_of(physPt);
   Cells nbrs;
   get_neighbors(idx, nbrs);
   // 27 <= we include the center cell itself, too.
   assert(nbrs.size() <= 27);
   for (Cells::const_iterator itCells = nbrs.begin(), endCells = nbrs.end();
        itCells != endCells; ++itCells) {
      Pts const* pts = *itCells;
      if (pts) {
         for (Pts::const_iterator itPts = pts->begin(), endPts = pts->end();
              itPts != endPts; ++itPts) {
            if (itPts->SquaredEuclideanDistanceTo<double>(physPt) < d2) {
               return true;
            }
         }
      }
   }
   return false;
}
Beispiel #12
0
/* len = length of bwt */
int bwt_to_alpha_mapping(int* bwt, int* alpha, struct intarray2* dictc, 
	int len, int pbwt)
{
	assert(pbwt>=0 && pbwt<len);
	int rk_in_bwt = 1; //在BWT中,这个元素排名第几?
	int needle = bwt[pbwt];
	int j, i = index_of(bwt, pbwt, needle, 1);
	for(i; i>=0; i--)
	{if(bwt[i]==needle)rk_in_bwt=rk_in_bwt+1;}
	j = index_of(dictc->ia1, dictc->len1, needle, 0);
	assert(j!=-1);
	int ret=dictc->ia2[j]+rk_in_bwt-1;
//	printf("rk_in_bwt=%d, dictc->ia2[j]=%d\n", rk_in_bwt, dictc->ia2[j]);
//	printf("BWT[%d]=%c to Alpha[%d]=%c\n", pbwt, bwt[pbwt], ret, alpha[ret]);
	return ret;
}
Beispiel #13
0
/**
 * Decodifica el contenido del arreglo 'input' en
 * codificado en base 64. El resultado se almacena
 * en el arreglo 'output'. Devuelve true si la
 * operacion fue exitosa.
 */
bool decode_from_base64(char input[4], char output[3], int* padding) {
	int32_t temporal = 0;
	int i;
	*padding = 0;
	char* temporal_array = (char*) &temporal;

	for (i = 3; i >= 0; i--) {
		int index = index_of(input[i], ALFABETO, 64); //TODO: largo de la base
		if (index < 0) {
			if (input[i] != PADDING_CHAR)
				return false;
			*padding += 1;
			if (little_endian)
				temporal_array[3] = 0;
			else
				temporal_array[0] = 0;

		} else {
			if (little_endian)
				temporal_array[3] = index;
			else
				temporal_array[0] = index;
		}
		temporal = temporal >> 6;
	}

	if (little_endian)
		array_invert(temporal_array);

	memcpy(output, (temporal_array + 1), 3);
	return true;
}
Beispiel #14
0
int main ()
{
	std::tuple<int, double, std::string> t;
	void* p = &std::get<1>(t);

	std::cout << index_of(p, t);
}
/*
 * Add t to buffer b
 * - t must be defined in table and be a bitvector term of same bitsize as b
 * - b->ptbl must be the same as table->pprods
 */
void bvarith_buffer_add_term(bvarith_buffer_t *b, term_table_t *table, term_t t) {
  pprod_t **v;
  bvpoly_t *p;
  int32_t i;

  assert(b->ptbl == table->pprods);
  assert(pos_term(t) && good_term(table, t) && is_bitvector_term(table, t) &&
         term_bitsize(table, t) == b->bitsize);

  i = index_of(t);
  switch (table->kind[i]) {
  case POWER_PRODUCT:
    bvarith_buffer_add_pp(b, pprod_for_idx(table, i));
    break;

  case BV_CONSTANT:
    bvarith_buffer_add_const(b, bvconst_for_idx(table, i)->data);
    break;

  case BV_POLY:
    p = bvpoly_for_idx(table, i);
    v = pprods_for_bvpoly(table, p);
    bvarith_buffer_add_bvpoly(b, p, v);
    term_table_reset_pbuffer(table);
    break;

  default:
    bvarith_buffer_add_var(b, t);
    break;
  }
}
Beispiel #16
0
int main() {
  Hash_map hash_map;
  Data data= (Data) 123;
  Key key= (Key) 456;

  mcheck_pedantic(NULL);

  hash_map= create_example_hash_map();

  /* calling some of the functions with an empty Hash_map */
  if (!get(&hash_map, (Key) (long) 1, &data) &&
      !get_smallest(hash_map, &key, &data) &&  num_keys(hash_map, 0) == 0 &&
      index_of(hash_map, (Key) (long) 5) == -1 && data == (Data) 123 &&
      key == (Key) 456)
    printf("Your functions can handle an empty hash_map correctly!\n");
  else printf("Oops!  Error in handling anempty hash_map.\n");

  clear(&hash_map);

  if (mallinfo().uordblks != 0)
    printf("Memory leak of %d bytes. :(\n", mallinfo().uordblks);
  else printf("No memory leak detected. :)\n");

  return 0;
}
Beispiel #17
0
int dos2unix(const char *ifile, const char * ofile) {

    FILE * is = NULL;
    FILE * os = NULL;
    char * buf = NULL;
    chars contents = NULL;
    if ((is = fopen(ifile, "r")) == NULL) {
        fprintf(stderr, "Open file %s error", ifile);
        return -1;
    }
    buf = (char *)Malloc(MAX_LINE);
    bzero(buf, MAX_LINE);
    chars_init(&contents, MAX_LINE);

    while (!feof(is)) {
        fgets(buf, MAX_LINE, is);
        int index = 0;
        if ((index = index_of(buf, "\r\n")) != -1) {
            buf[index] = '\n';
            buf[index + 1] = '\0';
        }
        chars_append(contents, buf);
    }
    free(buf);
    fclose(is);
    
    if ((os = fopen(ofile, "w")) == NULL) {
        fprintf(stderr, "open output file %s error", ofile);
        return -1;
    }
    fputs(contents->s, os);
    chars_free(&contents);
    fclose(os);
    return 0;
}
Beispiel #18
0
/*---------------------------------------------------------------------------*/
static int
handle_config(const char *arg)
{
  char name[64];
  const char *opt;
  int arg_len;

  arg_len = index_of('=', (const uint8_t *)arg, 0, strlen(arg));

  if(arg_len > 0 && arg_len < sizeof(name)) {
    memcpy(name, arg, arg_len);
    name[arg_len] = '\0';
    opt = arg + arg_len + 1;

    if(strcmp("channel", name) == 0) {
      dectoi((const uint8_t *)opt, strlen(opt), &br_config_radio_channel);
      return 1;
    }

    if(strcmp("panid", name) == 0) {
      dectoi((const uint8_t *)opt, strlen(opt), &br_config_radio_panid);
      return 1;
    }
  }
  fprintf(stderr, "*** Unsupported configuration: %s\n", arg);
  return 0;
}
Beispiel #19
0
/** This is the cool version: This tries to find a version, and keeps looking if
 * there are no matches */
b_tree_t* b_tree_reconstruct_bulletproof(const b_tree_value_t* preorder,
                                         const b_tree_value_t* order,
                                         size_t length) {
    size_t parent_index_order;
    size_t search_from = 0;
    b_tree_t* ret;

    if (length == 0)
        return NULL;

    /** The first value in preorder is the root of the tree */
    ret = b_tree_new(preorder[0]);

    /** To allow repeated values we must while it */
    while (1) {
        /** Try to find the center of the order tree, which is also the number
         * of elements in the left of the tree */
        parent_index_order = index_of(order, length, preorder[0], search_from);

        /** Something bad happened: we use this null below if we are a "child"
          function
          or return it if theres no other possible combination */
        if (parent_index_order == NPOS) {
            b_tree_destroy(ret);
            return NULL;
        }

        /** Pick the left elements */
        ret->left = b_tree_reconstruct_bulletproof(preorder + 1, order,
                                                   parent_index_order);

        /** And the right ones */
        ret->right = b_tree_reconstruct_bulletproof(
            preorder + 1 + parent_index_order, order + 1 + parent_index_order,
            length - parent_index_order - 1);

        /** If should have values but it hasn't... try with the next match  */
        if ((parent_index_order != 0 && ret->left == NULL) ||
            (parent_index_order != length - 1 && ret->right == NULL)) {
            search_from = parent_index_order + 1;

            if (ret->right) {
                b_tree_destroy(ret->right);
                ret->right = NULL;
            }

            if (ret->left) {
                b_tree_destroy(ret->left);
                ret->left = NULL;
            }

            continue;
        }

        /** Else we go out */
        break;
    }

    return ret;
}
Beispiel #20
0
/**
 * This is the easy version of the function
 * It's not used in this program, just for reference
 */
b_tree_t* b_tree_reconstruct(const b_tree_value_t* preorder,
                             const b_tree_value_t* order, size_t length) {
    size_t parent_index_order;
    b_tree_t* ret;

    /** Find the center of the order tree, which is also the number of elements
     * in the left of the tree */
    /** Note that if length == 0 NPOS will be returned an we'll return null */
    parent_index_order = index_of(order, length, preorder[0], 0);

    /** Something bad happened: the parent value is not here */
    if (parent_index_order == NPOS)
        return NULL;

    /** The first value in preorder is the root of the tree */
    ret = b_tree_new(preorder[0]);

    /** Pick the left elements */
    ret->left = b_tree_reconstruct(preorder + 1, order, parent_index_order);

    /** And the right ones */
    ret->right = b_tree_reconstruct(preorder + 1 + parent_index_order,
                                    order + 1 + parent_index_order,
                                    length - parent_index_order - 1);

    /** If should have leaves but it doesn't... return null, it's invalid */
    if ((parent_index_order != 0 && ret->left == NULL) ||
        (parent_index_order != length - 1 && ret->right == NULL)) {
        b_tree_destroy(ret);

        return NULL;
    }

    return ret;
}
Beispiel #21
0
/*
 * Change the mapping of r:
 * - replace the current mapping by x
 * - r must be a root, already mapped, and with positive polarity
 */
void intern_tbl_remap_root(intern_tbl_t *tbl, term_t r, int32_t x) {
  assert(0 <= x && x < INT32_MAX && is_pos_term(r) &&
         intern_tbl_is_root(tbl, r) && intern_tbl_root_is_mapped(tbl, r));

  ai32_write(&tbl->map, index_of(r), (INT32_MIN|x));

  assert(intern_tbl_map_of_root(tbl, r) == x);
}
Beispiel #22
0
/*
 * Add the mapping r --> x then freeze r
 * - x must be non-negative and strictly smaller than INT32_MAX
 * - r must be a root, not mapped to anything yet, and must have positive
 *   polarity.
 */
void intern_tbl_map_root(intern_tbl_t *tbl, term_t r, int32_t x) {
  assert(0 <= x && x < INT32_MAX &&
         is_pos_term(r) && ai32_read(&tbl->map, index_of(r)) == NULL_MAP);

  // Freeze r and record its type if needed
  if (! intern_tbl_term_present(tbl, r)) {
    partition_add_frozen(tbl, r);
  } else if (au8_read(&tbl->rank, index_of(r)) < 255) {
    au8_write(&tbl->rank, index_of(r), 255);
  }

  // add the mapping
  ai32_write(&tbl->map, index_of(r), (INT32_MIN|x));

  assert(intern_tbl_map_of_root(tbl, r) == x &&
         intern_tbl_is_root(tbl, r) && !intern_tbl_root_is_free(tbl, r));
}
Beispiel #23
0
 isl::expression var(array_var_ptr var) const
 {
     int i = index_of(var);
     if (i >= 0)
         return space.var(i);
     else
         return isl::expression(nullptr);
 }
Beispiel #24
0
/*
 * Check whether r is a free root:
 * - r must be a root
 * - it's free if rank[r] < 255 (not frozen) or if r
 *   is not in the table and is uninterpreted.
 */
bool intern_tbl_root_is_free(intern_tbl_t *tbl, term_t r) {
  assert(intern_tbl_is_root(tbl, r));

  if (intern_tbl_term_present(tbl, r)) {
    return au8_read(&tbl->rank, index_of(r)) < 255;
  } else {
    return term_kind(tbl->terms, r) == UNINTERPRETED_TERM;
  }
}
Beispiel #25
0
/*
 * Hash for a set of constants a[0 ... n-1]
 * - we use index_of(a[i]) since the sign bit of a[i] is 0
 */
static uint32_t hash_const_set(term_t *a, uint32_t n) {
  uint32_t h, i;

  h = 0;
  for (i=0; i<n; i++) {
    h |= ((uint32_t) 1) << ((uint32_t) index_of(a[i]) & 0x1f);
  }
  return h;
}
Beispiel #26
0
const std::string decode_mnemonic(const string_list& words)
{
    std::ostringstream ss;
    ss << std::hex;
    auto words_end = words.end() - words.size() % 3;
    std::cout << std::endl;
    for (auto it = words.begin(); it != words_end; it += 3)
    {
        const int n = (int)common_words.size();
        int index_1 = (int)index_of(*it);
        int index_2 = (int)index_of(*(it + 1)) % n;
        int index_3 = (int)index_of(*(it + 2)) % n;
        uint32_t val = index_1 +
            n * special_modulo(index_2 - index_1, n) +
            n * n * special_modulo(index_3 - index_2, n);
        ss << val;
    }
    return ss.str();
}
static unsigned int
is_job_done (pid_t pid, unsigned int print, unsigned int details)
{
    int status;
    int idx = index_of (pid);
    job *j = get_job (idx);
    const char l = (j == list->tail) ? '+' : '-';
    pid_t p = waitpid (pid, &status, WNOHANG|WUNTRACED);
    if (p == -1)
    {
        warn ("jobs [%d] %d (%s)", j->content->job,
                                   j->content->pid,
                                   j->content->cmd);
        remove_job (idx);
        return TRUE;
    }
    if (j == NULL)
    {
        fprintf (stderr, "'%d': no such job\n", pid);
        return TRUE;
    }
    if (j->content->stopped && print)
    {
        if (details)
        {
            int i;
            fprintf (stdout, "[%d]  %c %d suspended: %s",
                             j->content->job,
                             l,
                             pid,
                             j->content->cmd);
            for (i = 0; i < j->content->argc; i++)
            {
                char q;
                switch (j->content->protected[i])
                {
                    case NONE: q = '\0'; break;
                    case DOUBLE_QUOTE: q = '"'; break;
                    case SINGLE_QUOTE: q = '\'';
                }
                fprintf (stdout, " %c%s%c", q, j->content->argv[i], q);
            }
            fprintf (stdout, "\n");
        }
        else
        {
            fprintf (stdout, "[%d]  %c %d (%s) suspended\n",
                             j->content->job,
                             l,
                             pid,
                             j->content->cmd);
        }
        /* well, it's a lie but we don't want to print it twice */
        return TRUE;
    }
Beispiel #28
0
int bwt_to_alpha_mapping_wltree(int needle, struct wavelettree* wltree, int until) {
	int rk = wavelettree_rank(wltree, until, needle);
	struct intarray2* dictc = wltree->dictc;
	int i = index_of(dictc->ia1, dictc->len1, needle, 0);
	assert(i!=-1);
#ifdef DEBUG
	printf("rk=%d, dictc->ia2[i]=%d\n", rk, dictc->ia2[i]);
#endif
	int ret = dictc->ia2[i] + rk - 1;
	return ret;
}
Beispiel #29
0
void edge::initialize_end(point* p, wrtp<edge> e, int end, int dir,
                          wrtp<edge>& self_w) {
    if (e == 0) {
        self_w->set_neighbors(end, dir, self_w, self_w);
        self_w->set_neighbors(end, 1-dir, self_w, self_w);
        p->set_first_edge<wrtp>(self_w);
    } else {
        int i = index_of(e, p);
        self_w->set_neighbors(end, 1-dir, e, self_w);
        self_w->set_neighbors(end, dir, e->get_neighbors(i, dir, e), self_w);
        e->set_neighbors(i, dir, self_w, e);

        // accessing neighbors directly in constructor code. This must no be a
        // shared edge during this call
        edge::Sp nbor_s(self_w->get_neighbors(end, dir, self_w));
        wrtp<edge> nbor_w(nbor_s);
        i = index_of(nbor_w, p);
        nbor_w->set_neighbors(i, 1-dir, self_w, nbor_w);
    }
}
Beispiel #30
0
/*
 * We visit terms breadth-first to check for cycles.
 * The index of all visited terms are stored in subst->cache.
 * The terms to process are stored in subst->queue.
 */
static void bfs_visit_index(intern_tbl_t *tbl, int32_t i) {
  if (kind_for_idx(tbl->terms, i) == UNINTERPRETED_TERM) {
    // replace i by its root
    i = index_of(intern_tbl_get_root(tbl, pos_term(i)));
  }

  if (int_hset_add(tbl->cache, i)) {
    // i has not been seen before
    int_queue_push(tbl->queue, i);
  }
}