Class to track data flow and detect a specific pattern

Committer:
chris215
Date:
Mon Jan 11 04:48:08 2016 +0000
Revision:
0:b8cca837d413
Child:
1:038d0018a542
Class to find a pattern. Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris215 0:b8cca837d413 1 #ifndef PATTERN_FINDER_H
chris215 0:b8cca837d413 2 #define PATTERN_FINDER_H
chris215 0:b8cca837d413 3 #include <stdint.h>
chris215 0:b8cca837d413 4 #include <string.h>
chris215 0:b8cca837d413 5
chris215 0:b8cca837d413 6
chris215 0:b8cca837d413 7 template <typename T>
chris215 0:b8cca837d413 8 class PatternFinder
chris215 0:b8cca837d413 9 {
chris215 0:b8cca837d413 10 private:
chris215 0:b8cca837d413 11 T *m_PatternReference;
chris215 0:b8cca837d413 12 T *m_CurrentPattern;
chris215 0:b8cca837d413 13 uint32_t m_PatternSize;
chris215 0:b8cca837d413 14 void ShiftDataInCurrentPattern(T data);
chris215 0:b8cca837d413 15 bool CheckForPattern(void);
chris215 0:b8cca837d413 16 public:
chris215 0:b8cca837d413 17 PatternFinder(T* Pattern,uint32_t PatternSize = 1);
chris215 0:b8cca837d413 18 ~PatternFinder();
chris215 0:b8cca837d413 19 bool CheckPattern(T data); //returns true if pattern was found in the stream
chris215 0:b8cca837d413 20 };
chris215 0:b8cca837d413 21
chris215 0:b8cca837d413 22 template <class T>
chris215 0:b8cca837d413 23 inline void PatternFinder<T>::ShiftDataInCurrentPattern(T data)
chris215 0:b8cca837d413 24 {
chris215 0:b8cca837d413 25 for(int x = (m_PatternSize-1);x > 0;x--)
chris215 0:b8cca837d413 26 m_CurrentPattern[x] = m_CurrentPattern[x-1];
chris215 0:b8cca837d413 27 m_CurrentPattern[0] = data;
chris215 0:b8cca837d413 28 }
chris215 0:b8cca837d413 29 template <class T>
chris215 0:b8cca837d413 30 inline bool PatternFinder<T>::CheckForPattern(void)
chris215 0:b8cca837d413 31 {
chris215 0:b8cca837d413 32 uint32_t NumberOfIdenticalElementsFoundInPattern = 0;
chris215 0:b8cca837d413 33 for(int x = 0;x < m_PatternSize;x++)
chris215 0:b8cca837d413 34 {
chris215 0:b8cca837d413 35 if(m_PatternReference[x] == m_CurrentPattern[x])
chris215 0:b8cca837d413 36 {
chris215 0:b8cca837d413 37 NumberOfIdenticalElementsFoundInPattern++;
chris215 0:b8cca837d413 38 }
chris215 0:b8cca837d413 39 }
chris215 0:b8cca837d413 40 return NumberOfIdenticalElementsFoundInPattern == m_PatternSize;
chris215 0:b8cca837d413 41 }
chris215 0:b8cca837d413 42
chris215 0:b8cca837d413 43 template <class T>
chris215 0:b8cca837d413 44 inline bool PatternFinder<T>::CheckPattern(T data)
chris215 0:b8cca837d413 45 {
chris215 0:b8cca837d413 46 ShiftDataInCurrentPattern(data);
chris215 0:b8cca837d413 47 return CheckForPattern();
chris215 0:b8cca837d413 48 }
chris215 0:b8cca837d413 49 #endif