예제 #1
0
int Disp::insert(Disp *node)
{
    if (NULL == _child) {
        //first time insert
        _child = new My570List();
        if (NULL == _child) {
            cerr << "malloc failed.\n";
            return FALSE;
        }
        
        return _child->Append(node);
    } else {  //not first time, needs to strcmp to find out the right pos
        My570ListElem *item = NULL;
        Disp *dirnode = NULL;
        for (item = _child->First(); item != NULL; item = _child->Next(item))
        {
            dirnode = (Disp *)item->Obj();
            if (strcmp(node->_name, dirnode->_name) < 0) {
                return _child->InsertBefore((void *)node, item);
            }
        }

        //finish the for loop without return, means we get to the end
        //just Append
        return _child->Append(node);
    }
}
예제 #2
0
void Disp::output(uint64_t bits)
{
    printPre(bits);
    cout << _name;
    if (S_ISDIR(_type)) {
        cout << "/";
    }
    if (_fail == 1) {
        cout << "/";
    }
    cout << endl;

    if (_child == NULL) {
        return;
    }

    My570ListElem *item = NULL;
    Disp *dp = NULL;
    uint64_t subbits = bits;
    for (item = _child->First(); item != NULL; item = _child->Next(item))
    {
        dp = (Disp *)item->Obj();
        if (item == _child->Last()) {
            subbits |= 1<<dp->_level;
        }
        dp->output(subbits);
    }
}
예제 #3
0
/*
 * sort by time when insert
 */
int TransRecord::insertNode(TransNode *node)
{
    int res = 0;

    if (_list.Empty()) {
        res = _list.Append((void *)node);
        if (res == FALSE) {
            cerr << "insertNode error. \n";
            return FALSE;
        }

        return TRUE;
    }

    My570ListElem *item = NULL;
    for (item = _list.First(); item != NULL; item = _list.Next(item))
    {
        if (node->_time == ((TransNode *)(item->Obj()))->_time)
        {
            cerr << "two transaction with same time.\n";
            return FALSE;
        }

        if (node->_time < ((TransNode *)(item->Obj()))->_time)
        {
            res = _list.InsertBefore((void *)node, item);
            if (res == FALSE) {
                cerr << "error when insert node.\n";
                return FALSE;
            }
            return TRUE;
        }
    }

    //after for without return, means get to the end of list, just apprend.
    res = _list.Append((void *)node);
    if (res == FALSE) {
        cerr << "insertNode error. \n";
        return FALSE;
    }

    return TRUE;
}
예제 #4
0
/*
 * Begin code I did not write.
 * This code is derived after looking at the pseudo code in wikipedia: http://en.wikipedia.org/wiki/Dijkstra's_algorithm
 *
 */
