Esempio n. 1
0
static int del_word(lua_State *s)
{
    if (!s || lua_gettop(s) < 2 || !lua_isstring(s, -1) || !lua_islightuserdata(s, -2))
    {
        return 0;
    }

    struct word_filter_t *flt = (struct word_filter_t *) lua_touserdata(s, -2);
    if (!flt)
    {
        return 0;
    }

    size_t word_len;
    const char *word = lua_tolstring(s, -1, &word_len);

    if (word)
    {
        int n;
        unsigned char code;
        struct hash_tree_node_t *curr = _get_node(flt, flt->root);
        
        for (n = 0; n < word_len; n ++)
        {
            if (!curr)
            {
                return 0;
            }
            
            code = (unsigned char) word[n];
            if (!curr->path[code])
            {
                // Word not exists
                return 0;
            }

            curr = _get_node(flt, curr->path[code]);
        }

        if (curr)
        {
            // Empty value
            curr->value = 0;
            curr->length = 0;
        }
    }

    return 0;
}
Esempio n. 2
0
void * llget(LList * list, int i)
{
     llnode * ret = _get_node(list, i);
     if(ret ==NULL)
	  return(void *) NULL;
     return ret->data;
}
Esempio n. 3
0
Node *Node::get_node(const NodePath& p_path) const {

	Node *node = _get_node(p_path);
	ERR_EXPLAIN("Node not found: "+p_path);
	ERR_FAIL_COND_V(!node,NULL);
	return node;
}
Esempio n. 4
0
void ext4fs_push_revoke_blk(char *buffer)
{
	struct revoke_blk_list *node = NULL;
	struct ext_filesystem *fs = get_fs();
	if (buffer == NULL) {
		printf("buffer ptr is NULL\n");
		return;
	}
	node = _get_node();
	if (!node) {
		printf("_get_node: malloc failed\n");
		return;
	}

	node->content = zalloc(fs->blksz);
	if (node->content == NULL)
		return;
	memcpy(node->content, buffer, fs->blksz);

	if (first_node == true) {
		revk_blk_list = node;
		prev_node = node;
		 first_node = false;
	} else {
		prev_node->next = node;
		prev_node = node;
	}
}
Esempio n. 5
0
/** \brief Translates virtual address to real address
    \param loc Virtual address to translate
    \return Pointer to real memory
 */
void* mem_translate_addr(uint32_t loc){
  struct mem_node* node = _get_node(loc);
  if(node != 0 && node->state == FULL){
    uint32_t offset = loc - node->loc;
    return node->mem + offset;
  }else{
    return 0;
  }
}
Esempio n. 6
0
/** \brief Frees a block of memory
    \param loc The starting address of the block to free
    \return 0 on success, negative on failure
 */
int mem_free(uint32_t loc){
  struct mem_node* node = _get_node(loc);
  if(node->loc != loc){
    return -1;
  }
  if(node->state == LOCKED){
    return -2;
  }
  node->state = FREE;
  return _collapse_node(node->parent);
}
	__normal_call void_type push_root (
		real_type *_pmin,
		real_type *_pmax
		)
	{
    /*---------------------------- de-alloc. existing */
		this->_nset.clear() ;
		this->_tset.clear() ;

    /*---------------------------- scale initial tria */
        real_type static constexpr _scal = 
            (real_type)+tria_pred::_dims ;

        real_type _pdel[tria_pred::_dims];
        for (auto _idim = tria_pred::_dims + 0 ; 
                  _idim-- != + 0 ; )
        {
            _pdel[_idim] = _pmax[_idim] -
                           _pmin[_idim] ;
        }

    /*---------------------------- push tria indexing */
		iptr_type _itri = _get_tria() ;
        for (auto _inod = tria_pred::_dims + 1 ; 
                  _inod-- != + 0 ; )
        {
            tria(_itri)->node(_inod) = _inod;
            
            tria(_itri)->fpos(_inod) = -1 ;
            
			tria(_itri)->next(_inod) =
                __doflip(this->null_flag());
        }

    /*------------------------- push node coordinates */
        for (auto _inod = tria_pred::_dims + 1 ; 
                  _inod-- != + 0 ; )
        {
	    iptr_type _jnod = _get_node() ;
	    for (auto _idim = tria_pred::_dims + 0 ; 
	              _idim-- != + 0 ; )
	    {
	        if (_idim != _jnod - 1 )
		        node(_jnod)->
		        pval(_idim) = _pmin [_idim];
	        else
		        node(_jnod)->
		        pval(_idim) = _pmin [_idim]+ 
		       _pdel[_idim] * _scal;
	    }
	    node(_jnod)->next() = _itri;
        }
	}
Esempio n. 8
0
bool _find_child_node(const trie_t &trie, const plane_node_t *curr_node, const char_t &key, const plane_node_t **next_node) {
  char_t *key_begin = (char_t*)((char*)(curr_node) + sizeof(plane_node_t));
  char_t *key_end = key_begin + curr_node->child_count;
  static less_t _less;
  char_t *key_ptr = std::lower_bound(key_begin, key_end, key, _less);
  if (key_end == key_ptr or *key_ptr != key) {
    return false;
  }
  size_t *offset_begin = (size_t*)((char*)(curr_node) + sizeof(plane_node_t) + round_up_8(curr_node->child_count));
  size_t new_offset = *(offset_begin + (size_t)(key_ptr - key_begin));
  *next_node = _get_node(trie, new_offset); //(const plane_node_t*)((char*)trie.addr + new_offset);
  return true;
}
Esempio n. 9
0
void * llremove(LList * list, int i)
{
     llnode * node = _get_node(list, i);
     if(node==NULL)
	  return (void *)NULL;

     if(node->prev != NULL)
     {
	  if(node->next !=NULL)
	  {
	       node->prev->next = node->next;
	       node->next->prev = node->prev;
	  }
	  else
	  {
	       node->prev->next = NULL;
	       list->tail = node->prev;
	  }
     }
     else
     {
	  if(node->next !=NULL)
	  {
	       node->next->prev = NULL;
	       list->head = node->next;
	  }
	  else
	  {
	       list->head=NULL;
	       list->tail=NULL;
	  }
     }

     list->length--;
     void * rett = node->data;
     free(node);
     return rett;
}
Esempio n. 10
0
void fuzzy_search(
    const trie_t &dict_trie,
    const trie_t &word_trie,
    const char_t *lower_limit,
    const char_t *upper_limit,
    size_t max_dist,
    output_t &output) {

  less_t _less;

  typedef _fuzzy_search_task_t task_t;
  typedef _fuzzy_search_greater_task_t greater_task_t;

  struct equal_task_t {
  bool operator()(const task_t &lhs, const task_t &rhs) {
    return 
      lhs.word_node == rhs.word_node and 
      lhs.dict_node == rhs.dict_node;// and
//      lhs.ttl <= rhs.ttl;
  }
  };

  //queue_t<task_t, (sizeof(task_t) * 1024 - sizeof(void*)) / sizeof(task_t)> q;
  std::priority_queue<task_t, typename std::vector<task_t>, greater_task_t> q;
  q.push((task_t){dict_trie.root_node, word_trie.root_node, max_dist});

//  size_t step_no = 0;
//  size_t skip_no = 0;
  while (! q.empty() ) {
    //++step_no;
    const task_t task = q.top();
    q.pop();

//    while (! q.empty() and equal_task_t()(q.top(), task)) {
//      ++skip_no;
//      q.pop();
//    }

//    if (step_no % 10000 == 0) {
//      std::cout << "step_no:" << step_no << "\tq_size:" << q.size() << "\tskip_no:" << skip_no <<"\t"<<task.word_node << "\t"<<task.dict_node  << "\t" << task.ttl<< std::endl;
//    }

    const size_t *dict_offset_begin = _get_offset_begin(task.dict_node);
    const size_t *word_offset_begin = _get_offset_begin(task.word_node);
    
    if (task.ttl) {
      //delete
      for (size_t i = 0; i < task.dict_node->child_count; ++i) {
        const plane_node_t *dict_child = _get_node(dict_trie, dict_offset_begin[i]);
        if (_empty_node(dict_child)) continue;
        task_t new_task = task;
        new_task.ttl -= 1;
        new_task.dict_node = dict_child;
        q.push(new_task);
      }

      // insert
      for (size_t i = 0; i < task.word_node->child_count; ++i) {
        const plane_node_t *word_child = _get_node(word_trie, word_offset_begin[i]);
        if (_empty_node(word_child)) continue;
        task_t new_task = task;
        new_task.ttl -= 1;
        new_task.word_node = word_child;
        q.push(new_task);
      }

      // replace
      // foreach curr_node keys
      for (size_t dict_child_id = 0; dict_child_id < task.dict_node->child_count; ++dict_child_id) {
        const plane_node_t *dict_child = _get_node(dict_trie, dict_offset_begin[dict_child_id]);
        if (_empty_node(dict_child)) continue;

        for (size_t word_child_id = 0; word_child_id < task.word_node->child_count; ++word_child_id) {
          const plane_node_t *word_child = _get_node(word_trie, word_offset_begin[word_child_id]);
          if (_empty_node(word_child)) continue;

          const char_t &dict_key = ((char_t*)((char*)task.dict_node + sizeof(plane_node_t)))[dict_child_id];
          const char_t &word_key = ((char_t*)((char*)task.word_node + sizeof(plane_node_t)))[word_child_id];
          
          if (dict_key == word_key) {
            continue; // ignore replace if it's the same as paste
          }

          task_t new_task = task;
          new_task.ttl -= 1;
          new_task.dict_node = dict_child;
          new_task.word_node = word_child;
          q.push(new_task);
        }
      }
    }
    
    // paste
    for (size_t i = 0, j = 0; i < task.dict_node->child_count && j < task.word_node->child_count; ) {
      const char_t &dict_key = ((char_t*)((char*)task.dict_node + sizeof(plane_node_t)))[i];
      const char_t &word_key = ((char_t*)((char*)task.word_node + sizeof(plane_node_t)))[j];

      if (_less(dict_key, word_key)) {
        ++i;
        continue;
      } else if (_less(word_key, dict_key)) {
        ++j;
        continue;
      } else {
        const plane_node_t *dict_child = _get_node(dict_trie, dict_offset_begin[i]);
        if (_empty_node(dict_child)) {
          ++i;
          ++j;
          continue;
        }
        
        const plane_node_t *word_child = _get_node(word_trie, word_offset_begin[j]);
        if (_empty_node(word_child)) {
          ++i;
          ++j;
          continue;
        }
        //std::cout << "EQ " << i << " " << j << " : " << word_key << " " << dict_key << std::endl; 
        task_t new_task = task;
        new_task.dict_node = dict_child;
        new_task.word_node = word_child;
        q.push(new_task);
       
        ++i;
        ++j;
      }

    }

    if (task.dict_node->value && task.word_node->value) {
      output.insert(std::make_pair(task.word_node->value, task.dict_node->value));
    }
  }
}
Esempio n. 11
0
void fuzzy_search_impl(
    const trie_t &trie,
    const char_t* word,
    const exit_condition_t &exit_condition,
    size_t max_dist, 
    output_t &output
){
  //task: word_pos, dict_pos, ttl
  queue_t<task_t, (sizeof(task_t) * 1024 - sizeof(void*)) / sizeof(task_t)> q;
  //std::queue<task_t> q;
  q.push((task_t){0, 0, max_dist, trie.root_node});

  while(!q.empty()) {
    const task_t &task = q.front();

    if (task.ttl) {
      // delete
      // foreach curr_node keys
      const size_t *offset_begin = _get_offset_begin(task.node);
      for (size_t i = 0; i < task.node->child_count; ++i) {
        q.push(task);
        q.back().ttl -= 1;
        q.back().dict_pos += 1;
        q.back().node = _get_node(trie, offset_begin[i]);
      }

      // insert
      if (task.ttl and !exit_condition(word, task.word_pos)) {
        q.push(task);
        q.back().ttl -= 1;
        q.back().word_pos += 1;
      }

      // replace
      // foreach curr_node keys
      if (!exit_condition(word, task.word_pos)) {
        for (size_t i = 0; i < task.node->child_count; ++i) {
          const char_t &key = ((char_t*)((char*)task.node + sizeof(plane_node_t)))[i];
          if (key == word[task.word_pos]) {
            continue; // ignore replace if it's the same as paste
          } 
          q.push(task);
          q.back().ttl -= 1;
          q.back().dict_pos += 1;
          q.back().word_pos += 1;
          q.back().node = _get_node(trie, offset_begin[i]);
        }
      }
      // transpose
      // check two positions
    }
    
    bool no_pop = false;
    if (!exit_condition(word, task.word_pos)) {
      // paste
      const plane_node_t *next_node;
      if (_find_child_node(trie, task.node, word[task.word_pos], &next_node)) {
        no_pop = true;
        task_t &new_task = q.front();
//        q.push(task);
//      task_t &new_task = q.back();  
        new_task.dict_pos += 1;
        new_task.word_pos += 1;
        new_task.node = next_node;
      }
    } else {
      // report found word
      if (task.node->value) {
        output(task.node->value, max_dist - task.ttl);
//        std::cout << "FOUND: " << task.node->value << std::endl;
      }
    }
    if (not no_pop)
      q.pop();
  }
}
Esempio n. 12
0
bool Node::has_node(const NodePath& p_path) const {

	return _get_node(p_path)!=NULL;
}
Esempio n. 13
0
static int do_filter(lua_State *s)
{
    if (!s || lua_gettop(s) < 2 || !lua_istable(s, -1) || !lua_islightuserdata(s, -2))
    {
        return 0;
    }

    struct word_filter_t *flt = (struct word_filter_t *) lua_touserdata(s, -2);
    if (!flt)
    {
        return 0;
    }

    struct hash_tree_node_t *root = _get_node(flt, flt->root), *curr = NULL;
    if (!root)
    {
        // No root node
        return 0;
    }

    const char *content = NULL, *replacement = NULL;
    size_t content_len = 0, n, j, seg_len = 0;
    int all = 0, matched = 0, start = 0, replace = 0;
    unsigned char code;
    BSP_STRING *res = NULL;

    lua_getfield(s, -1, "content");
    if (lua_isstring(s, -1))
    {
        content = lua_tolstring(s, -1, &content_len);
    }
    lua_pop(s, 1);

    lua_getfield(s, -1, "all");
    if (lua_isboolean(s, -1))
    {
        all = lua_toboolean(s, -1);
    }
    lua_pop(s, 1);

    lua_getfield(s, -1, "replace");
    if (lua_isboolean(s, -1))
    {
        replace = lua_toboolean(s, -1);
    }
    lua_pop(s, 1);

    lua_getfield(s, -1, "replacement");
    if (lua_isstring(s, -1))
    {
        replacement = lua_tostring(s, -1);
    }
    lua_pop(s, 1);

    lua_newtable(s);
    if (content && content_len)
    {
        if (replace)
        {
            res = new_string(NULL, 0);
            if (!replacement || !strlen(replacement))
            {
                replacement = DEFAULT_REPLACEMENT;
            }
        }
        
        for (n = 0; n < content_len; n ++)
        {
            if (!all && matched)
            {
                break;
            }
            
            curr = root;
            start = n;
            for (j = n; j <= content_len; j ++)
            {
                if (curr->value)
                {
                    // Get word
                    matched ++;
                    lua_pushinteger(s, matched);
                    lua_newtable(s);
                    lua_pushstring(s, "word");
                    lua_pushlstring(s, content + start, j - start);
                    lua_settable(s, -3);
                    lua_pushstring(s, "offset");
                    lua_pushinteger(s, start);
                    lua_settable(s, -3);
                    lua_pushstring(s, "length");
                    lua_pushinteger(s, j - start);
                    lua_settable(s, -3);
                    lua_pushstring(s, "dsp_length");
                    lua_pushinteger(s, curr->length);
                    lua_settable(s, -3);
                    lua_pushstring(s, "value");
                    lua_pushinteger(s, curr->value);
                    lua_settable(s, -3);
                    lua_settable(s, -3);

                    seg_len = j - start;
                    if (!all)
                    {
                        break;
                    }
                }

                if (j == content_len)
                {
                    break;
                }
                
                code = (unsigned char) content[j];
                if (!curr->path[code])
                {
                    // No match
                    break;
                }
                curr = _get_node(flt, curr->path[code]);
                if (!curr)
                {
                    break;
                }
            }

            if (replace)
            {
                if (seg_len > 0)
                {
                    string_printf(res, "%s", replacement);
                    seg_len --;
                }

                else
                {
                    string_printf(res, "%c", (unsigned char) content[n]);
                }
            }
        }

        if (replace)
        {
            lua_pushstring(s, "replaced");
            lua_pushlstring(s, STR_STR(res), STR_LEN(res));
            lua_settable(s, -3);
            del_string(res);
        }
    }

    return 1;
}
Esempio n. 14
0
static int add_word(lua_State *s)
{
    const char *word = NULL;
    size_t word_len = 0;
    int dsp_len = 0;
    int value = 1;
    
    if (!s || lua_gettop(s) < 2 || !lua_istable(s, -1) || !lua_islightuserdata(s, -2))
    {
        return 0;
    }

    struct word_filter_t *flt = (struct word_filter_t *) lua_touserdata(s, -2);
    if (!flt)
    {
        return 0;
    }
    lua_getfield(s, -1, "word");
    if (lua_isstring(s, -1))
    {
        word = lua_tolstring(s, -1, &word_len);
        dsp_len = word_len;
    }
    lua_pop(s, 1);

    lua_getfield(s, -1, "len");
    if (lua_isnumber(s, -1))
    {
        dsp_len = lua_tointeger(s, -1);
    }
    lua_pop(s, 1);

    lua_getfield(s, -1, "value");
    if (lua_isnumber(s, -1))
    {
        value = lua_tointeger(s, -1);
    }
    lua_pop(s, 1);
    if (word && value)
    {
        // Insert into tree
        int n;
        unsigned char code;
        struct hash_tree_node_t *curr = _get_node(flt, flt->root);
        
        for (n = 0; n < word_len; n ++)
        {
            if (!curr)
            {
                return 0;
            }
            
            code = (unsigned char) word[n];
            if (!curr->path[code])
            {
                // New node
                curr->path[code] = _get_new_node(flt);
            }

            curr = _get_node(flt, curr->path[code]);
        }

        if (curr)
        {
            // Set values
            curr->value = value;
            curr->length = dsp_len;
        }
    }

    return 0;
}
Esempio n. 15
0
	__normal_call bool_type push_node (
		real_type *_ppos,
		iptr_type &_node,
		iptr_type  _hint  = null_flag() ,
	    list_type *_tnew  = nullptr   ,
        list_type *_told  = nullptr   ,
        list_type *_circ  = nullptr
		)
	{
        this->_work.clear ();

	/*--------------------------- _find enclosing element */
		iptr_type _elem = -1;
		if (walk_mesh_node(_ppos, _elem, _hint))
		{

	/*--------------------------- push new node onto list */
		_node = _get_node() ;
        for (auto _idim = tria_pred::_dims + 0 ; 
                  _idim-- != +0 ; )
        {
        node(_node)->pval(_idim) = _ppos[_idim];
        }	

    /*--------------------------- grab enclosing indexing */
        iptr_type _tnod[ +1 + tria_pred::_dims];
        for (auto _inod = tria_pred::_dims + 1 ; 
                  _inod-- != +0 ; )
        {
        _tnod[_inod] = tria(_elem)->node(_inod);
        }

    /*--------------------------- test for node duplicate */
        real_type _dist  = 
            std::numeric_limits
                <real_type>::infinity() ;
                
        for (auto _inod = tria_pred::_dims + 1 ; 
                  _inod-- != +0 ; )
        {
        _dist = std::min(_dist,
            tria_pred::lensqr_kd(_ppos,
                &node(_tnod[_inod])->pval(+0)));
        }

        if (_dist == (real_type)0.)
        {
            _put_node(_node) ;
        /*----------------------- bail-out on duplicates! */
            return ( false ) ;
        }

    /*-------------------- retriangulate enclosing cavity */
		typename tria_pred::
		template circ_pred<
            self_type> _pred( _ppos) ;
		
		if (_circ == nullptr)
        walk_tria_list(_elem, +1   , 
                       _pred, _work) ;
        else
        _work.push_tail(_circ->head(), 
                        _circ->tend()
                       ) ;
            
        star_tria_void(_work, _node, 
                   +1, _tnew, _told) ;

    /*-------------------- delaunay topology is recovered */
        return (  true ) ;
		}
		
		return ( false ) ;
	}