]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/usetiter.cpp
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / common / usetiter.cpp
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
374ca955 3* Copyright (c) 2002-2003, International Business Machines
b75a7d8f
A
4* Corporation and others. All Rights Reserved.
5**********************************************************************
b75a7d8f
A
6*/
7#include "unicode/usetiter.h"
8#include "unicode/uniset.h"
9#include "unicode/unistr.h"
10#include "uvector.h"
11
12U_NAMESPACE_BEGIN
13
374ca955 14UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UnicodeSetIterator)
b75a7d8f
A
15
16/**
17 * Create an iterator
18 * @param set set to iterate over
19 */
20UnicodeSetIterator::UnicodeSetIterator(const UnicodeSet& uSet) {
21 reset(uSet);
22}
23
24/**
25 * Create an iterator. Convenience for when the contents are to be set later.
26 */
27UnicodeSetIterator::UnicodeSetIterator() {
28 this->set = NULL;
29 reset();
30}
31
32UnicodeSetIterator::~UnicodeSetIterator() {
33 // Nothing to do
34}
35
36/**
37 * Returns the next element in the set.
38 * @return true if there was another element in the set.
39 * if so, if codepoint == IS_STRING, the value is a string in the string field
40 * else the value is a single code point in the codepoint field.
41 * <br>You are guaranteed that the codepoints are in sorted order, and the strings are in sorted order,
42 * and that all code points are returned before any strings are returned.
43 * <br>Note also that the codepointEnd is undefined after calling this method.
44 */
45UBool UnicodeSetIterator::next() {
46 if (nextElement <= endElement) {
47 codepoint = codepointEnd = nextElement++;
48 return TRUE;
49 }
50 if (range < endRange) {
51 loadRange(++range);
52 codepoint = codepointEnd = nextElement++;
53 return TRUE;
54 }
55
56 if (nextString >= stringCount) return FALSE;
57 codepoint = (UChar32)IS_STRING; // signal that value is actually a string
58 string = (const UnicodeString*) set->strings->elementAt(nextString++);
59 return TRUE;
60}
61
62/**
63 * @return true if there was another element in the set.
64 * if so, if codepoint == IS_STRING, the value is a string in the string field
65 * else the value is a range of codepoints in the <codepoint, codepointEnd> fields.
66 * <br>Note that the codepoints are in sorted order, and the strings are in sorted order,
67 * and that all code points are returned before any strings are returned.
68 * <br>You are guaranteed that the ranges are in sorted order, and the strings are in sorted order,
69 * and that all ranges are returned before any strings are returned.
70 * <br>You are also guaranteed that ranges are disjoint and non-contiguous.
71 * <br>Note also that the codepointEnd is undefined after calling this method.
72 */
73UBool UnicodeSetIterator::nextRange() {
74 if (nextElement <= endElement) {
75 codepointEnd = endElement;
76 codepoint = nextElement;
77 nextElement = endElement+1;
78 return TRUE;
79 }
80 if (range < endRange) {
81 loadRange(++range);
82 codepointEnd = endElement;
83 codepoint = nextElement;
84 nextElement = endElement+1;
85 return TRUE;
86 }
87
88 if (nextString >= stringCount) return FALSE;
89 codepoint = (UChar32)IS_STRING; // signal that value is actually a string
90 string = (const UnicodeString*) set->strings->elementAt(nextString++);
91 return TRUE;
92}
93
94/**
95 *@param set the set to iterate over. This allows reuse of the iterator.
96 */
97void UnicodeSetIterator::reset(const UnicodeSet& uSet) {
98 this->set = &uSet;
99 reset();
100}
101
102/**
103 * Resets to the start, to allow the iteration to start over again.
104 */
105void UnicodeSetIterator::reset() {
106 if (set == NULL) {
107 // Set up indices to empty iteration
108 endRange = -1;
109 stringCount = 0;
110 } else {
111 endRange = set->getRangeCount() - 1;
112 stringCount = set->strings->size();
113 }
114 range = 0;
115 endElement = -1;
116 nextElement = 0;
117 if (endRange >= 0) {
118 loadRange(range);
119 }
120 nextString = 0;
121}
122
123void UnicodeSetIterator::loadRange(int32_t iRange) {
124 nextElement = set->getRangeStart(iRange);
125 endElement = set->getRangeEnd(iRange);
126}
127
128U_NAMESPACE_END
129
130//eof