]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/perf/strsrchperf/strsrchperf.h
ICU-461.18.tar.gz
[apple/icu.git] / icuSources / test / perf / strsrchperf / strsrchperf.h
CommitLineData
46f4442e
A
1/********************************************************************
2 * COPYRIGHT:
729e4ab9 3 * Copyright (C) 2008-2009 IBM, Inc. All Rights Reserved.
46f4442e
A
4 *
5 ********************************************************************/
6#ifndef _STRSRCHPERF_H
7#define _STRSRCHPERF_H
8
9#include "unicode/ubrk.h"
10#include "unicode/usearch.h"
729e4ab9
A
11#include "unicode/colldata.h"
12#include "unicode/bmsearch.h"
46f4442e
A
13#include "unicode/uperf.h"
14#include <stdlib.h>
15#include <stdio.h>
16
729e4ab9
A
17#define TEST_BOYER_MOORE_SEARCH
18
19#ifdef TEST_BOYER_MOORE_SEARCH
20typedef void (*StrSrchFn) (BoyerMooreSearch * bms, const UChar *src, int32_t srcLen, const UChar *pttrn, int32_t pttrnLen, UErrorCode *status);
21#else
46f4442e 22typedef void (*StrSrchFn)(UStringSearch* srch, const UChar* src,int32_t srcLen, const UChar* pttrn, int32_t pttrnLen, UErrorCode* status);
729e4ab9 23#endif
46f4442e
A
24
25class StringSearchPerfFunction : public UPerfFunction {
26private:
27 StrSrchFn fn;
28 const UChar* src;
29 int32_t srcLen;
30 const UChar* pttrn;
31 int32_t pttrnLen;
729e4ab9
A
32#ifdef TEST_BOYER_MOORE_SEARCH
33 BoyerMooreSearch *bms;
34#else
46f4442e 35 UStringSearch* srch;
729e4ab9 36#endif
46f4442e
A
37
38public:
39 virtual void call(UErrorCode* status) {
729e4ab9
A
40#ifdef TEST_BOYER_MOORE_SEARCH
41 (*fn)(bms, src, srcLen, pttrn, pttrnLen, status);
42#else
46f4442e 43 (*fn)(srch, src, srcLen, pttrn, pttrnLen, status);
729e4ab9 44#endif
46f4442e
A
45 }
46
47 virtual long getOperationsPerIteration() {
729e4ab9 48#if 0
46f4442e 49 return (long)(srcLen/pttrnLen);
729e4ab9
A
50#else
51 return (long) srcLen;
52#endif
46f4442e
A
53 }
54
729e4ab9
A
55#ifdef TEST_BOYER_MOORE_SEARCH
56 StringSearchPerfFunction(StrSrchFn func, BoyerMooreSearch *search, const UChar *source, int32_t sourceLen, const UChar *pattern, int32_t patternLen) {
57 fn = func;
58 src = source;
59 srcLen = sourceLen;
60 pttrn = pattern;
61 pttrnLen = patternLen;
62 bms = search;
63 }
64#else
46f4442e
A
65 StringSearchPerfFunction(StrSrchFn func, UStringSearch* search, const UChar* source,int32_t sourceLen, const UChar* pattern, int32_t patternLen) {
66 fn = func;
67 src = source;
68 srcLen = sourceLen;
69 pttrn = pattern;
70 pttrnLen = patternLen;
71 srch = search;
72 }
729e4ab9 73#endif
46f4442e
A
74};
75
76class StringSearchPerformanceTest : public UPerfTest {
77private:
78 const UChar* src;
79 int32_t srcLen;
80 UChar* pttrn;
81 int32_t pttrnLen;
729e4ab9
A
82#ifdef TEST_BOYER_MOORE_SEARCH
83 UnicodeString *targetString;
84 BoyerMooreSearch *bms;
85#else
46f4442e 86 UStringSearch* srch;
729e4ab9 87#endif
46f4442e
A
88
89public:
90 StringSearchPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status);
91 ~StringSearchPerformanceTest();
92 virtual UPerfFunction* runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = NULL);
93
94 UPerfFunction* Test_ICU_Forward_Search();
729e4ab9 95
46f4442e
A
96 UPerfFunction* Test_ICU_Backward_Search();
97};
98
729e4ab9
A
99
100#ifdef TEST_BOYER_MOORE_SEARCH
101void ICUForwardSearch(BoyerMooreSearch *bms, const UChar *source, int32_t sourceLen, const UChar *pattern, int32_t patternLen, UErrorCode * /*status*/) {
102 int32_t offset = 0, start = -1, end = -1;
103
104 while (bms->search(offset, start, end)) {
105 offset = end;
106 }
107}
108
109void ICUBackwardSearch(BoyerMooreSearch *bms, const UChar *source, int32_t sourceLen, const UChar *pattern, int32_t patternLen, UErrorCode * /*status*/) {
110 int32_t offset = 0, start = -1, end = -1;
111
112 /* NOTE: No Boyer-Moore backward search yet... */
113 while (bms->search(offset, start, end)) {
114 offset = end;
115 }
116}
117#else
46f4442e
A
118void ICUForwardSearch(UStringSearch *srch, const UChar* source, int32_t sourceLen, const UChar* pattern, int32_t patternLen, UErrorCode* status) {
119 int32_t match;
120
121 match = usearch_first(srch, status);
122 while (match != USEARCH_DONE) {
123 match = usearch_next(srch, status);
124 }
125}
126
127void ICUBackwardSearch(UStringSearch *srch, const UChar* source, int32_t sourceLen, const UChar* pattern, int32_t patternLen, UErrorCode* status) {
128 int32_t match;
129
130 match = usearch_last(srch, status);
131 while (match != USEARCH_DONE) {
132 match = usearch_previous(srch, status);
133 }
134}
729e4ab9 135#endif
46f4442e
A
136
137#endif /* _STRSRCHPERF_H */