Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:03

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 *******************************************************************************
0005 *   Copyright (C) 2010-2016, International Business Machines
0006 *   Corporation and others.  All Rights Reserved.
0007 *******************************************************************************
0008 *   file name:  bytestriebuilder.h
0009 *   encoding:   UTF-8
0010 *   tab size:   8 (not used)
0011 *   indentation:4
0012 *
0013 *   created on: 2010sep25
0014 *   created by: Markus W. Scherer
0015 */
0016 
0017 /**
0018  * \file
0019  * \brief C++ API: Builder for icu::BytesTrie
0020  */
0021 
0022 #ifndef __BYTESTRIEBUILDER_H__
0023 #define __BYTESTRIEBUILDER_H__
0024 
0025 #include "unicode/utypes.h"
0026 
0027 #if U_SHOW_CPLUSPLUS_API
0028 
0029 #include "unicode/bytestrie.h"
0030 #include "unicode/stringpiece.h"
0031 #include "unicode/stringtriebuilder.h"
0032 
0033 class BytesTrieTest;
0034 
0035 U_NAMESPACE_BEGIN
0036 
0037 class BytesTrieElement;
0038 class CharString;
0039 /**
0040  * Builder class for BytesTrie.
0041  *
0042  * This class is not intended for public subclassing.
0043  * @stable ICU 4.8
0044  */
0045 class U_COMMON_API BytesTrieBuilder : public StringTrieBuilder {
0046 public:
0047     /**
0048      * Constructs an empty builder.
0049      * @param errorCode Standard ICU error code.
0050      * @stable ICU 4.8
0051      */
0052     BytesTrieBuilder(UErrorCode &errorCode);
0053 
0054     /**
0055      * Destructor.
0056      * @stable ICU 4.8
0057      */
0058     virtual ~BytesTrieBuilder();
0059 
0060     /**
0061      * Adds a (byte sequence, value) pair.
0062      * The byte sequence must be unique.
0063      * The bytes will be copied; the builder does not keep
0064      * a reference to the input StringPiece or its data().
0065      * @param s The input byte sequence.
0066      * @param value The value associated with this byte sequence.
0067      * @param errorCode Standard ICU error code. Its input value must
0068      *                  pass the U_SUCCESS() test, or else the function returns
0069      *                  immediately. Check for U_FAILURE() on output or use with
0070      *                  function chaining. (See User Guide for details.)
0071      * @return *this
0072      * @stable ICU 4.8
0073      */
0074     BytesTrieBuilder &add(StringPiece s, int32_t value, UErrorCode &errorCode);
0075 
0076     /**
0077      * Builds a BytesTrie for the add()ed data.
0078      * Once built, no further data can be add()ed until clear() is called.
0079      *
0080      * A BytesTrie cannot be empty. At least one (byte sequence, value) pair
0081      * must have been add()ed.
0082      *
0083      * This method passes ownership of the builder's internal result array to the new trie object.
0084      * Another call to any build() variant will re-serialize the trie.
0085      * After clear() has been called, a new array will be used as well.
0086      * @param buildOption Build option, see UStringTrieBuildOption.
0087      * @param errorCode Standard ICU error code. Its input value must
0088      *                  pass the U_SUCCESS() test, or else the function returns
0089      *                  immediately. Check for U_FAILURE() on output or use with
0090      *                  function chaining. (See User Guide for details.)
0091      * @return A new BytesTrie for the add()ed data.
0092      * @stable ICU 4.8
0093      */
0094     BytesTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
0095 
0096     /**
0097      * Builds a BytesTrie for the add()ed data and byte-serializes it.
0098      * Once built, no further data can be add()ed until clear() is called.
0099      *
0100      * A BytesTrie cannot be empty. At least one (byte sequence, value) pair
0101      * must have been add()ed.
0102      *
0103      * Multiple calls to buildStringPiece() return StringPieces referring to the
0104      * builder's same byte array, without rebuilding.
0105      * If buildStringPiece() is called after build(), the trie will be
0106      * re-serialized into a new array (because build() passes on ownership).
0107      * If build() is called after buildStringPiece(), the trie object returned
0108      * by build() will become the owner of the underlying string for the
0109      * previously returned StringPiece.
0110      * After clear() has been called, a new array will be used as well.
0111      * @param buildOption Build option, see UStringTrieBuildOption.
0112      * @param errorCode Standard ICU error code. Its input value must
0113      *                  pass the U_SUCCESS() test, or else the function returns
0114      *                  immediately. Check for U_FAILURE() on output or use with
0115      *                  function chaining. (See User Guide for details.)
0116      * @return A StringPiece which refers to the byte-serialized BytesTrie for the add()ed data.
0117      * @stable ICU 4.8
0118      */
0119     StringPiece buildStringPiece(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
0120 
0121     /**
0122      * Removes all (byte sequence, value) pairs.
0123      * New data can then be add()ed and a new trie can be built.
0124      * @return *this
0125      * @stable ICU 4.8
0126      */
0127     BytesTrieBuilder &clear();
0128 
0129 private:
0130     friend class ::BytesTrieTest;
0131 
0132     BytesTrieBuilder(const BytesTrieBuilder &other) = delete;  // no copy constructor
0133     BytesTrieBuilder &operator=(const BytesTrieBuilder &other) = delete;  // no assignment operator
0134 
0135     void buildBytes(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
0136 
0137     virtual int32_t getElementStringLength(int32_t i) const override;
0138     virtual char16_t getElementUnit(int32_t i, int32_t byteIndex) const override;
0139     virtual int32_t getElementValue(int32_t i) const override;
0140 
0141     virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t byteIndex) const override;
0142 
0143     virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t byteIndex) const override;
0144     virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t byteIndex, int32_t count) const override;
0145     virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t byteIndex, char16_t byte) const override;
0146 
0147     virtual UBool matchNodesCanHaveValues() const override { return false; }
0148 
0149     virtual int32_t getMaxBranchLinearSubNodeLength() const override { return BytesTrie::kMaxBranchLinearSubNodeLength; }
0150     virtual int32_t getMinLinearMatch() const override { return BytesTrie::kMinLinearMatch; }
0151     virtual int32_t getMaxLinearMatchLength() const override { return BytesTrie::kMaxLinearMatchLength; }
0152 
0153     /**
0154      * @internal (private)
0155      */
0156     class BTLinearMatchNode : public LinearMatchNode {
0157     public:
0158         BTLinearMatchNode(const char *units, int32_t len, Node *nextNode);
0159         virtual bool operator==(const Node &other) const override;
0160         virtual void write(StringTrieBuilder &builder) override;
0161     private:
0162         const char *s;
0163     };
0164     
0165     virtual Node *createLinearMatchNode(int32_t i, int32_t byteIndex, int32_t length,
0166                                         Node *nextNode) const override;
0167 
0168     UBool ensureCapacity(int32_t length);
0169     virtual int32_t write(int32_t byte) override;
0170     int32_t write(const char *b, int32_t length);
0171     virtual int32_t writeElementUnits(int32_t i, int32_t byteIndex, int32_t length) override;
0172     virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal) override;
0173     virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node) override;
0174     virtual int32_t writeDeltaTo(int32_t jumpTarget) override;
0175     static int32_t internalEncodeDelta(int32_t i, char intBytes[]);
0176 
0177     CharString *strings;  // Pointer not object so we need not #include internal charstr.h.
0178     BytesTrieElement *elements;
0179     int32_t elementsCapacity;
0180     int32_t elementsLength;
0181 
0182     // Byte serialization of the trie.
0183     // Grows from the back: bytesLength measures from the end of the buffer!
0184     char *bytes;
0185     int32_t bytesCapacity;
0186     int32_t bytesLength;
0187 };
0188 
0189 U_NAMESPACE_END
0190 
0191 #endif /* U_SHOW_CPLUSPLUS_API */
0192 
0193 #endif  // __BYTESTRIEBUILDER_H__