node.cpp

Go to the documentation of this file.
00001 #ifndef NODE_IMPLEMENTATION_FILE
00002 #define NODE_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : node                                                              *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "node.h"
00019 
00020 #include <basis/function.h>
00021 #include <basis/guards.h>
00022 #include <data_struct/amorph.cpp>
00023 
00024 namespace nodes {
00025 
00026 // the internal_link class anonymously hangs onto a pointer to the object.
00027 struct internal_link {
00028   node *_connection;
00029   internal_link(node *destination = NIL) : _connection(destination) {}
00030   virtual ~internal_link() { _connection = NIL; }
00031 };
00032 
00033 class node_link_amorph : public amorph<internal_link>
00034 {
00035 public:
00036   node_link_amorph(int num) : amorph<internal_link>(num) {}
00037 };
00038 
00040 
00041 node::node(int number_of_links)
00042 : _links(new node_link_amorph(number_of_links))
00043 { for (int i = 0; i < number_of_links; i++) set_empty(i); }
00044 
00045 node::~node()
00046 {
00047   _links->reset();
00048   WHACK(_links);
00049 }
00050 
00051 int node::links() const { return _links->elements(); }
00052 
00053 // set_empty: assumes used correctly by internal functions--no bounds return.
00054 void node::set_empty(int link_num)
00055 {
00056   internal_link *blank_frank = new internal_link(NIL);
00057   _links->put(link_num, blank_frank);
00058 }
00059 
00060 #define test_arg(link_num) bounds_return(link_num, 0, _links->elements()-1, );
00061 
00062 void node::set_link(int link_number, node *new_link)
00063 {
00064   test_arg(link_number);
00065   (*_links)[link_number]->_connection = new_link;
00066 }
00067 
00068 void node::zap_link(int link_number)
00069 {
00070   test_arg(link_number);
00071   _links->zap(link_number, link_number);
00072 }
00073 
00074 void node::insert_link(int where, node *to_insert)
00075 {
00076   // make sure that the index to insert at will not be rejected by the
00077   // amorph insert operation.
00078   if (where > links())
00079     where = links();
00080   _links->insert(where, 1);
00081   set_empty(where);
00082   set_link(where, to_insert);
00083 }
00084 
00085 node *node::get_link(int link_number) const
00086 {
00087   bounds_return(link_number, 0, _links->elements()-1, NIL);
00088   return (*_links)[link_number]->_connection;
00089 }
00090 
00091 int node::which(node *branch_to_find) const
00092 {
00093   int to_return = common::NOT_FOUND;
00094   for (int i = 0; i <= links() - 1; i++)
00095     if (branch_to_find == get_link(i)) {
00096       to_return = i;
00097       break;
00098     }
00099   return to_return;
00100 }
00101 
00102 } // namespace.
00103 
00104 #endif //NODE_IMPLEMENTATION_FILE
00105 

Generated on Fri Nov 28 04:29:17 2008 for HOOPLE Libraries by  doxygen 1.5.1