searchengine/oss/loc/analysis/inc/private/statemachine.inl
changeset 24 65456528cac2
equal deleted inserted replaced
23:d4d56f5e7c55 24:65456528cac2
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef STATEMACHINE_INL_
       
    19 #define STATEMACHINE_INL_
       
    20 
       
    21 namespace analysis {
       
    22 
       
    23 	template<class Encoding>
       
    24 	StateCursor<Encoding>::StateCursor()
       
    25 	:  blob_(0), 
       
    26 	   pointer_(0)
       
    27 	{}
       
    28 	
       
    29 	template<class Encoding>
       
    30 	void StateCursor<Encoding>::reset(byte_t* blob, StateOffset offset)
       
    31 	{
       
    32 		blob_ = blob; 
       
    33 		pointer_ = &blob[offset]; 
       
    34 	}
       
    35 
       
    36 	template<class Encoding>
       
    37 	bool StateCursor<Encoding>::isFinal() 
       
    38 	{
       
    39 		return Encoding::getFinal(pointer_); 
       
    40 	}
       
    41 		
       
    42 	template<class Encoding>
       
    43 	bool StateCursor<Encoding>::next(wchar_t c) 
       
    44 	{
       
    45 		byte_t* p = pointer_; 
       
    46 
       
    47 		// How many transitions we have
       
    48 		int n = Encoding::readSuccCount(p);
       
    49 
       
    50 		// Search transition that matches 'c'
       
    51 		for (int i = 0; i < n; i++) 
       
    52 		{ 	// linear speed: O(n/2)
       
    53 			wchar_t c2 = Encoding::readChar(p);
       
    54 			if ( c2 == c ) {
       
    55 				// Found transition, state is changed.
       
    56 				// Read pointer to the next state
       
    57 				int offset = Encoding::getOffset(p);
       
    58 				pointer_ = &blob_[offset];
       
    59 				return true; 
       
    60 			} else if ( c2 > c ) break;
       
    61 			// Skip pointer to next state
       
    62 			p += Encoding::SIZEOF_OFFSET;
       
    63 		}
       
    64 		// Maching transition was not found
       
    65 		return false; 
       
    66 	}
       
    67 
       
    68 	template<class Encoding>
       
    69 	const StateOffset StateMachine<Encoding>::ROOT_STATE_OFFSET = 0;
       
    70 	
       
    71 	template<class Encoding>
       
    72 	StateMachine<Encoding>::StateMachine()
       
    73 	:	blob_(0)
       
    74 	{}
       
    75 	
       
    76 	template<class Encoding>
       
    77 	void StateMachine<Encoding>::reset(byte_t* blob) 
       
    78 	{
       
    79 		blob_ = blob; 
       
    80 	}
       
    81 			
       
    82 	template<class Encoding>
       
    83 	void StateMachine<Encoding>::rootState(StateCursor<Encoding>& cursor)
       
    84 	{
       
    85 		cursor.reset(blob_, ROOT_STATE_OFFSET); 
       
    86 	}
       
    87 
       
    88 }
       
    89 
       
    90 
       
    91 #endif /* STATEMACHINE_INL_ */