void mazesolvebyDijkstra() {

    My570List *Dijkstralist=new My570List();	///<push everything into a queue
    for(int i=0; i<totalnumofrooms; i++) {
        Rooms[i]->parent = NULL;
        (void)Dijkstralist->Append((void*)Rooms[i]);

    }
    Rooms[startroomnum]->dist = 0;					///> distance from source to source
    while(!Dijkstralist->Empty()) {
        My570ListElem *elem = getMinDistRoom(Dijkstralist);	///complexity here is o(n);
        struct Room* mindistroom = (struct Room*)(elem->Obj());
        if(elem==NULL) {
            fprintf(stderr,"(maze has no solution) :queu returned back NULL meaning que is empty\n");
            exit(-1);
        }
        if(mindistroom->dist==INFINITY) {
            break;	///< all remaining vertices are inaccessible from source.
        }
        Dijkstralist->Unlink(elem); ///remove min dist element from queue.
        mindistroom->isvisited = true;
        if(mindistroom->index==endroomnum) {
            break;
        }
        for(int v=0; v<totalnumofrooms; v++) {
            if(isConnected(mindistroom->index,Rooms[v]->index)) {
                if(!Rooms[v]->isvisited) {
                    int alt = mindistroom->dist + AdjMatrix[mindistroom->index][v];
                    if(alt < Rooms[v]->dist) {
                        Rooms[v]->dist = alt;
                        Rooms[v]->parent = mindistroom;
                    }
                }
            }
        }
    }
    Dijkstra_shortestpath_cost = 0;
    struct Room* curroom = Rooms[endroomnum];
    while(curroom->parent!=NULL) {
        updateWallwithApple(curroom,curroom->parent); //the wall between cur room and its parent should be an apple.
        curroom->apple = '@';
        Dijkstra_shortestpath_cost +=  AdjMatrix[curroom->index][curroom->parent->index];
        curroom = curroom->parent;
    }
    Rooms[startroomnum]->apple='@';///coz curroom->index == startroomnum
}
예제 #5
0
void TransRecord::printRecord()
{
    My570ListElem *item = NULL;
    TransNode *node = NULL;
    int balance = 0;
    for (item = _list.First(); item != NULL; item = _list.Next(item))
    {
        node = (TransNode *)item->Obj();
        if (node->_type == '+') {
            balance += node->_amount;
        } else if (node->_type == '-'){
            balance -= node->_amount;
        } else {
            cerr << "error transaction type\n";
            exit(-1);
        }

        printDate(node);
        printDesc(node);
        printAmount(node);
        printBalance(balance);
    }
}
예제 #6
0
int Disp::readin(char *dirname)
{
    /*
     * in order to distinguish the topdir and following subdir,
     * use a static variable, tricky method.
     */
    static int open_cnt = 0;
    open_cnt++;
    DIR *dir = opendir(dirname);
    if (NULL == dir) {
        if (open_cnt == 1) {
            //open topdir failed, should exit program.
            cerr << "error when open topdir: " << dirname << endl;
            exit(-1);
        } else {
            //open subdir failed, no matter, treat it as empty, set the _fail
            _fail = 1;
            cerr << "error when open dir: " << dirname << endl;
            return FALSE;
        }
    }

    struct dirent * entry = NULL;
    while ((entry = readdir(dir)) != NULL)
    {
        if (strcmp(entry->d_name, ".") == 0 ||
            strcmp(entry->d_name, "..") == 0)
        {
            continue;
        }

        struct stat fs;
        mode_t type;
        string cur_name(entry->d_name);
        string path(_path);
        string new_path = path + "/" + cur_name;
        if (0 == stat(new_path.c_str(), &fs)) {
            type = fs.st_mode;
        }
        if (S_ISDIR(type) || S_ISREG(type))
        {
            int level = _level + 1;
            Disp * node = new Disp(new_path.c_str(), entry->d_name, type, level);
            if (NULL == node) {
                cerr << "malloc failed.\n";
                return FALSE;
            }

            insert(node);
        }
    }

    //close the current dir before go into the subdir
    closedir(dir);

    //after read the current dir, we go through the dirs in it
    //so, looks like recursively.
    My570ListElem *it = NULL;
    Disp *dt = NULL;
    int res = 0;
    if (_child == NULL) {
        return TRUE;
    }
    for (it = _child->First(); it != NULL; it = _child->Next(it)) 
    {
        dt = (Disp *)it->Obj();
        if (S_ISDIR(dt->_type)) {
            res = dt->readin();
            if (res == FALSE) {
                cerr << "error readin." << dt->_path << endl;
            }
        }
    }

    return TRUE;
}
예제 #7
0
void readRndFile() {
	///< OPEN THE FILE FIRST
	FILE* fptr;
	if(file==NULL) {
		//meaning no file was specified and hence has to be read from stdin
		fptr=stdin;
	} else { ///< a valid file name is specified
		ProcessFilenameFailure(file);
		fptr = fopen(file, "rb");
		if (fptr==0) {
			fprintf (stderr,"Unable to open Rndfile %s\n",file); 
			exit (-1);
		}
	}
	
///Push all the walls into a list; ---------------------------------------------------------
	int totalwalls = ((w-1)*h) + ((h-1)*w);
	int minwallstoremove = (w*h)-1;
	My570List *list=new My570List();
	for(int i=0;i<totalwalls;i++) {
		(void)list->Append((void*)Walls[i]);
	}	
	#ifdef DEBUG1
	///< to check what was pushed into the list really went in.
	My570ListElem *elem=NULL;
	for (elem=list->First(); elem != NULL; elem=list->Next(elem)) {
        struct Wall *temp =(struct Wall*)(elem->Obj());
		fprintf(stdout,"%d\n",temp->index); 	
    }
	#endif
	
///< -----------------------------------------------------------------------------------------
	int iteration=1;
	while(minwallstoremove!=0) {
		int listlen = 0;
		listlen = list->Length();
		unsigned char buf[4] ;
		if(fread (buf,1,4,fptr)!=4){	
			if(file==NULL)
				fprintf(stderr,"Random input ran out of bytes\n"); 
			else
				fprintf(stderr,"Rnd input file: %s ran out of bytes\n",file);
			
			exit(-1); 
		}
		///////TODO: mark the ones removed as "has been inspected!!"
		unsigned int rmwallnum=0;
		memcpy(&rmwallnum, buf, 4);
		int walltopushtolist = rmwallnum % (list->Length());

		/*
		My570ListElem *elem = list->Find((void*)Walls[walltopushtolist]);
		if(elem==NULL) {
			fprintf(stderr,"Elem popped from list is NULL \n");
			exit(-1);
		}
		*/
		///< Iterate to the random number'th element in the list and unlink it first.
		My570ListElem *elem = NULL;
		elem=list->First();
		struct Wall *temp =(struct Wall*)(elem->Obj());
		for (int i=0;i<walltopushtolist;i++){
			elem=list->Next(elem);
			temp =(struct Wall*)(elem->Obj());
		} 			
		///< TO CHECK> should the wall be inspected if it is to be removed again?
		list->Unlink(elem);
		
		TWOROOMS pairofroom = getAdjacentRooms(temp);
		

				
		#ifdef DEBUG1
		if(temp->isvertical)
			fprintf(stdout,"[room:%d][%d|]room:%d]",pairofroom.room1->index,temp->index,pairofroom.room2->index);
		else
			fprintf(stdout,"[room:%d][%d--]room:%d]",pairofroom.room1->index,temp->index,pairofroom.room2->index);
	
		fprintf(stdout,"\n");
		#endif

		
		if(isUnion(pairofroom.room1,pairofroom.room2)) {
		#ifdef DEBUG0
		fprintf(stdout,"iteration:%d \t Listlen:%d \t R:%d \taction: wall not removed %d\n",iteration,listlen,walltopushtolist,temp->index);
		#endif			
		} else {
			minwallstoremove--;
			temp->isremoved=true;
			#ifdef DEBUG0
			fprintf(stdout,"iteration:%d \t Listlen:%d \t R:%d \taction: remove wall %d\n",iteration,listlen,walltopushtolist,temp->index);
			#endif
			if(infoFlag){
				fprintf(stdout,"Wall %d removed.\n",temp->index);
			}
		}
		

		///This operation is done only if the wall is removable else you dont decrement it..
		
		iteration++;
	}	
}