Example #1
0
File: hehe.C Project: cook-/Ve281
bool Tree<Key,Val>::insert(Key* key, Val* val)
{
	//Node<Key,Val>* node=root;//can directly use root
	if (isEmpty()) {
		root=new Node<Key,Val>;//should use "root" not node
		root->lkey=key;//if not pointer,should use &key
		root->lval=val;
		root->left=root->right=root->center=NULL;
		root->rkey=NULL;
		root->rval=NULL;
		return true;
	}
	Val* a=search(*key);//check if (*key) exist in the tree
	if (a!=NULL) return false;
	Node<Key,Val>* node1=NULL;
	Node<Key,Val>* node2=NULL;
	bool t=insert_help(root,&node1,&node2,key,val);
	if (t){//new node at root
		root=new Node<Key,Val>;
		root->lkey=key;//if not pointer,should use &key
		root->lval=val;
		root->rkey=NULL;
		root->rval=NULL;
		root->left=node1;
		root->center=node2;
		root->right=NULL;
	}
	return true;
}
Example #2
0
void insert_sort(int* list)
{
    int a=1;
    while(a)
    {
        a=insert_help(list);
    }
}
Example #3
0
void insert_sort(int* list, int* max)
{
    int a=1;
    while(a)
    {
        a=insert_help(list, max);
    }
}
Example #4
0
// Read one statement
Expr* IpeReader::readStmt()
{
  int   lexerStatus  = 100;
  int   parserStatus = YYPUSH_MORE;
  Expr* retval       = 0;

  mContext.latestComment = NULL;

  while (lexerStatus != 0 && parserStatus == YYPUSH_MORE && retval == 0)
  {
    YYSTYPE yylval;

    mContext.generatedStmt = NULL;

    lexerStatus            = yylex(&yylval, &mYYlloc, mContext.scanner);

    if (lexerStatus >= 0)
      parserStatus = yypush_parse(mParser, lexerStatus, &yylval, &mYYlloc, &mContext);

    else if (lexerStatus == YYLEX_BLOCK_COMMENT)
      mContext.latestComment = yylval.pch;

    if (mContext.generatedStmt != 0)
    {
      if (BlockStmt* bs = toBlockStmt(mContext.generatedStmt))
      {
        if (Expr* expr = bs->body.last())
          retval = expr;
      }
    }
  }

  if (retval != 0)
    insert_help(retval, NULL, NULL);

  return retval;
}
Example #5
0
void Expr::replace(Expr* new_ast) {
  if (new_ast->parentSymbol || new_ast->parentExpr)
    INT_FATAL(new_ast, "Argument is already in AST in Expr::replace");
  if (new_ast->list)
    INT_FATAL(new_ast, "Argument is in a list in Expr::replace");
  if (list) {
    new_ast->next = next;
    new_ast->prev = prev;
    new_ast->list = list;
    if (next)
      next->prev = new_ast;
    else
      list->tail = new_ast;
    if (prev)
      prev->next = new_ast;
    else
      list->head = new_ast;
    next = NULL;
    prev = NULL;
    list = NULL;
  } else {
    callReplaceChild(this, new_ast);
  }

  Symbol* myParentSymbol = parentSymbol;
  Expr* myParentExpr = parentExpr;
  remove_help(this, 'p');
  insert_help(new_ast, myParentExpr, myParentSymbol);

  // Update the _this field in a FnSymbol if necessary.
  if (DefExpr* def = toDefExpr(this))
    if (ArgSymbol* arg = toArgSymbol(def->sym))
      if (FnSymbol* fn = toFnSymbol(myParentSymbol))
        if (fn->_this == arg)
          fn->_this = toDefExpr(new_ast)->sym;
}
Example #6
0
File: hehe.C Project: cook-/Ve281
static bool insert_help(Node<Key,Val>* node, Node<Key,Val>** node1_tem, Node<Key,Val>** node2_tem, Key* &key,Val* &val)
{
	Node<Key,Val>* node1=NULL;//create node1 at this level
	Node<Key,Val>* node2=NULL;//create node2 at this level
	Node<Key,Val>* newnode=NULL;//create a new node at this level
	Node<Key,Val>* nodeposition[4];
	Key* tem_key[3];
	Val* tem_val[3];

	if (node->left!=NULL) //interior node
	{
		if (*(key)<*(node->lkey)) {//left-insert
			bool a=insert_help(node->left,&node1,&node2,key,val);
			if (a==false) return false;
			if (node->rkey!=NULL){//3-node under left-insert
				tem_key[0]=key;
				tem_key[1]=node->lkey;	
				tem_key[2]=node->rkey;
				tem_val[0]=val;
				tem_val[1]=node->lval;
				tem_val[2]=node->rval;
				nodeposition[0]=node1;
				nodeposition[1]=node2;
				nodeposition[2]=node->center;
				nodeposition[3]=node->right;
			}else {//2-node under left-insert
				node->rkey=node->lkey;
				node->rval=node->lval;
				node->lkey=key;
				node->lval=val;
				node->right=node->center;
				node->center=node2;
				node->left=node1;
				return false;
			}
		}else {//center-insert or right-insert
			if (node->rkey==NULL){ //center-insert or right-insert based on 2-node
				if (*(key)>*(node->lkey)) {//center-insert based on 2-node
					bool a=insert_help(node->center,&node1,&node2,key,val);
					if (a==false) return false;
					node->rkey=key;
					node->rval=val;
					node->center=node1;
					node->right=node2;
					return false;
					
				}
			}else {	 //center-insert or right-insert based on 3-node
				if (*(key)<*(node->rkey)) { //center-insert based on 3-node
					bool a=insert_help(node->center,&node1,&node2,key,val);
					if (a==false) return false;
					tem_key[0]=node->lkey;
					tem_key[1]=key;
					tem_key[2]=node->rkey;
					tem_val[0]=node->lval;
					tem_val[1]=val;
					tem_val[2]=node->rval;
					nodeposition[0]=node->left;
					nodeposition[1]=node1;
					nodeposition[2]=node2;
					nodeposition[3]=node->right;
				}
				if (*(key)>*(node->rkey)) {//right-insert based on 3-node
					bool a=insert_help(node->right,&node1,&node2,key,val);
					if (a==false) return false;
					tem_key[0]=node->lkey;
					tem_key[1]=node->rkey;
					tem_key[2]=key;
					tem_val[0]=node->lval;
					tem_val[1]=node->rval;
					tem_val[2]=val;
					nodeposition[0]=node->left;
					nodeposition[1]=node->center;
					nodeposition[2]=node1;
					nodeposition[3]=node2;
				}
			}
		}
	}
	else//leaf node
	{
		if (node->rkey!=NULL){	//3-node leaf
			if (*(key)<*(node->lkey)) {//left-insert
				tem_key[0]=key;
				tem_key[1]=node->lkey;	
				tem_key[2]=node->rkey;
				tem_val[0]=val;
				tem_val[1]=node->lval;
				tem_val[2]=node->rval;
				nodeposition[0]=NULL;
				nodeposition[1]=NULL;
				nodeposition[2]=NULL;
				nodeposition[3]=NULL;
			}else if (*(key)>*(node->rkey)) {//right-insert
				tem_key[0]=node->lkey;
				tem_key[1]=node->rkey;
				tem_key[2]=key;
				tem_val[0]=node->lval;
				tem_val[1]=node->rval;
				tem_val[2]=val;
				nodeposition[0]=NULL;
				nodeposition[1]=NULL;
				nodeposition[2]=NULL;
				nodeposition[3]=NULL;
			}else {//center-insert
				tem_key[0]=node->lkey;
				tem_key[1]=key;
				tem_key[2]=node->rkey;
				tem_val[0]=node->lval;
				tem_val[1]=val;
				tem_val[2]=node->rval;
				nodeposition[0]=NULL;
				nodeposition[1]=NULL;
				nodeposition[2]=NULL;
				nodeposition[3]=NULL;
			}
		}else { //2-node leaf
			if (*(key)<*(node->lkey)) {//left-insert
				node->rkey=node->lkey;
				node->rval=node->lval;
				node->lkey=key;
				node->lval=val;
			}else {//right-insert
				node->rkey=key;
				node->rval=val;
			}
			return false;
		}
	}

	//reset key and val
	key=tem_key[1];
	val=tem_val[1];

	//reset the node and newnode, and newnode is always at the right of node at same level
	node->lkey=tem_key[0];
	node->rkey=NULL;
	node->lval=tem_val[0];
	node->rval=NULL;
	node->left=nodeposition[0];
	node->center=nodeposition[1];
	node->right=NULL;
	newnode=new Node<Key,Val>;
	newnode->lkey=tem_key[2];
	newnode->rkey=NULL;
	newnode->lval=tem_val[2];
	newnode->rval=NULL;
	newnode->left=nodeposition[2];
	newnode->center=nodeposition[3];
	newnode->right=NULL;

	//reset the node1_tem and node2_tem
	*node1_tem=node;
	*node2_tem=newnode;
	return true;
}