00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00027 #include <cstdarg>
00028 #include <cassert>
00029 #include <sstream>
00030 #include "ShVariableNode.hpp"
00031 #include "ShError.hpp"
00032 #include "ShDebug.hpp"
00033 #include "ShAlgebra.hpp"
00034 #include "ShManipulator.hpp"
00035
00036 namespace SH {
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 template<>
00050 SH_DLLEXPORT
00051 int OffsetRange<int>::absIndex( int index, int offset, const ShProgramNode::VarList &v ) const {
00052 if( offset == _FIRST ) return 0;
00053 else if( offset == _SECOND ) return 1;
00054 else if( offset == _LAST ) return v.size() - 1;
00055 if( index < 0 ) index += v.size();
00056 if( index < 0 || index >= static_cast<int>(v.size()) ) return OFFSET_RANGE_BAD_INDEX;
00057
00058 index += offset;
00059 if( index < 0 || index >= static_cast<int>(v.size()) ) return OFFSET_RANGE_BAD_OFFSET;
00060 return index;
00061 }
00062
00063
00064 template<>
00065 SH_DLLEXPORT
00066 int OffsetRange<std::string>::absIndex( std::string index, int offset,
00067 const ShProgramNode::VarList &v ) const {
00068 if( offset == _FIRST ) return 0;
00069 else if( offset == _SECOND ) return 1;
00070 else if( offset == _LAST ) return v.size() - 1;
00071
00072 int result;
00073 result = OFFSET_RANGE_BAD_INDEX;
00074
00075
00076 ShProgramNode::VarList::const_iterator it = v.begin();
00077 for( int i = 0; it != v.end(); ++it, ++i ) {
00078 if( (*it)->name() == index && result == OFFSET_RANGE_BAD_INDEX ) {
00079 result = i;
00080 break;
00081 }
00082 }
00083
00084 if( result == OFFSET_RANGE_BAD_INDEX ) {
00085 std::ostringstream os;
00086 os << "Could not find index " << index << (offset >= 0 ? "+" : "")
00087 << offset << " in variable channels:";
00088 ShProgramNode::print(os, v) << std::endl;
00089 shError( ShAlgebraException( os.str()));
00090 }
00091
00092 result += offset;
00093 if( result < 0 || result >= static_cast<int>(v.size())) result = OFFSET_RANGE_BAD_OFFSET;
00094 return result;
00095 }
00096
00097 }
00098