2010-12-31 18:21:36 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2012-02-20 04:28:17 +00:00
|
|
|
* Copyright (C) 2010-2012, International Business Machines
|
2010-12-31 18:21:36 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*******************************************************************************
|
2011-01-05 21:05:47 +00:00
|
|
|
* file name: bytestriebuilder.h
|
2010-12-31 18:21:36 +00:00
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2010sep25
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*/
|
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
#ifndef __BYTESTRIEBUILDER_H__
|
|
|
|
#define __BYTESTRIEBUILDER_H__
|
2010-12-31 18:21:36 +00:00
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2011-03-03 21:56:36 +00:00
|
|
|
#include "unicode/bytestrie.h"
|
2010-12-31 18:21:36 +00:00
|
|
|
#include "unicode/stringpiece.h"
|
2011-03-03 21:56:36 +00:00
|
|
|
#include "unicode/stringtriebuilder.h"
|
2010-12-31 18:21:36 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
class BytesTrieElement;
|
2011-03-03 21:56:36 +00:00
|
|
|
class CharString;
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
/**
|
|
|
|
* Builder class for BytesTrie.
|
2011-01-27 21:41:03 +00:00
|
|
|
*
|
|
|
|
* This class is not intended for public subclassing.
|
2012-02-20 04:28:17 +00:00
|
|
|
* @stable ICU 4.8
|
2011-01-05 21:05:47 +00:00
|
|
|
*/
|
2011-03-03 21:56:36 +00:00
|
|
|
class U_COMMON_API BytesTrieBuilder : public StringTrieBuilder {
|
2010-12-31 18:21:36 +00:00
|
|
|
public:
|
2011-01-27 21:41:03 +00:00
|
|
|
/**
|
|
|
|
* Constructs an empty builder.
|
|
|
|
* @param errorCode Standard ICU error code.
|
2012-02-20 04:28:17 +00:00
|
|
|
* @stable ICU 4.8
|
2011-01-27 21:41:03 +00:00
|
|
|
*/
|
|
|
|
BytesTrieBuilder(UErrorCode &errorCode);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
2012-02-20 04:28:17 +00:00
|
|
|
* @stable ICU 4.8
|
2011-01-27 21:41:03 +00:00
|
|
|
*/
|
2011-01-05 21:05:47 +00:00
|
|
|
virtual ~BytesTrieBuilder();
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-01-27 21:41:03 +00:00
|
|
|
/**
|
|
|
|
* Adds a (byte sequence, value) pair.
|
|
|
|
* The byte sequence must be unique.
|
|
|
|
* The bytes will be copied; the builder does not keep
|
|
|
|
* a reference to the input StringPiece or its data().
|
|
|
|
* @param s The input byte sequence.
|
|
|
|
* @param value The value associated with this byte sequence.
|
|
|
|
* @param errorCode Standard ICU error code. Its input value must
|
|
|
|
* pass the U_SUCCESS() test, or else the function returns
|
|
|
|
* immediately. Check for U_FAILURE() on output or use with
|
|
|
|
* function chaining. (See User Guide for details.)
|
|
|
|
* @return *this
|
2012-02-20 04:28:17 +00:00
|
|
|
* @stable ICU 4.8
|
2011-01-27 21:41:03 +00:00
|
|
|
*/
|
2011-01-05 21:05:47 +00:00
|
|
|
BytesTrieBuilder &add(const StringPiece &s, int32_t value, UErrorCode &errorCode);
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-01-27 21:41:03 +00:00
|
|
|
/**
|
|
|
|
* Builds a BytesTrie for the add()ed data.
|
|
|
|
* Once built, no further data can be add()ed until clear() is called.
|
|
|
|
*
|
|
|
|
* This method passes ownership of the builder's internal result array to the new trie object.
|
|
|
|
* Another call to any build() variant will re-serialize the trie.
|
|
|
|
* After clear() has been called, a new array will be used as well.
|
|
|
|
* @param buildOption Build option, see UStringTrieBuildOption.
|
|
|
|
* @param errorCode Standard ICU error code. Its input value must
|
|
|
|
* pass the U_SUCCESS() test, or else the function returns
|
|
|
|
* immediately. Check for U_FAILURE() on output or use with
|
|
|
|
* function chaining. (See User Guide for details.)
|
|
|
|
* @return A new BytesTrie for the add()ed data.
|
2012-02-20 04:28:17 +00:00
|
|
|
* @stable ICU 4.8
|
2011-01-27 21:41:03 +00:00
|
|
|
*/
|
|
|
|
BytesTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a BytesTrie for the add()ed data and byte-serializes it.
|
|
|
|
* Once built, no further data can be add()ed until clear() is called.
|
|
|
|
*
|
|
|
|
* Multiple calls to buildStringPiece() return StringPieces referring to the
|
|
|
|
* builder's same byte array, without rebuilding.
|
|
|
|
* If buildStringPiece() is called after build(), the trie will be
|
|
|
|
* re-serialized into a new array.
|
|
|
|
* If build() is called after buildStringPiece(), the trie object will become
|
|
|
|
* the owner of the previously returned array.
|
|
|
|
* After clear() has been called, a new array will be used as well.
|
|
|
|
* @param buildOption Build option, see UStringTrieBuildOption.
|
|
|
|
* @param errorCode Standard ICU error code. Its input value must
|
|
|
|
* pass the U_SUCCESS() test, or else the function returns
|
|
|
|
* immediately. Check for U_FAILURE() on output or use with
|
|
|
|
* function chaining. (See User Guide for details.)
|
|
|
|
* @return A StringPiece which refers to the byte-serialized BytesTrie for the add()ed data.
|
2012-02-20 04:28:17 +00:00
|
|
|
* @stable ICU 4.8
|
2011-01-27 21:41:03 +00:00
|
|
|
*/
|
|
|
|
StringPiece buildStringPiece(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-01-27 21:41:03 +00:00
|
|
|
/**
|
|
|
|
* Removes all (byte sequence, value) pairs.
|
|
|
|
* New data can then be add()ed and a new trie can be built.
|
|
|
|
* @return *this
|
2012-02-20 04:28:17 +00:00
|
|
|
* @stable ICU 4.8
|
2011-01-27 21:41:03 +00:00
|
|
|
*/
|
2011-03-03 21:56:36 +00:00
|
|
|
BytesTrieBuilder &clear();
|
2010-12-31 18:21:36 +00:00
|
|
|
|
|
|
|
private:
|
2011-01-27 21:41:03 +00:00
|
|
|
BytesTrieBuilder(const BytesTrieBuilder &other); // no copy constructor
|
|
|
|
BytesTrieBuilder &operator=(const BytesTrieBuilder &other); // no assignment operator
|
|
|
|
|
|
|
|
void buildBytes(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
|
|
|
|
|
2011-01-02 07:22:36 +00:00
|
|
|
virtual int32_t getElementStringLength(int32_t i) const;
|
|
|
|
virtual UChar getElementUnit(int32_t i, int32_t byteIndex) const;
|
|
|
|
virtual int32_t getElementValue(int32_t i) const;
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-01-06 05:04:51 +00:00
|
|
|
virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t byteIndex) const;
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-01-02 07:22:36 +00:00
|
|
|
virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t byteIndex) const;
|
|
|
|
virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t byteIndex, int32_t count) const;
|
|
|
|
virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t byteIndex, UChar byte) const;
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-01-02 07:22:36 +00:00
|
|
|
virtual UBool matchNodesCanHaveValues() const { return FALSE; }
|
2010-12-31 23:56:06 +00:00
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
virtual int32_t getMaxBranchLinearSubNodeLength() const { return BytesTrie::kMaxBranchLinearSubNodeLength; }
|
|
|
|
virtual int32_t getMinLinearMatch() const { return BytesTrie::kMinLinearMatch; }
|
|
|
|
virtual int32_t getMaxLinearMatchLength() const { return BytesTrie::kMaxLinearMatchLength; }
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-08-01 02:20:46 +00:00
|
|
|
#ifndef U_HIDE_INTERNAL_API
|
2011-05-05 01:35:22 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2010-12-31 18:21:36 +00:00
|
|
|
class BTLinearMatchNode : public LinearMatchNode {
|
|
|
|
public:
|
|
|
|
BTLinearMatchNode(const char *units, int32_t len, Node *nextNode);
|
|
|
|
virtual UBool operator==(const Node &other) const;
|
2011-01-05 21:05:47 +00:00
|
|
|
virtual void write(StringTrieBuilder &builder);
|
2010-12-31 18:21:36 +00:00
|
|
|
private:
|
|
|
|
const char *s;
|
|
|
|
};
|
2011-08-01 02:20:46 +00:00
|
|
|
#endif /* U_HIDE_INTERNAL_API */
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-01-06 05:04:51 +00:00
|
|
|
virtual Node *createLinearMatchNode(int32_t i, int32_t byteIndex, int32_t length,
|
2011-01-02 07:22:36 +00:00
|
|
|
Node *nextNode) const;
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-01-02 07:22:36 +00:00
|
|
|
UBool ensureCapacity(int32_t length);
|
|
|
|
virtual int32_t write(int32_t byte);
|
|
|
|
int32_t write(const char *b, int32_t length);
|
|
|
|
virtual int32_t writeElementUnits(int32_t i, int32_t byteIndex, int32_t length);
|
2011-01-06 05:04:51 +00:00
|
|
|
virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal);
|
2011-01-02 07:22:36 +00:00
|
|
|
virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node);
|
|
|
|
virtual int32_t writeDeltaTo(int32_t jumpTarget);
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-03-03 21:56:36 +00:00
|
|
|
CharString *strings; // Pointer not object so we need not #include internal charstr.h.
|
2011-01-05 21:05:47 +00:00
|
|
|
BytesTrieElement *elements;
|
2010-12-31 18:21:36 +00:00
|
|
|
int32_t elementsCapacity;
|
|
|
|
int32_t elementsLength;
|
|
|
|
|
|
|
|
// Byte serialization of the trie.
|
|
|
|
// Grows from the back: bytesLength measures from the end of the buffer!
|
|
|
|
char *bytes;
|
|
|
|
int32_t bytesCapacity;
|
|
|
|
int32_t bytesLength;
|
|
|
|
};
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
#endif // __BYTESTRIEBUILDER_H__
|