Exemple #1
0
        private void checkData(StringAndValue[] data, int dataLength, TrieBuilderOption buildOption)
        {
            BytesTrie trie = buildTrie(data, dataLength, buildOption);

            checkFirst(trie, data, dataLength);
            checkNext(trie, data, dataLength);
            checkNextWithState(trie, data, dataLength);
            checkNextString(trie, data, dataLength);
            checkIterator(trie, data, dataLength);
        }
Exemple #2
0
        private BytesTrie buildTrie(StringAndValue[] data, int dataLength,
                                    TrieBuilderOption buildOption)
        {
            // Add the items to the trie builder in an interesting (not trivial, not random) order.
            int index, step;

            if ((dataLength & 1) != 0)
            {
                // Odd number of items.
                index = dataLength / 2;
                step  = 2;
            }
            else if ((dataLength % 3) != 0)
            {
                // Not a multiple of 3.
                index = dataLength / 5;
                step  = 3;
            }
            else
            {
                index = dataLength - 1;
                step  = -1;
            }
            builder_.Clear();
            for (int i = 0; i < dataLength; ++i)
            {
                builder_.Add(data[index].bytes, data[index].bytes.Length, data[index].value);
                index = (index + step) % dataLength;
            }
            BytesTrie trie = builder_.Build(buildOption);

            try
            {
                builder_.Add(/* "zzz" */ new byte[] { 0x7a, 0x7a, 0x7a }, 0, 999);
                Errln("builder.build().add(zzz) did not throw InvalidOperationException");
            }
            catch (InvalidOperationException e)
            {
                // good
            }
            ByteBuffer trieBytes = builder_.BuildByteBuffer(buildOption);

            Logln("serialized trie size: " + trieBytes.Remaining + " bytes\n");
            // Tries from either build() method should be identical but
            // BytesTrie does not implement equals().
            // We just return either one.
            if ((dataLength & 1) != 0)
            {
                return(trie);
            }
            else
            {
                return(new BytesTrie(trieBytes.Array, trieBytes.ArrayOffset + trieBytes.Position));
            }
        }
Exemple #3
0
        private CharsTrie buildTrie(StringAndValue[] data, int dataLength,
                                    TrieBuilderOption buildOption)
        {
            // Add the items to the trie builder in an interesting (not trivial, not random) order.
            int index, step;

            if ((dataLength & 1) != 0)
            {
                // Odd number of items.
                index = dataLength / 2;
                step  = 2;
            }
            else if ((dataLength % 3) != 0)
            {
                // Not a multiple of 3.
                index = dataLength / 5;
                step  = 3;
            }
            else
            {
                index = dataLength - 1;
                step  = -1;
            }
            builder_.Clear();
            for (int i = 0; i < dataLength; ++i)
            {
                builder_.Add(data[index].s, data[index].value);
                index = (index + step) % dataLength;
            }
            CharsTrie trie = builder_.Build(buildOption);

            try
            {
                builder_.Add("zzz", 999);
                Errln("builder.Build().Add(zzz) did not throw IllegalStateException");
            }
            catch (InvalidOperationException e)
            {
                // good
            }
            ICharSequence trieChars = builder_.BuildCharSequence(buildOption);

            Logln("serialized trie size: " + trieChars.Length + " chars");
            // Tries from either build() method should be identical but
            // CharsTrie does not implement equals().
            // We just return either one.
            if ((dataLength & 1) != 0)
            {
                return(trie);
            }
            else
            {
                return(new CharsTrie(trieChars, 0));
            }
        }
Exemple #4
0
        private void BuildChars(TrieBuilderOption buildOption)
        {
            // Create and char-serialize the trie for the elements.
            if (chars == null)
            {
                chars = new char[1024];
            }
#pragma warning disable 612, 618
            BuildImpl(buildOption);
#pragma warning restore 612, 618
        }
Exemple #5
0
        private void BuildBytes(TrieBuilderOption buildOption)
        {
            // Create and byte-serialize the trie for the elements.
            if (bytes == null)
            {
                bytes = new byte[1024];
            }
#pragma warning disable 612, 618
            BuildImpl(buildOption);
#pragma warning restore 612, 618
        }
