- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Planning/Reactive/CCA/rpPolicies.cpp

    00001 //==============================================================================
    00002 //
    00003 // Copyright (c) 2001           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Author:                      Seung H. Chung
    00010 // Organization:        Model-based Embedded and Robotic Systems Group
    00011 //                                      Massachusetts Institute of Technology
    00012 //
    00013 // rpPolicies class
    00014 //
    00015 //==============================================================================
    00016
    00017 #include <cstdio>
    00018 #include <cassert>
    00019
    00020 #include "rpState.h"
    00021 #include "rpTransition.h"
    00022 #include "rpPolicies.h"
    00023 //------------------------------------------------------------------------------
    00024 // Constructors, Copy Constructors, & Destructors
    00025 //------------------------------------------------------------------------------
    00026 rpPolicies::rpPolicies() {
    00027         _rpTransitionPtr_array  = NULL;
    00028         _width                                  = 0;
    00029 }
    00030 rpPolicies::rpPolicies(sizeType numb) {
    00031         _width                                  = numb;
    00032         _rpTransitionPtr_array  = new const rpTransition *[size()];
    00033         for (sizeType i = 0; i < size(); i++) {
    00034                 _rpTransitionPtr_array[i] = NULL;
    00035         }
    00036 }
    00037 rpPolicies::rpPolicies(const rpPolicies &other) {
    00038         if (this != &other) {
    00039                 copy(other);
    00040         }
    00041 }
    00042 rpPolicies::~rpPolicies() {
    00043         destroy();
    00044 }
    00045 //------------------------------------------------------------------------------
    00046 // Public Function Members
    00047 //------------------------------------------------------------------------------
    00048 const unsigned int rpPolicies::size() const {
    00049         return _width * _width;
    00050 }
    00051 const bool rpPolicies::empty() const {
    00052         return (_width == 0);
    00053 }
    00054 const rpTransition *const rpPolicies::rpPolicy(const rpState &from_rpState,
    00055                                                                                            const rpState &to_rpState) const {
    00056         if (index(from_rpState.index(), to_rpState.index()) < size()) {
    00057                 return _rpTransitionPtr_array[index(from_rpState.index(),
    00058                                                                                         to_rpState.index())];
    00059         } else {
    00060                 return NULL;
    00061         }
    00062 }
    00063 //------------------------------------------------------------------------------
    00064 const bool rpPolicies::set_rpPolicy(const rpState &from_rpState,
    00065                                                                         const rpState &to_rpState,
    00066                                                                         const rpTransition &_rpTransition) {
    00067         if (index(from_rpState.index(), to_rpState.index()) < size()) {
    00068                 _rpTransitionPtr_array[index(from_rpState.index(),
    00069                                                                          to_rpState.index())] = &_rpTransition;
    00070                 return true;
    00071         } else {
    00072                 return false;
    00073         }
    00074 }
    00075 void rpPolicies::clear() {
    00076         for (sizeType i = 0; i < size(); i++) {
    00077                 _rpTransitionPtr_array[i] = NULL;
    00078         }
    00079 }
    00080 //------------------------------------------------------------------------------
    00081 // Public Operators
    00082 //------------------------------------------------------------------------------
    00083 const rpTransition *&rpPolicies::operator [](const unsigned int index_) {
    00084         if (index_ < size()) {
    00085                 return _rpTransitionPtr_array[index_];
    00086         } else {
    00087                 return _rpTransitionPtr_array[0];
    00088         }
    00089 }
    00090 const rpTransition *const & rpPolicies::operator [](const unsigned int index_) const {
    00091         if (index_ < size()) {
    00092                 return _rpTransitionPtr_array[index_];
    00093         } else {
    00094                 return _rpTransitionPtr_array[0];
    00095         }
    00096 }
    00097 const rpPolicies &rpPolicies::operator =(const rpPolicies &other) {
    00098         if (this != &other) {
    00099                 destroy();
    00100                 copy(other);
    00101         }
    00102         return (*this);
    00103 }
    00104 const bool rpPolicies::operator ==(const rpPolicies &other) const {
    00105         if (_width == other._width) {
    00106                 for (sizeType i = 0; i < size(); i++) {
    00107                         if (_rpTransitionPtr_array[i] != other._rpTransitionPtr_array[i]) return false;
    00108                 }
    00109                 return true;
    00110         }
    00111         return false;
    00112 }
    00113 const bool rpPolicies::operator !=(const rpPolicies &other) const {
    00114         return !(*this == other);
    00115 }
    00116 std::ostream &operator <<(std::ostream &_ostream, const rpPolicies &_rpPolicies) {
    00117         unsigned int i, j;
    00118         for (i = 0; i < _rpPolicies._width; i++) {
    00119                 for (j = 0; j < _rpPolicies._width; j++) {
    00120                         if (_rpPolicies[i*_rpPolicies._width + j] == NULL) {
    00121                                 _ostream << i << " -> " << j << " None";
    00122                         } else {
    00123                                 _ostream << i << " -> " << j << " " << *_rpPolicies[i*_rpPolicies._width + j];
    00124                         }
    00125                         if (i * _rpPolicies._width + j < _rpPolicies.size() - 1) {
    00126                                 _ostream << std::endl;
    00127                         }
    00128                 }
    00129         }
    00130         return _ostream;
    00131 }
    00132 //------------------------------------------------------------------------------
    00133 // Private Function Members
    00134 //------------------------------------------------------------------------------
    00135 const unsigned int rpPolicies::index(const unsigned int from_index,
    00136                                                                          const unsigned int to_index) const {
    00137         return from_index * _width + to_index;
    00138 }
    00139 //------------------------------------------------------------------------------
    00140 void rpPolicies::destroy() {
    00141         clear();
    00142         delete [] _rpTransitionPtr_array;
    00143 }
    00144 void rpPolicies::copy(const rpPolicies &other) {
    00145         _width                                  = other._width;
    00146         _rpTransitionPtr_array  = new const rpTransition *[size()];
    00147         for (sizeType i = 0; i < size(); i++) {
    00148                 _rpTransitionPtr_array[i] = other[i];
    00149         }
    00150 }