intl/icu/source/common/usetiter.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/usetiter.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,150 @@
     1.4 +/*
     1.5 +**********************************************************************
     1.6 +* Copyright (c) 2002-2006, International Business Machines
     1.7 +* Corporation and others.  All Rights Reserved.
     1.8 +**********************************************************************
     1.9 +*/
    1.10 +#include "unicode/usetiter.h"
    1.11 +#include "unicode/uniset.h"
    1.12 +#include "unicode/unistr.h"
    1.13 +#include "uvector.h"
    1.14 +
    1.15 +U_NAMESPACE_BEGIN
    1.16 +
    1.17 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UnicodeSetIterator)
    1.18 +
    1.19 +/**
    1.20 + * Create an iterator
    1.21 + * @param set set to iterate over
    1.22 + */
    1.23 +UnicodeSetIterator::UnicodeSetIterator(const UnicodeSet& uSet) {
    1.24 +    cpString  = NULL;
    1.25 +    reset(uSet);
    1.26 +}
    1.27 +
    1.28 +/**
    1.29 + * Create an iterator. Convenience for when the contents are to be set later.
    1.30 + */
    1.31 +UnicodeSetIterator::UnicodeSetIterator() {
    1.32 +    this->set = NULL;
    1.33 +    cpString  = NULL;
    1.34 +    reset();
    1.35 +}
    1.36 +
    1.37 +UnicodeSetIterator::~UnicodeSetIterator() {
    1.38 +    delete cpString;
    1.39 +}
    1.40 +
    1.41 +/**
    1.42 + * Returns the next element in the set.
    1.43 + * @return true if there was another element in the set.
    1.44 + * if so, if codepoint == IS_STRING, the value is a string in the string field
    1.45 + * else the value is a single code point in the codepoint field.
    1.46 + * <br>You are guaranteed that the codepoints are in sorted order, and the strings are in sorted order,
    1.47 + * and that all code points are returned before any strings are returned.
    1.48 + * <br>Note also that the codepointEnd is undefined after calling this method.
    1.49 + */
    1.50 +UBool UnicodeSetIterator::next() {
    1.51 +    if (nextElement <= endElement) {
    1.52 +        codepoint = codepointEnd = nextElement++;
    1.53 +        string = NULL;
    1.54 +        return TRUE;
    1.55 +    }
    1.56 +    if (range < endRange) {
    1.57 +        loadRange(++range);
    1.58 +        codepoint = codepointEnd = nextElement++;
    1.59 +        string = NULL;
    1.60 +        return TRUE;
    1.61 +    }
    1.62 +
    1.63 +    if (nextString >= stringCount) return FALSE;
    1.64 +    codepoint = (UChar32)IS_STRING; // signal that value is actually a string
    1.65 +    string = (const UnicodeString*) set->strings->elementAt(nextString++);
    1.66 +    return TRUE;
    1.67 +}
    1.68 +
    1.69 +/**
    1.70 + * @return true if there was another element in the set.
    1.71 + * if so, if codepoint == IS_STRING, the value is a string in the string field
    1.72 + * else the value is a range of codepoints in the <codepoint, codepointEnd> fields.
    1.73 + * <br>Note that the codepoints are in sorted order, and the strings are in sorted order,
    1.74 + * and that all code points are returned before any strings are returned.
    1.75 + * <br>You are guaranteed that the ranges are in sorted order, and the strings are in sorted order,
    1.76 + * and that all ranges are returned before any strings are returned.
    1.77 + * <br>You are also guaranteed that ranges are disjoint and non-contiguous.
    1.78 + * <br>Note also that the codepointEnd is undefined after calling this method.
    1.79 + */
    1.80 +UBool UnicodeSetIterator::nextRange() {
    1.81 +    string = NULL;
    1.82 +    if (nextElement <= endElement) {
    1.83 +        codepointEnd = endElement;
    1.84 +        codepoint = nextElement;
    1.85 +        nextElement = endElement+1;
    1.86 +        return TRUE;
    1.87 +    }
    1.88 +    if (range < endRange) {
    1.89 +        loadRange(++range);
    1.90 +        codepointEnd = endElement;
    1.91 +        codepoint = nextElement;
    1.92 +        nextElement = endElement+1;
    1.93 +        return TRUE;
    1.94 +    }
    1.95 +
    1.96 +    if (nextString >= stringCount) return FALSE;
    1.97 +    codepoint = (UChar32)IS_STRING; // signal that value is actually a string
    1.98 +    string = (const UnicodeString*) set->strings->elementAt(nextString++);
    1.99 +    return TRUE;
   1.100 +}
   1.101 +
   1.102 +/**
   1.103 + *@param set the set to iterate over. This allows reuse of the iterator.
   1.104 + */
   1.105 +void UnicodeSetIterator::reset(const UnicodeSet& uSet) {
   1.106 +    this->set = &uSet;
   1.107 +    reset();
   1.108 +}
   1.109 +
   1.110 +/**
   1.111 + * Resets to the start, to allow the iteration to start over again.
   1.112 + */
   1.113 +void UnicodeSetIterator::reset() {
   1.114 +    if (set == NULL) {
   1.115 +        // Set up indices to empty iteration
   1.116 +        endRange = -1;
   1.117 +        stringCount = 0;
   1.118 +    } else {
   1.119 +        endRange = set->getRangeCount() - 1;
   1.120 +        stringCount = set->strings->size();
   1.121 +    }
   1.122 +    range = 0;
   1.123 +    endElement = -1;
   1.124 +    nextElement = 0;            
   1.125 +    if (endRange >= 0) {
   1.126 +        loadRange(range);
   1.127 +    }
   1.128 +    nextString = 0;
   1.129 +    string = NULL;
   1.130 +}
   1.131 +
   1.132 +void UnicodeSetIterator::loadRange(int32_t iRange) {
   1.133 +    nextElement = set->getRangeStart(iRange);
   1.134 +    endElement = set->getRangeEnd(iRange);
   1.135 +}
   1.136 +
   1.137 +
   1.138 +const UnicodeString& UnicodeSetIterator::getString()  {
   1.139 +    if (string==NULL && codepoint!=(UChar32)IS_STRING) {
   1.140 +       if (cpString == NULL) {
   1.141 +          cpString = new UnicodeString();
   1.142 +       }
   1.143 +       if (cpString != NULL) {
   1.144 +          cpString->setTo((UChar32)codepoint);
   1.145 +       }
   1.146 +       string = cpString;
   1.147 +    }
   1.148 +    return *string;
   1.149 +}
   1.150 +
   1.151 +U_NAMESPACE_END
   1.152 +
   1.153 +//eof

mercurial