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