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