// file: $isip/class/search/Context/Context.h // version: $Id: Context.h 9042 2003-03-12 17:35:25Z huang $ // // make sure definitions are only made once // #ifndef ISIP_CONTEXT #define ISIP_CONTEXT // isip include files // #ifndef ISIP_GRAPH_VERTEX #include #endif #ifndef ISIP_CIRCULAR_DELAY_LINE #include #endif #ifndef ISIP_SEARCH_NODE #include #endif // forward class definition(s): // class SearchNode; // Context: a class to keep the context through a search // it contain a circular delay line of pointers to GraphVertex // these pointers stored in the form of Ulong // since we need to generate a hash table that maps all accounted // contexts to subgraphs at the next lower level, we need to provide // a hashing function here // class Context : public CircularDelayLine { //--------------------------------------------------------------------------- // // public constants // //--------------------------------------------------------------------------- public: // define the class name // static const String CLASS_NAME; //---------------------------------------- // // i/o related constants // //---------------------------------------- static const String PARAM_CENTRAL; static const String PARAM_EXTENSION_LENGTH; //---------------------------------------- // // default values and arguments // //---------------------------------------- // default values // static const int32 DEF_LENGTH = 1; static const int32 DEF_CENTRAL_INDEX = 1; static const int32 DEF_LAST_LENGTH = -1; static const int32 DEF_EXTENSION_LENGTH = 0; static const ulong DEF_HASH_INDEX = 0; // define default arguments to methods // static const ulong DEF_REF_INCR = 1; static const ulong DEF_REF_DECR = 1; //--------------------------------------- // // error codes // //--------------------------------------- static const int32 ERR = 90100; //--------------------------------------------------------------------------- // // protected data // //--------------------------------------------------------------------------- protected: // index of the central vertex in context // int32 central_d; // length of the future context extension, used if we have a // cross-symbol context // int32 ext_length_d; // static debug level // static Integral::DEBUG debug_level_d; // static memory manager // static MemoryManager mgr_d; // type definition // typedef GraphVertex GVSnode; //--------------------------------------------------------------------------- // // required public methods // //--------------------------------------------------------------------------- public: // method: name // static const String& name() { return CLASS_NAME; } // other static methods // static bool8 diagnose(Integral::DEBUG debug_level); // the debug method is inherited from the Stack class // // method: setDebug // static bool8 setDebug(Integral::DEBUG debug_level) { debug_level_d = debug_level; return true; } // method: destructor // ~Context() { if (debug_level_d >= Integral::ALL) { fprintf(stdout, "Destructor of context: %p\n", this); fflush(stdout); } } // method: default constructor // Context(int32 context_length = DEF_LENGTH, int32 central_index = DEF_CENTRAL_INDEX) : CircularDelayLine(context_length) { if (debug_level_d >= Integral::ALL) { fprintf(stdout, "Constructor of context: %p\n", this); fflush(stdout); } central_d = central_index; ext_length_d = DEF_EXTENSION_LENGTH; } // method: copy constructor // Context(const Context& copy_context) : CircularDelayLine(copy_context.length()) { if (debug_level_d >= Integral::ALL) { fprintf(stdout, "Constructor of context: %p\n", this); fflush(stdout); } assign(copy_context); } // assign method // bool8 assign(const Context& context_cmp); // equality method // bool8 eq(const Context& context_cmp) const; // method: sofSize // int32 sofSize() const; // method: read // bool8 read(Sof& sof, int32 tag, const String& name = CLASS_NAME); // method: write // bool8 write(Sof& sof, int32 tag, const String& name = CLASS_NAME) const; // method: readData // bool8 readData(Sof& sof, const String& pname = String::EMPTY, int32 size = SofParser::FULL_OBJECT, bool8 param = true, bool8 nested = false); // method: writeData // bool8 writeData(Sof& sof, const String& pname = String::EMPTY) const; // method: new // static void* operator new(size_t size) { return mgr_d.get(); } // method: new[] // static void* operator new[](size_t size) { return mgr_d.getBlock(size); } // method: delete // static void operator delete(void* ptr) { mgr_d.release(ptr); } // method: delete[] // static void operator delete[](void* ptr) { mgr_d.releaseBlock(ptr); } // method: setGrowSize // static bool8 setGrowSize(int32 grow_size) { return mgr_d.setGrow(grow_size); } // method: clear // bool8 clear(Integral::CMODE cmode = Integral::DEF_CMODE) { // call the vector clear // v_d.clear(cmode); // clear the index in any case // index_d = DEF_INDEX; ext_length_d = DEF_EXTENSION_LENGTH; return true; } //--------------------------------------------------------------------------- // // class-specific public methods // //--------------------------------------------------------------------------- // method: getCentralLength // int32 getCentralLength() const { return central_d; } // method: setCentralLength // bool8 setCentralLength(int32 arg) { return (central_d = arg); } // method: getExtendedLength // int32 getExtendedLength() const { return ext_length_d; } // method: setExtendedLength // bool8 setExtendedLength(int32 arg) { return (ext_length_d = arg); } // method: isExtended // bool8 isExtended() { return (ext_length_d > 0); } // method: getCentralVertex // GVSnode* getCentralVertex() { ulong pointer_to_central = (*this)(-central_d - ext_length_d); return (GVSnode*) pointer_to_central; } // method: setCentralVertex // bool8 setCentralVertex(ulong new_gvnode_a) { (*this)(-central_d - ext_length_d) = new_gvnode_a; return true; } // method: getBeforeCentralVertex // GVSnode* getBeforeCentralVertex() { ulong pointer_to_vertex = (*this)(-central_d - ext_length_d - 1); return (GVSnode*) pointer_to_vertex; } // method: setBeforeCentralVertex // bool8 setBeforeCentralVertex(ulong new_gvnode_a) { (*this)(-central_d - ext_length_d - 1 ) = new_gvnode_a; return true; } // method: getAfterCentralVertex // GVSnode* getAfterCentralVertex() { ulong pointer_to_vertex = (*this)(-central_d - ext_length_d + 1); return (GVSnode*) pointer_to_vertex; } // method: getLastVertex // GVSnode* getLastVertex() { ulong pointer_to_last = (*this)(DEF_LAST_LENGTH); return (GVSnode*) pointer_to_last; } // method: setLastVertex // bool8 setLastVertex(ulong new_gvnode_a) { (*this)(DEF_LAST_LENGTH) = new_gvnode_a; return true; } // method to print the context // bool8 print(); bool8 print(String& output); // method to convert GraphVertex pointers to SearchSymbol // indices (non-reversible) // // conversion needs to be done before hashing, because key to the // hash table is the array of search indices, not array of pointers // to graph vertices // bool8 convert(); bool8 convert(Context*& output, bool8 extended = false); // methods to extend the context // bool8 fold(); bool8 extend(GVSnode* pointer_to_vertex); // method: getHashIndex // int32 getHashIndex() { ulong hash_index = (ulong)0; for (int32 i = v_d.length(); i > 0; i--) { ulong index = (ulong)(*this)(-i); hash_index = (hash_index << 5) ^ (hash_index >> 27) ^ index; } return hash_index; } // hash function // int32 hash(int32 capacity) const; //--------------------------------------------------------------------------- // // class-specific public methods: // core data manipulation methods // //--------------------------------------------------------------------------- // method: advanceAndAssign // bool8 advanceAndAssign(const Ulong& input, int32 increment = DEF_INCREMENT) { bool8 status = advance(increment); v_d(index_d) = input; return status; } // method: assignAndAdvance // bool8 assignAndAdvance(const Ulong& input, int32 increment = DEF_INCREMENT) { v_d(index_d) = input; bool8 status = advance(increment); return status; } //--------------------------------------------------------------------------- // // private methods // //--------------------------------------------------------------------------- private: }; // end of include file // #endif