Exemple #6
0
        internal void BuildImpl(TrieBuilderOption buildOption) // ICU4N specific - marked internal instead of protected, since the functionality is obsolete
        {
            switch (state)
            {
            case State.Adding:
                if (root == null)
                {
                    throw new IndexOutOfRangeException("No (string, value) pairs were added.");
                }
                if (buildOption == TrieBuilderOption.Fast)
                {
                    state = State.BuildingFast;
                    // Building "fast" is somewhat faster (25..50% in some test)
                    // because it makes registerNode() return the input node
                    // rather than checking for duplicates.
                    // As a result, we sometimes write larger trie serializations.
                    //
                    // In either case we need to fix-up linear-match nodes (for their maximum length)
                    // and branch nodes (turning dynamic branch nodes into trees of
                    // runtime-equivalent nodes), but the HashMap/hashCode()/equals() are omitted for
                    // nodes other than final values.
                }
                else
                {
                    state = State.BuildingSmall;
                }
                break;

            case State.BuildingFast:
            case State.BuildingSmall:
                // Building must have failed.
                throw new InvalidOperationException("Builder failed and must be clear()ed.");

            case State.Built:
                return;      // Nothing more to do.
            }
            // Implementation note:
            // We really build three versions of the trie.
            // The first is a fully dynamic trie, built successively by addImpl().
            // Then we call root.register() to turn it into a tree of nodes
            // which is 1:1 equivalent to the runtime data structure.
            // Finally, root.markRightEdgesFirst() and root.write() write that serialized form.
            root = root.Register(this);
            root.MarkRightEdgesFirst(-1);
            root.Write(this);
            state = State.Built;
        }
Exemple #7
0
 public BytesTrie buildMonthsTrie(TrieBuilderOption buildOption)
 {
     // All types of nodes leading to the same value,
     // for code coverage of recursive functions.
     // In particular, we need a lot of branches on some single level
     // to exercise a split-branch node.
     StringAndValue[] data =
     {
         new StringAndValue("august",                          8),
         new StringAndValue("jan",                             1),
         new StringAndValue("jan.",                            1),
         new StringAndValue("jana",                            1),
         new StringAndValue("janbb",                           1),
         new StringAndValue("janc",                            1),
         new StringAndValue("janddd",                          1),
         new StringAndValue("janee",                           1),
         new StringAndValue("janef",                           1),
         new StringAndValue("janf",                            1),
         new StringAndValue("jangg",                           1),
         new StringAndValue("janh",                            1),
         new StringAndValue("janiiii",                         1),
         new StringAndValue("janj",                            1),
         new StringAndValue("jankk",                           1),
         new StringAndValue("jankl",                           1),
         new StringAndValue("jankmm",                          1),
         new StringAndValue("janl",                            1),
         new StringAndValue("janm",                            1),
         new StringAndValue("jannnnnnnnnnnnnnnnnnnnnnnnnnnnn", 1),
         new StringAndValue("jano",                            1),
         new StringAndValue("janpp",                           1),
         new StringAndValue("janqqq",                          1),
         new StringAndValue("janr",                            1),
         new StringAndValue("januar",                          1),
         new StringAndValue("january",                         1),
         new StringAndValue("july",                            7),
         new StringAndValue("jun",                             6),
         new StringAndValue("jun.",                            6),
         new StringAndValue("june", 6)
     };
     return(buildTrie(data, data.Length, buildOption));
 }
Exemple #8
0
 /// <summary>
 /// Builds a <see cref="CharsTrie"/> for the <see cref="Add(string, int)"/>ed data and char-serializes it.
 /// Once built, no further data can be <see cref="Add(string, int)"/>ed until <see cref="Clear()"/> is called.
 /// </summary>
 /// <remarks>
 /// A <see cref="CharsTrie"/> cannot be empty. At least one (string, value) pair
 /// must have been <see cref="Add(string, int)"/>ed.
 /// <para/>
 /// Multiple calls to <see cref="Build(TrieBuilderOption)"/> or <see cref="BuildCharSequence(TrieBuilderOption)"/> return tries or sequences
 /// which share the builder's char array, without rebuilding.
 /// After <see cref="Clear()"/> has been called, a new array will be used.
 /// </remarks>
 /// <param name="buildOption">Build option, see <see cref="TrieBuilderOption"/>.</param>
 /// <returns>A <see cref="ICharSequence"/> with the char-serialized <see cref="CharsTrie"/> for the <see cref="Add(string, int)"/>ed data.</returns>
 /// <stable>ICU 4.8</stable>
 public ICharSequence BuildCharSequence(TrieBuilderOption buildOption)
 {
     BuildChars(buildOption);
     return(CharBuffer.Wrap(chars, chars.Length - charsLength, charsLength));
 }
