michael@0: /* michael@0: ****************************************************************************** michael@0: * michael@0: * Copyright (C) 2007, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ****************************************************************************** michael@0: * file name: unisetspan.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2007mar01 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: #ifndef __UNISETSPAN_H__ michael@0: #define __UNISETSPAN_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/uniset.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /* michael@0: * Implement span() etc. for a set with strings. michael@0: * Avoid recursion because of its exponential complexity. michael@0: * Instead, try multiple paths at once and track them with an IndexList. michael@0: */ michael@0: class UnicodeSetStringSpan : public UMemory { michael@0: public: michael@0: /* michael@0: * Which span() variant will be used? michael@0: * The object is either built for one variant and used once, michael@0: * or built for all and may be used many times. michael@0: */ michael@0: enum { michael@0: FWD = 0x20, michael@0: BACK = 0x10, michael@0: UTF16 = 8, michael@0: UTF8 = 4, michael@0: CONTAINED = 2, michael@0: NOT_CONTAINED = 1, michael@0: michael@0: ALL = 0x3f, michael@0: michael@0: FWD_UTF16_CONTAINED = FWD | UTF16 | CONTAINED, michael@0: FWD_UTF16_NOT_CONTAINED = FWD | UTF16 | NOT_CONTAINED, michael@0: FWD_UTF8_CONTAINED = FWD | UTF8 | CONTAINED, michael@0: FWD_UTF8_NOT_CONTAINED = FWD | UTF8 | NOT_CONTAINED, michael@0: BACK_UTF16_CONTAINED = BACK | UTF16 | CONTAINED, michael@0: BACK_UTF16_NOT_CONTAINED= BACK | UTF16 | NOT_CONTAINED, michael@0: BACK_UTF8_CONTAINED = BACK | UTF8 | CONTAINED, michael@0: BACK_UTF8_NOT_CONTAINED = BACK | UTF8 | NOT_CONTAINED michael@0: }; michael@0: michael@0: UnicodeSetStringSpan(const UnicodeSet &set, const UVector &setStrings, uint32_t which); michael@0: michael@0: // Copy constructor. Assumes which==ALL for a frozen set. michael@0: UnicodeSetStringSpan(const UnicodeSetStringSpan &otherStringSpan, const UVector &newParentSetStrings); michael@0: michael@0: ~UnicodeSetStringSpan(); michael@0: michael@0: /* michael@0: * Do the strings need to be checked in span() etc.? michael@0: * @return TRUE if strings need to be checked (call span() here), michael@0: * FALSE if not (use a BMPSet for best performance). michael@0: */ michael@0: inline UBool needsStringSpanUTF16(); michael@0: inline UBool needsStringSpanUTF8(); michael@0: michael@0: // For fast UnicodeSet::contains(c). michael@0: inline UBool contains(UChar32 c) const; michael@0: michael@0: int32_t span(const UChar *s, int32_t length, USetSpanCondition spanCondition) const; michael@0: michael@0: int32_t spanBack(const UChar *s, int32_t length, USetSpanCondition spanCondition) const; michael@0: michael@0: int32_t spanUTF8(const uint8_t *s, int32_t length, USetSpanCondition spanCondition) const; michael@0: michael@0: int32_t spanBackUTF8(const uint8_t *s, int32_t length, USetSpanCondition spanCondition) const; michael@0: michael@0: private: michael@0: // Special spanLength byte values. michael@0: enum { michael@0: // The spanLength is >=0xfe. michael@0: LONG_SPAN=0xfe, michael@0: // All code points in the string are contained in the parent set. michael@0: ALL_CP_CONTAINED=0xff michael@0: }; michael@0: michael@0: // Add a starting or ending string character to the spanNotSet michael@0: // so that a character span ends before any string. michael@0: void addToSpanNotSet(UChar32 c); michael@0: michael@0: int32_t spanNot(const UChar *s, int32_t length) const; michael@0: int32_t spanNotBack(const UChar *s, int32_t length) const; michael@0: int32_t spanNotUTF8(const uint8_t *s, int32_t length) const; michael@0: int32_t spanNotBackUTF8(const uint8_t *s, int32_t length) const; michael@0: michael@0: // Set for span(). Same as parent but without strings. michael@0: UnicodeSet spanSet; michael@0: michael@0: // Set for span(not contained). michael@0: // Same as spanSet, plus characters that start or end strings. michael@0: UnicodeSet *pSpanNotSet; michael@0: michael@0: // The strings of the parent set. michael@0: const UVector &strings; michael@0: michael@0: // Pointer to the UTF-8 string lengths. michael@0: // Also pointer to further allocated storage for meta data and michael@0: // UTF-8 string contents as necessary. michael@0: int32_t *utf8Lengths; michael@0: michael@0: // Pointer to the part of the (utf8Lengths) memory block that stores michael@0: // the lengths of span(), spanBack() etc. for each string. michael@0: uint8_t *spanLengths; michael@0: michael@0: // Pointer to the part of the (utf8Lengths) memory block that stores michael@0: // the UTF-8 versions of the parent set's strings. michael@0: uint8_t *utf8; michael@0: michael@0: // Number of bytes for all UTF-8 versions of strings together. michael@0: int32_t utf8Length; michael@0: michael@0: // Maximum lengths of relevant strings. michael@0: int32_t maxLength16; michael@0: int32_t maxLength8; michael@0: michael@0: // Set up for all variants of span()? michael@0: UBool all; michael@0: michael@0: // Memory for small numbers and lengths of strings. michael@0: // For example, for 8 strings: michael@0: // 8 UTF-8 lengths, 8*4 bytes span lengths, 8*2 3-byte UTF-8 characters michael@0: // = 112 bytes = int32_t[28]. michael@0: int32_t staticLengths[32]; michael@0: }; michael@0: michael@0: UBool UnicodeSetStringSpan::needsStringSpanUTF16() { michael@0: return (UBool)(maxLength16!=0); michael@0: } michael@0: michael@0: UBool UnicodeSetStringSpan::needsStringSpanUTF8() { michael@0: return (UBool)(maxLength8!=0); michael@0: } michael@0: michael@0: UBool UnicodeSetStringSpan::contains(UChar32 c) const { michael@0: return spanSet.contains(c); michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif