/* trouve la position */ 
tree find_position(tree T,unsigned long long int x, unsigned long long int y) {

	if ( x < T->x ) {
		if ( T->left == NULL) {
			return(T);
		} else {
			return(find_position(T->left,x,y));
		}
	} else {
		if ( x == T->x && y < T->y) {
			if ( T->left == NULL) {
				return(T);
			} else {
				return(find_position(T->left,x,y));
			}		
		} else {
			if ( x == T->x && y == T->y ) {
				return(T);
			} else {
				if ( T->right == NULL) {
					return(T);
				} else {
					return(find_position(T->right,x,y));
				}
			}
		}
	}
}
示例#2
0
void manage_path()
{
	char *dir = cmdArgv[2];
	int i = 0;
	int pos;

	/* path */
	if (cmdArgc == 1) {
		printf("%s", path[0]);
		for(i = 1; i < path_num; i++)
			printf(":%s", path[i]);
		printf("\n");
	}
	/* path + */ 
	else if (strcmp(cmdArgv[1], "+") == 0 && dir != NULL) {
		if (find_position(path, path_num, dir) >= 0) {
			print_error("directory already exists");
			return;
		}
		if (path_num >= path_size) 
			enlarge_path_size();
		strcpy(path[path_num], dir);
		path_num++;
	}
	/* path - */ 
	else if (strcmp(cmdArgv[1], "-") == 0 && dir != NULL) {
		if(path_num == 0){
			print_error("the path is empty");
			return;
		}

		pos = find_position(path, path_num, dir);

		if (pos == -1) {
			print_error("no such directory in the path");
			return;
		}

		if (path_num == 1) {
			path[0] = NULL;
		} else {
			path[pos] = path[path_num - 1];
			path[path_num - 1] = NULL;
		}
		path_num--;
	} else {
		print_error("invalid command");
	}
}
示例#3
0
void Allocator::defrag()
{
    this->defragged = !this->defragged;

    for(index_t pointer_id = 0; pointer_id < this->total_blocks; ++pointer_id)
    {
        if(get_start_block(pointer_id) != -1)
        {
            index_t start_block = get_start_block(pointer_id);

            size_t required_blocks = get_size_blocks(pointer_id);

            index_t new_position = find_position(required_blocks);

            if(new_position == -1)
            {   
                throw AllocError(AllocErrorType::NoMemory);
            }

            move(start_block, new_position, required_blocks, required_blocks);

            set_start_block(pointer_id, new_position);
            set_n_blocks(pointer_id, required_blocks);
        }

    }


}
示例#4
0
int main(int argc, char **argv)
{
	int i;
	int pos;
	char *commands[] = {"cd", "exit", "path"};
	cmdArgv = malloc(MAX_CMD_ARGS * sizeof(char*));;
	init();

	while (TRUE) {
		for (i = 0; i < MAX_CMD_ARGS; i++)
			cmdArgv[i] = NULL;
		cmdArgc = 0;

		printf("$ ");
		read_and_parse_input();
		pos = find_position(commands, 3, cmdArgv[0]);
		switch (pos) {
		case 0:
			change_directory();
			break;
		case 1:
			exit(0);
			break;
		case 2:
			manage_path();
			break;
		default:
			exec_command();
			break;
		}
	}
	return 0;
}
示例#5
0
文件: mfmhd.c 项目: JensGrabner/mame
/*
    Reading bytes from the hard disk.

    Returns true if the time limit will be exceeded before reading the bit or complete byte.
    Otherwise returns the bit at the given position, or the complete data byte with the clock byte.
*/
bool mfm_harddisk_device::read(attotime &from_when, const attotime &limit, UINT16 &cdata)
{
	UINT16* track = m_cache->get_trackimage(m_current_cylinder, m_current_head);

	if (track==NULL)
	{
		// What shall we do in this case?
		throw emu_fatalerror("Cannot read CHD image");
	}

	// Get a copy for later debug output
	attotime fw = from_when;

	int bytepos = 0;
	int bitpos = 0;

	bool offlimit = find_position(from_when, limit, bytepos, bitpos);
	if (offlimit) return true;

	if (m_encoding == MFM_BITS)
	{
		// We will deliver a single bit
		cdata = ((track[bytepos] << bitpos) & 0x8000) >> 15;
		if (TRACE_BITS) logerror("%s: Reading (c=%d,h=%d,bit=%d) at cell %d [%s] = %d\n", tag(), m_current_cylinder, m_current_head, bitpos, ((bytepos<<4) + bitpos), tts(fw).c_str(), cdata);
	}
/* a partir de la racine de l'arbre */
int adding_node(tree T, tree T2 ) 
{
	tree tmp = find_position(T,T2->x,T2->y);
	if ( T2->x < tmp->x ) {
		tmp->left = T2;
		return(0);
	} else {
		if ( T2->x == tmp->x && T2->y < tmp->y) {
			tmp->left = T2;
			return(0);
		} else {
			if ( T2->x == tmp->x && T2->y == tmp->y ) {
				/* action à definir */	
				if ( T2->b == tmp->b ){
					printf("echec------------------------------------\n");
					free(T2);
					return(0);
				} else {
					printf("%lld %lld\n",tmp->x, tmp->y);
					printf("%lld %lld\n",tmp->a, tmp->b);
					printf("%lld %lld\n",T2->x, T2->y);
					printf("%lld %lld\n",T2->a, T2->b);
					printf("reussi------------------------------------\n");
					return(1);
				}
			} else {
				tmp->right = T2;
				return(0);
			}
		}
	}
}
示例#7
0
void *tc_list_pop(TCList *L, int pos)
{
    TCListItem *IT = find_position(L, pos);
    void *data = NULL;
    if (IT) {
        data = IT->data;
        if (L->head == IT) {
            if (IT->next) {
                IT->next->prev = NULL;
            }
            L->head = IT->next;
        } else if (L->tail == IT) {
            if (IT->prev) {
                IT->prev->next = NULL;
            }
            L->tail = IT->prev;
        } else {
            IT->next->prev = IT->prev;
            IT->prev->next = IT->next;
        }
        
        del_item(L, IT);
        L->nelems--;
    }
    return data;
}
int * sortedArrayInsertNumber(int *arr, int len, int num){
	int pos,index=0,i,*arr_new,count=0;
	arr_new = (int*)malloc(sizeof(int));
	if ( arr!= NULL&&len>0){
		pos = find_position(arr, len, num);
		for (i = 0; i<len + 1; i++){
			arr_new = (int *)realloc(arr_new, sizeof(int)*(index+4));
			if (i == pos&&count!=1){
				arr_new[index] = num;
				index++;
				i--;
				count++;
				
			}
			else{
				arr_new[index] = arr[i];
				index++;
			}
		}
		return arr_new;
	}
	else {
		return NULL;
	}
}
示例#9
0
文件: hash_set.hpp 项目: shenfeng/ac
 void erase(const int v) {
     auto pos = find_position(v);
     if (pos.where != ILLEGAL_BUCKET) {
         _num_deleted += 1;
         _size -= 1;
         _table[pos.where] = -1;
     }
 }
示例#10
0
void removeError(obd_code_e errorCode) {
	int pos = find_position(errorCode);
	if (pos >= 0) {
		for (int t = pos; t < error_codes_set.count; t++) 					// shift all right elements to one pos left
			error_codes_set.error_codes[t] = error_codes_set.error_codes[t + 1];
	error_codes_set.error_codes[--error_codes_set.count] = (obd_code_e)0;				// place 0
	}
}
示例#11
0
void Ball::update(float elapsed_time) {
  for (int i = 0; i < 2; i++) {
    x[i] = find_position(a[i], elapsed_time, v[i], x[i]);
    v.x[i] = find_velocity(a[i], elapsed_time, v[i]);
    a[i] = find_accel(v[i]);
  }

  checkBounds();
}
示例#12
0
    Scalar CSMatrix<Scalar>::get(unsigned int m, unsigned int n) const
    {
      // Find m-th row in the n-th column.
      int mid = find_position(Ai + Ap[n], Ap[n + 1] - Ap[n], m);

      if(mid < 0) // if the entry has not been found
        return 0.0;
      else
        return Ax[Ap[n] + mid];
    }
示例#13
0
scalar PardisoMatrix::get(int m, int n)
{
  _F_
  // Find n-th column in the m-th row.
  int mid = find_position(Ai + Ap[m], Ap[m + 1] - Ap[m], n);

  if (mid < 0) // if the entry has not been found
    return 0.0;   
  else 
    return Ax[Ap[m]+mid];
}
示例#14
0
scalar UMFPackMatrix::get(int m, int n)
{
  _F_
  // Find m-th row in the n-th column.
  int mid = find_position(Ai + Ap[n], Ap[n + 1] - Ap[n], m);

  if (mid < 0) // if the entry has not been found
    return 0.0;   
  else 
    return Ax[Ap[n]+mid];
}
示例#15
0
文件: map.hpp 项目: Nikhil14/Map
typename Map<T,H>::Node* Map<T,H>::find_position(Map::Node* n ,Key k)
{
    assert(n!=nullptr);
    if(n->pair_obj->first == k)return n;
    else if(n->pair_obj->first < k)
    {
        if(n->right ==  nullptr)
            return n;
        else
            return find_position(n->right,k);
    }
    else if(n->pair_obj->first > k)
    {
        if(n->left == nullptr)
            return n;
        else
            return find_position(n->left,k);
    }
    return nullptr;
}
示例#16
0
pair<size_t, size_t> PathIndex::round_outward(size_t start, size_t past_end) const {
    // Find the node occurrence the start position is on
    auto start_occurrence = find_position(start);
    // Seek to the start of that occurrence
    size_t start_rounded = start_occurrence->first;
    
    // Now try and round the end
    size_t past_end_rounded;
    if (past_end == 0) {
        // Range must have been empty anyway, so keep it ending before the first
        // node.
        past_end_rounded = 0;
    } else {
        // Look for the node holding the last included base
        auto end_occurrence = find_position(past_end - 1);
        // Then go out past its end.
        past_end_rounded = end_occurrence->first + node_length(end_occurrence);
    }
    
    return make_pair(start_rounded, past_end_rounded);
}
int insert_item(TOrderedList *list, Titem item) {
    int find_position(Titem *array, Titem item, int number_of_items);
    int make_room(Titem *array, int where, int size_of_array);
    int i;
    
    if (list->count < MAX ) {
        i = find_position(list->array, item,  list->count);
        make_room(list->array,i, list->count);
        list->array[i] = item;
        list->count++;
        return 1;
    } else
        return 0;
}
示例#18
0
int IndexedArray::get(int key)
{
	int page = find_page_in_index(key);
	//std::cout<<"key is found in page:"<<page<<"\n";
	int position = find_position(page* KV_in_page, ((page+1)*KV_in_page)-1,key);

	if (array[position].key != key){
		return NOT_FOUND;
	}
	else {
		return array[position].value;
	}

}
示例#19
0
dataobject::dataobject(Rcpp::NumericMatrix _data, svec _parnames) {
  Data = _data;
  parnames = _parnames;

  Rcpp::List dimnames = Data.attr("dimnames");
  Data_names = Rcpp::as<svec>(dimnames[1]);

  Idcol = find_position("ID", Data_names);
  if(Idcol < 0) Rcpp::stop("Could not find ID column in data set.");

  // Connect Names in the data set with positions in the parameter list
  match_both(Data_names, parnames, par_from, par_to);

}
示例#20
0
scalar MumpsMatrix::get(int m, int n)
{
  _F_
  // Find m-th row in the n-th column.
  int mid = find_position(Ai + Ap[n], Ap[n + 1] - Ap[n], m);
  // Return 0 if the entry has not been found.
  if (mid < 0) return 0.0;
  // Otherwise, add offset to the n-th column and return the value.
  if (mid >= 0) mid += Ap[n];
#if !defined(H2D_COMPLEX) && !defined(H3D_COMPLEX)
  return Ax[mid];
#else
  return cplx(Ax[mid].r, Ax[mid].i);
#endif
}
示例#21
0
文件: hash_set.hpp 项目: shenfeng/ac
    bool insert(const int v) {
        if (_num_buckets - _size - _num_deleted < _num_buckets / 8) {
            dense_int_set tmp(*this);
            std::swap(_table, tmp._table);
            _num_deleted = 0;
        }

        auto pos = find_position(v);
        if (pos.where != ILLEGAL_BUCKET) {
            return false; // already contains
        }
        _table[pos.insert] = v;
        _size += 1;
        return true;
    }
示例#22
0
bool Allocator::realloc_move(Pointer &p, size_t required_blocks)
{       
        index_t pointer_id = p.get_id();
        index_t new_position = find_position(required_blocks);

        if(new_position == -1)
        {
            throw AllocError(AllocErrorType::NoMemory);
        }

        move(get_start_block(pointer_id), new_position, get_size_blocks(pointer_id), required_blocks);
        set_n_blocks(pointer_id, required_blocks);
        set_start_block(pointer_id, new_position);
  
        return true;
}
示例#23
0
    void CSMatrix<std::complex<double> >::add(unsigned int m, unsigned int n, std::complex<double> v)
    {
      if(v != 0.0)   // ignore zero values.
      {
        // Find m-th row in the n-th column.
        int pos = find_position(Ai + Ap[n], Ap[n + 1] - Ap[n], m);
        // Make sure we are adding to an existing non-zero entry.
        if(pos < 0)
        {
          this->info("CSMatrix<Scalar>::add(): i = %d, j = %d.", m, n);
          throw Hermes::Exceptions::Exception("Sparse matrix entry not found: [%i, %i]", m, n);
        }

#pragma omp critical
        Ax[Ap[n] + pos] += v;
      }
    }
示例#24
0
static int item_insert_after(TCList *L, int pos, void *data)
{
    int ret = TC_ERROR;
    TCListItem *ref = find_position(L, pos);
    if (ref) {
        TCListItem *ext = new_item(L);
        if (ext) {
            ext->data = data;
            ref->next->prev = ext;
            ext->next = ref->next;
            ext->prev = ref;
            ref->next = ext;
            L->nelems++;
            ret = TC_OK;
        }
    }
    return ret;
}
示例#25
0
Pointer Allocator::alloc(size_t required_bytes)
{

    size_t required_blocks = bytes_to_blocks(required_bytes);
    index_t position = find_position(required_blocks);

    if(position == -1)
    {   
    	throw AllocError(AllocErrorType::NoMemory);
    }


    fill_map(position, required_blocks);

    index_t pointer_id = insert(position, required_blocks);

    return Pointer(this, pointer_id);

}
示例#26
0
void purge(char *str)
{
	std::string const alpha = std::string("mechant");
	std::string arg = std::string(str);
	std::string token = std::string("");
	eState state = S0;
	int i;
	while (arg.empty() == false) {	
		i = find_position(arg[0], alpha);
		if (gActionTable[state][i] == MA) {
			state = gStateTable[state][i];
			token += alpha[i];
			arg.erase(0, 1);
			if (gActionTable[state][i] == HR) {
				std::cout << "Token found: " << token << std::endl;
				token.clear();
				state = S0;
			} 
		} else {
			arg.erase(0, 1);
		}
	}
}
示例#27
0
bool validate_mnemonic(const word_list& words, const dictionary& lexicon)
{
    const auto word_count = words.size();
    if ((word_count % mnemonic_word_multiple) != 0)
        return false;

    const auto total_bits = bits_per_word * word_count;
    const auto check_bits = total_bits / (entropy_bit_divisor + 1);
    const auto entropy_bits = total_bits - check_bits;

    BITCOIN_ASSERT((entropy_bits % byte_bits) == 0);

    size_t bit = 0;
    data_chunk data((total_bits + byte_bits - 1) / byte_bits, 0);

    for (const auto& word: words)
    {
        const auto position = find_position(lexicon, word);
        if (position == -1)
            return false;

        for (size_t loop = 0; loop < bits_per_word; loop++, bit++)
        {
            if (position & (1 << (bits_per_word - loop - 1)))
            {
                const auto byte = bit / byte_bits;
                data[byte] |= bip39_shift(bit);
            }
        }
    }

    data.resize(entropy_bits / byte_bits);

    const auto mnemonic = create_mnemonic(data, lexicon);
    return std::equal(mnemonic.begin(), mnemonic.end(), words.begin());
}
示例#28
0
void addError(obd_code_e errorCode) {
	if (error_codes_set.count < MAX_ERROR_CODES_COUNT && find_position(errorCode) == -1) {
		error_codes_set.error_codes[error_codes_set.count] = errorCode;
		error_codes_set.count++;
	}
}
示例#29
0
    pair < iterator, bool > insert_noresize (value_type obj)
      {
	pair < size_type, size_type > pos = find_position ((obj));
      }
示例#30
0
    return 0.0;   
  else 
    return Ax[Ap[n]+mid];
}

void UMFPackMatrix::zero() {
  _F_
  memset(Ax, 0, sizeof(scalar) * nnz);
}

void UMFPackMatrix::add(int m, int n, scalar v) {
  _F_
  if (v != 0.0 && m >= 0 && n >= 0)   // ignore dirichlet DOFs
  {
    // Find m-th row in the n-th column.
    int pos = find_position(Ai + Ap[n], Ap[n + 1] - Ap[n], m);
    // Make sure we are adding to an existing non-zero entry.
    if (pos < 0) 
      error("Sparse matrix entry not found");
    
    Ax[Ap[n]+pos] += v;
  }
}

void UMFPackMatrix::add(int m, int n, scalar **mat, int *rows, int *cols) {
  _F_
  for (int i = 0; i < m; i++)       // rows
    for (int j = 0; j < n; j++)     // cols
      add(rows[i], cols[j], mat[i][j]);
}