view state_db.c @ 10:35d0358b3fe6

update Modern CbC Compiler
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Mon, 01 Jul 2019 22:18:03 +0900
parents d4bc23cb728b
children 190dadd8405b
line wrap: on
line source

#include <stdlib.h>

#include "state_db.h"
#include "memory.h"

StateDB
create_stateDB()
{
    StateDB s = (StateDB)malloc(sizeof(StateNode));
    if (!s) die_exit("Cann't alloc state db node.");
    return s;
}

static MemoryPtr mem_db;

static int state_count0;

void
reset_state_count()
{
    state_count0 = 0;
}

int
state_count()
{
    return state_count0;
}


/*

    lookup_StateDB(struct state *s, StateDB *parent, StatePtr *out)

    s->memory points the real memory
    if s is new, it is copied in the database (parent).
    if s is in the database, existing state is returned.

    if return value is 0, it returns new state.
    if out is null, no copy_state is created. (lookup mode)

    Founded state or newly created state is returned in out.
    
 */

int
lookup_StateDB(StateDB s, StateDB *parent, StateDB *out)
{
    StateDB db;
    int r;

    while(1) {
	db = *parent;
	if (!db) {
	    /* not found */
	    if (out) {
		db = create_stateDB();
		db->left = db->right = 0;
		db->memory = copy_memory(s->memory,&mem_db);
		db->hash = s->hash;
		state_count0 ++;
		*parent = db;
		*out = db;
	    }
	    return 0;
	}
	if (s->hash == db->hash) {
	    r =  cmp_memory(s->memory,db->memory);
	} else
	    r =  (s->hash > db->hash)? 1 : -1;
	if(!r) {
	    /* bingo */
	    if (out) *out = db;
	    return 1;
	} else if (r>0) {
	    parent = &db->left;
	} else if (r<0) {
	    parent = &db->right;
	}
    }
}

/* end */