2012-09-28

SCIP in C++11 ― 3.3.3節その1


表の表現

特に大きな問題はない。

----
const List makeTable(void)
{return(makeList("*table*"));}

const List associate(const List& key, const List& records)
{
    if(isNull(records)){return(makeList());}
    if(isEqual(key,caar(records))){return(car(records));}
    return(associate(key,cdr(records)));
}

void insertf(const List& key, const List& value, const List& table)
{
    const auto record(associate(key,cdr(table)));
    if(!isNull(record)){setfCdr(record,makeList(value));}
    else{setfCdr(table,cons(cons(key,value),cdr(table)));}
}

void insertf(const List& key1, const List& key2,
             const List& value, const List& table)
{
    const auto subtable(associate(key1,cdr(table)));
    if(!isNull(subtable)){
        const auto record(associate(key2,cdr(subtable)));
        if(!isNull(record)){setfCdr(record,makeList(value));}
        else{setfCdr(subtable,cons(cons(key2,value),cdr(subtable)));}
    }else{
        setfCdr(table,cons(makeList(key1,cons(key2,value)),cdr(table)));
    }
}



const List lookup(const List& key, const List& table)
{
    const auto record(associate(key,cdr(table)));
    if(!isNull(record)){return(cdr(table));}
    return(record);
}

const List lookup(const List& key1, const List& key2, const List& table)
{
    const auto subtable(associate(key1,cdr(table)));
    if(!isNull(subtable)){
        const auto record(associate(key2,cdr(subtable)));
        if(!isNull(record)){return(cdr(record));}
        return(record);
    }
    return(subtable);
}


//---------abstraction barrier---------
int main(int argc, char** argv)
{
    auto t1(makeTable());
    cout<<"t1 = (makeTable) = "<<listString(t1)<<endl;
    insertf(makeLeaf("a"),makeLeaf(1),t1);
    cout<<"(insert! 'a 1 t1): t1="<<listString(t1)<<endl;
    insertf(makeLeaf("b"),makeLeaf(2),t1);
    cout<<"(insert! 'b 2 t1): t1="<<listString(t1)<<endl;
    insertf(makeLeaf("c"),makeLeaf(3),t1);
    cout<<"(insert! 'c 3 t1): t1="<<listString(t1)<<endl;
    insertf(makeLeaf("a"),makeLeaf(4),t1);
    cout<<"(insert! 'a 4 t1): t1="<<listString(t1)<<endl;

    cout<<endl;
    auto t2(makeTable());
    cout<<"t2 = (makeTable) = "<<listString(t2)<<endl;
    insertf(makeLeaf("math"),makeLeaf("+"),makeLeaf(43),t2);
    cout<<"(insert! 'math '+ 43 t2): t2="<<listString(t2)<<endl;
    insertf(makeLeaf("math"),makeLeaf("-"),makeLeaf(45),t2);
    cout<<"(insert! 'math '- 45 t2): t2="<<listString(t2)<<endl;
    insertf(makeLeaf("math"),makeLeaf("*"),makeLeaf(42),t2);
    cout<<"(insert! 'math '* 42 t2): t2="<<listString(t2)<<endl;
    insertf(makeLeaf("letters"),makeLeaf("a"),makeLeaf(97),t2);
    cout<<"(insert! 'letters 'a 97 t2): t2="<<listString(t2)<<endl;
    insertf(makeLeaf("letters"),makeLeaf("b"),makeLeaf(98),t2);
    cout<<"(insert! 'letters 'b 98 t2): t2="<<listString(t2)<<endl;



    return(0);
}

----
出力
----
t1 = (makeTable) = ('*table*)
(insert! 'a 1 t1): t1=('*table* ('a 1))
(insert! 'b 2 t1): t1=('*table* ('b 2) ('a 1))
(insert! 'c 3 t1): t1=('*table* ('c 3) ('b 2) ('a 1))
(insert! 'a 4 t1): t1=('*table* ('c 3) ('b 2) ('a 4))

t2 = (makeTable) = ('*table*)
(insert! 'math '+ 43 t2): t2=('*table* ('math ('+ 43)))
(insert! 'math '- 45 t2): t2=('*table* ('math ('- 45) ('+ 43)))
(insert! 'math '* 42 t2): t2=('*table* ('math ('* 42) ('- 45) ('+ 43)))
(insert! 'letters 'a 97 t2): t2=('*table* ('letters ('a 97)) ('math ('* 42) ('- 45) ('+ 43)))
(insert! 'letters 'b 98 t2): t2=('*table* ('letters ('b 98) ('a 97)) ('math ('* 42) ('- 45) ('+ 43)))

0 件のコメント :

コメントを投稿