Exemple #9
0
 /// <summary>
 /// Builds a <see cref="CharsTrie"/> for the <see cref="Add(string, int)"/>ed data.
 /// Once built, no further data can be <see cref="Add(string, int)"/>ed until <see cref="Clear()"/> is called.
 /// </summary>
 /// <remarks>
 /// A <see cref="CharsTrie"/> cannot be empty. At least one (string, value) pair
 /// must have been <see cref="Add(string, int)"/>ed.
 /// <para/>
 /// Multiple calls to <see cref="Build(TrieBuilderOption)"/> or <see cref="BuildCharSequence(TrieBuilderOption)"/> return tries or sequences
 /// which share the builder's char array, without rebuilding.
 /// After <see cref="Clear()"/> has been called, a new array will be used.
 /// </remarks>
 /// <param name="buildOption">Build option, see <see cref="TrieBuilderOption"/>.</param>
 /// <returns>A new <see cref="CharsTrie"/> for the <see cref="Add(string, int)"/>ed data.</returns>
 /// <stable>ICU 4.8</stable>
 public CharsTrie Build(TrieBuilderOption buildOption)
 {
     return(new CharsTrie(BuildCharSequence(buildOption), 0));
 }
Exemple #10
0
 /// <summary>
 /// Builds a <see cref="BytesTrie"/> for the <see cref="Add(byte[], int, int)"/> appended data.
 /// Once built, no further data can be added until <see cref="Clear()"/> is called.
 ///
 /// <para/>A <see cref="BytesTrie"/> cannot be empty. At least one (byte sequence, value) pair
 /// must have been added.
 ///
 /// <para/>Multiple calls to <see cref="Build(TrieBuilderOption)"/> or <see cref="BuildByteBuffer(TrieBuilderOption)"/> return tries or buffers
 /// which share the builder's byte array, without rebuilding.
 /// <em>The byte array must not be modified via the <see cref="BuildByteBuffer(TrieBuilderOption)"/> result object.</em>
 /// After <see cref="Clear()"/> has been called, a new array will be used.
 /// </summary>
 /// <param name="buildOption">Build option, see <see cref="TrieBuilderOption"/>.</param>
 /// <returns> A new <see cref="BytesTrie"/> for the added data.</returns>
 /// <stable>ICU 4.8</stable>
 public BytesTrie Build(TrieBuilderOption buildOption)
 {
     BuildBytes(buildOption);
     return(new BytesTrie(bytes, bytes.Length - bytesLength));
 }
Exemple #11
0
 /// <summary>
 /// Builds a <see cref="BytesTrie"/> for the <see cref="Add(byte[], int, int)"/> appended data and byte-serializes it.
 /// Once built, no further data can be added until <see cref="Clear()"/> is called.
 /// <para/>A <see cref="BytesTrie"/> cannot be empty. At least one (byte sequence, value) pair
 /// must have been added.
 ///
 /// <para/>Multiple calls to <see cref="Build(TrieBuilderOption)"/> or <see cref="BuildByteBuffer(TrieBuilderOption)"/> return tries or buffers
 /// which share the builder's byte array, without rebuilding.
 /// <em>Do not modify the bytes in the buffer!</em>
 /// After <see cref="Clear()"/> has been called, a new array will be used.
 ///
 /// <para/>The serialized <see cref="BytesTrie"/> is accessible via the buffer's
 /// <see cref="ByteBuffer.Array"/>/<see cref="ByteBuffer.ArrayOffset"/>+Position or Remaining/Get(byte[]) etc.
 /// </summary>
 /// <param name="buildOption">Build option, see <see cref="TrieBuilderOption"/>.</param>
 /// <returns>A <see cref="ByteBuffer"/> with the byte-serialized <see cref="BytesTrie"/> for the added data.
 /// The buffer is not read-only and <see cref="ByteBuffer.Array"/> can be called.</returns>
 /// <stable>ICU 4.8</stable>
 public ByteBuffer BuildByteBuffer(TrieBuilderOption buildOption)
 {
     BuildBytes(buildOption);
     return(ByteBuffer.Wrap(bytes, bytes.Length - bytesLength, bytesLength));
 }