Shadowrun: Awakened 29 September 2011 - Build 871
Graph.cpp
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
00003 
00004     This file is part of Threading Building Blocks.
00005 
00006     Threading Building Blocks is free software; you can redistribute it
00007     and/or modify it under the terms of the GNU General Public License
00008     version 2 as published by the Free Software Foundation.
00009 
00010     Threading Building Blocks is distributed in the hope that it will be
00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Threading Building Blocks; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     As a special exception, you may use this file as part of a free software
00020     library without restriction.  Specifically, if other files instantiate
00021     templates or use macros or inline functions from this file, or you compile
00022     this file and link it with other files to produce an executable, this
00023     file does not by itself cause the resulting executable to be covered by
00024     the GNU General Public License.  This exception does not however
00025     invalidate any other reasons why the executable file might be covered by
00026     the GNU General Public License.
00027 */
00028 
00029 #include <cstdio>
00030 #include <cstdlib>
00031 #include "Graph.h"
00032 
00033 using namespace std;
00034 
00035 void Graph::create_random_dag( size_t number_of_nodes ) {
00036     my_vertex_set.resize(number_of_nodes);
00037     for( size_t k=0; k<number_of_nodes; ++k ) {
00038         Cell& c = my_vertex_set[k];
00039         int op = int((rand()>>8)%5u);
00040         if( op>int(k) ) op = int(k);
00041         switch( op ) {
00042             default:
00043                 c.op = OP_VALUE;
00044                 c.value = Cell::value_type((float)k);
00045                 break;
00046             case 1:
00047                 c.op = OP_NEGATE;
00048                 break;
00049             case 2:
00050                 c.op = OP_SUB;
00051                 break;
00052             case 3: 
00053                 c.op = OP_ADD;
00054                 break;
00055             case 4: 
00056                 c.op = OP_MUL;
00057                 break;
00058         }
00059         for( int j=0; j<ArityOfOp[c.op]; ++j ) {
00060             Cell& input = my_vertex_set[rand()%k];
00061             c.input[j] = &input;
00062         }
00063     }
00064 }
00065 
00066 void Graph::print() {
00067     for( size_t k=0; k<my_vertex_set.size(); ++k ) {
00068         printf("Cell %d:",int(k));
00069         for( size_t j=0; j<my_vertex_set[k].successor.size(); ++j )
00070             printf(" %d",int(my_vertex_set[k].successor[j] - &my_vertex_set[0]));
00071         printf("\n");
00072     }
00073 }
00074 
00075 void Graph::get_root_set( vector<Cell*>& root_set ) {
00076     for( size_t k=0; k<my_vertex_set.size(); ++k ) {
00077         my_vertex_set[k].successor.clear();
00078     }
00079     root_set.clear();
00080     for( size_t k=0; k<my_vertex_set.size(); ++k ) {
00081         Cell& c = my_vertex_set[k];
00082         c.ref_count = ArityOfOp[c.op];
00083         for( int j=0; j<ArityOfOp[c.op]; ++j ) {
00084             c.input[j]->successor.push_back(&c);
00085         }
00086         if( ArityOfOp[c.op]==0 )
00087             root_set.push_back(&my_vertex_set[k]);
00088     }
00089 }
00090 
00091 void Cell::update() {
00092     switch( op ) {
00093         case OP_VALUE:
00094             break;
00095         case OP_NEGATE:
00096             value = -(input[0]->value);
00097             break;
00098         case OP_ADD:
00099             value = input[0]->value + input[1]->value;
00100             break;
00101         case OP_SUB:
00102             value = input[0]->value - input[1]->value;
00103             break;
00104         case OP_MUL:
00105             value = input[0]->value * input[1]->value;
00106             break;
00107     }
00108 }
00109 

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net