summaryrefslogtreecommitdiffstats
path: root/src/xmlpatterns/type/qcardinality_p.h
blob: 6d64510757d60febd0bcb807c64b0067a1dd9e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtXmlPatterns module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.

#ifndef Patternist_Cardinality_H
#define Patternist_Cardinality_H

#include <QtCore/QtGlobal>
#include <QtCore/private/qglobal_p.h>

QT_BEGIN_NAMESPACE

class QString;

namespace QPatternist
{
    /**
     * @short Represents a cardinality, a possible , often represented by occurrence indicators.
     *
     * As opposed to the cardinality concept in the XQuery/XPath specifications, which
     * only allows cardinalities to be expressed with kleene operators, this representation
     * allows ranges. For example, the cardinality 10-11, describes a sequence containing
     * ten or eleven items, inclusive.
     *
     * @ingroup Patternist_types
     * @see ItemType
     * @see SequenceType
     * @see <a href="http://www.w3.org/TR/xpath20/#prod-xpath-SequenceType">XML Path Language
     * (XPath) 2.0, The EBNF grammar for SequenceType</a>
     * @author Frans Englich <frans.englich@nokia.com>
     */
    class Cardinality
    {
    public:
        /**
         * This integer type, is what Cardinality uses for representing its ranges.
         */
        typedef qint32 Count;

        /**
         * Used with displayName(), and specifies
         * how a display name for a Cardinality should be.
         */
        enum CustomizeDisplayName
        {
            /**
             * Includes a describing string in the return value of displayName().
             */
            IncludeExplanation  = 1,

            /**
             * Excludes a describing string in the return value of displayName().
             */
            ExcludeExplanation
        };

        /**
         * A traditional copy constructor. This Cardinality becomes identical
         * to @p other.
         */
        inline Cardinality(const Cardinality &other) : m_min(other.m_min),
                                                       m_max(other.m_max)
        {
        }

        /**
         * This default constructor constructs an invalid Cardinality. Using
         * its operators and members yields undefined results. A value must
         * first be assigned to it by creating a Cardinality with fromRange(), fromCount(),
         * or one of the predefined cardinalities such as empty() or oneOrMore().
         */
        inline Cardinality() : m_min(-1), m_max(0)
        {
        }

        /**
         * The cardinality assigned to the exprssion <tt>()</tt>, formally speaking. The
         * cardinality part of <tt>empty-sequence()</tt>.
         */
        static inline Cardinality empty()
        {
            return Cardinality(0, 0);
        }

        /**
         * The cardinality implicitly specified in for example the sequence type
         * <tt>item()</tt>. It has no kleene operator.
         */
        static inline Cardinality exactlyOne()
        {
            return Cardinality(1, 1);
        }

        /**
         * Allows both no item, as in empty(), and exactlyOne(). Represented
         * by the kleene operator <tt>?</tt>.
         */
        static inline Cardinality zeroOrOne()
        {
            return Cardinality(0, 1);
        }

        /**
         * Allows any amount. This is therefore the widest, an unconstrained
         * cardinality. Represented by the kleene operator <tt>*</tt>.
         */
        static inline Cardinality zeroOrMore()
        {
            return Cardinality(0, -1);
        }

        /**
         * Allows one or more. Represented by the kleene operator <tt>+</tt>.
         */
        static inline Cardinality oneOrMore()
        {
            return Cardinality(1, -1);
        }

        /**
         * Allows one or more. This cardinality has no kleene operator and is used
         * by the implementation in order to be able to know when a cardinality
         * that at amximum allows one, is exceeded.
         */
        static inline Cardinality twoOrMore()
        {
            return Cardinality(2, -1);
        }

       /**
         * Determines the cardinality from the count of a sequence. For example, if
         * @p count is 11, a Cardinality is returned that allows at minimum and maximum
         * 11 items.
         *
         * @p count must be positive or zero. If it is not, the result is undefined.
         * When debugging is enabled, a Q_ASSERT() macro ensures this.
         */
        static inline Cardinality fromCount(const Count count)
        {
            Q_ASSERT_X(count > -1, Q_FUNC_INFO,
                       "A count smaller than 0 makes no sense.");
            return Cardinality(count, count);
        }

        /**
         * Creates a Cardinality that allows @p minimum and @p maximum
         * items, inclusive.
         *
         * If @p maximum is -1, it signals infinity.
         *
         * If you before hand knows that a predefined Cardinality is needed,
         * remember to use one of the factory functions empty(), zeroOrOne(),
         * exactlyOne(), oneOrMore() or zeroOrMore(), since they improves
         * readability, are safer, and slightly faster.
         */
        static inline Cardinality fromRange(const Count minimum, const Count maximum)
        {
            Q_ASSERT_X(minimum > -1, Q_FUNC_INFO,
                       "minimum should never be less than 0.");
            Q_ASSERT_X(minimum <= maximum || maximum == -1, Q_FUNC_INFO,
                       "minimum cannot be larger than maximum.");

            return Cardinality(minimum, maximum);
        }

        static inline Cardinality fromExact(const Count count)
        {
            Q_ASSERT(count >= 0);
            return Cardinality(count, count);
        }

        /**
         * @returns the minimum amount of items this Cardinality allows. For example,
         * for zeroOrOne() is 0 returned.
         */
        inline Count minimum() const
        {
            Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality are invalid.");
            return m_min;
        }

        /**
         * @returns the maximum amount of items this Cardinality allows. For example,
         * for zeroOrOne() is 1 returned.
         */
        inline Count maximum() const
        {
            Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality are invalid.");
            return m_max;
        }

        /**
         * @returns @c true if this Cardinality allows one or more items. For example, for
         * zeroOrOne() is @c false returned, while for zeroOrMore() is @c true returned.
         */
        inline bool allowsMany() const
        {
            Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality are invalid.");
            return m_max == -1 || m_max > 1;
        }

        /**
         * @returns @c true if this Cardinality allows no items. For example, for
         * zeroOrOne() is @c true returned, while for oneOrMore() is @c false returned.
         */
        inline bool allowsEmpty() const
        {
            Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality are invalid.");
            return m_min == 0;
        }

        /**
         * Maps directly to Formal Semantics' @c aggregate_quantifier function.
         *
         * @returns zeroOrOne() if this Cardinality allows the empty sequence, otherwise exactlyOne()
         * @see <a href="http://www.w3.org/TR/xquery-semantics/#jd_quantifier">XQuery 1.0 and
         * XPath 2.0 Formal Semantics, The function quantifier()</a>
         */
        inline Cardinality toWithoutMany() const
        {
            return m_min == 0 ? Cardinality(0, 1)
                              : Cardinality(1, 1);
        }

        /**
         * Determines whether all the possible outcomes represented by @p other,
         * will always match this Cardinality. For example, if this Cardinality
         * is oneOrMore(), @c true will be returned if @p other is exactlyOne(), but
         * false if @p other is zeroOrOne().
         */
        inline bool isMatch(const Cardinality &other) const
        {
            Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");
            if(other.m_min < m_min)
                return false;
            else
            { /* Ok, we now know the minimum will always be ok. */
                if(m_max == -1)
                    return true; /* We allow infinite, so anything can match. */
                else if(other.m_max == -1)
                    return false; /* other allows infinity, while we don't. */
                else
                   return m_max >= other.m_max;
            }
        }

        /**
         * Determines whether at least one of the possible outcomes represented by @p other,
         * can match this Cardinality. For example, if this Cardinality
         * is oneOrMore(), @c true will be returned if @p other is exactlyOne() or zeroOrOne().
         */
        inline bool canMatch(const Cardinality &other) const
        {
            Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");
            if(m_max == -1)
                return m_min <= other.m_min || other.m_max >= m_min || other.m_max == -1;
            else
            {
                if(m_max == other.m_min)
                    return true;
                else if(m_max > other.m_min)
                    return other.m_max >= m_min || other.m_max == -1;
                else /* m_max < other.m_min */
                    return false;
            }
        }

        /**
         * @returns @c true if this Cardinality is empty, the <tt>empty-sequence()</tt>, otherwise
         * @c false.
         */
        inline bool isEmpty() const
        {
            Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality is invalid.");
            return m_min == 0 && m_max == 0;
        }

        /**
         * @returns @c true if this Cardinality is zero-or-one, <tt>?</tt>, otherwise
         * @c false.
         */
        inline bool isZeroOrOne() const
        {
            Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality is invalid.");
            return m_min == 0 && m_max == 1;
        }

        /**
         * @returns @c true if this Cardinality only allows exactly one item, otherwise
         * @c false.
         */
        inline bool isExactlyOne() const
        {
            Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality is invalid.");
            return m_min == 1 && m_max == 1;
        }

        /**
         * @returns @c true if this Cardinality only allows one or more items, otherwise
         * @c false.
         */
        inline bool isOneOrMore() const
        {
            Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality is invalid.");
            return m_min > 0 && (m_max == -1 || m_max >= 1);
        }

        /**
         * Determines whether this Cardinality only allows a specific length. For example,
         * empty() and exactlyOne() are exact, but oneOrMore() or zeroOrOne() is not.
         */
        inline bool isExact() const
        {
            Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality is invalid.");
            return m_min == m_max;
        }

        /**
         * Returns a string representation of this Cardinality.
         *
         * If @p explain is ExcludeExplanation the kleene operator is returned. For example, if
         * the Cardinality is zeroOrOne, is "?" returned.
         *
         * If explain is IncludeExplanation a string more suited for human interpretation is returned,
         * which is appropriately translated. For example, when the locale is English and
         * this Cardinality being zeroOrOne, then is 'zero or one("?")' returned.
         *
         * Typically, passing ExcludeExplanation is useful when generating function
         * signatures and the like, while passing IncludeExplanation
         * is suitable appropriate when generating error messages.
         *
         * @returns a string representation for this Cardinality.
         */
        QString displayName(const CustomizeDisplayName explanation) const;

        /**
         * Computes the Cardinality that comprises this Cardinality as well as @p other. For
         * example, if this Cardinality is zeroOrOne() and @p other is oneOrMore(), then
         * is zeroOrMore() returned.
         */
        inline Cardinality operator|(const Cardinality &other) const
        {
            Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");
            if(m_max == -1 || other.m_max == -1)
                return Cardinality(qMin(m_min, other.m_min), -1);
            else
                return Cardinality(qMin(m_min, other.m_min), qMax(m_max, other.m_max));
        }

        /**
         * Behaves as operator|() but assigns the result to this Cardinality.
         */
        inline Cardinality &operator|=(const Cardinality &other)
        {
            Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");
            m_min = qMin(m_min, other.m_min);

            if(m_max == -1)
                return *this;
            else if(other.m_max == -1)
                m_max = -1;
            else
                m_max = qMax(m_max, other.m_max);

            return *this;
        }

        /**
         * Computes the intersection of this Cardinality and @p other, and returns
         * the result. For example, the intersection between zeroOrOne() and
         * oneOrMore() is exactlyOne().
         *
         * If no intersection exists, such as the case in empty() and exactlyOne(), then
         * is a default constructed Cardinality is returned. That is, an invalid Cardinality.
         */
        inline Cardinality operator&(const Cardinality &other) const
        {
            Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");

            if(m_max < other.m_min) /* No intersection. */
                return empty();

            const Count min = qMax(m_min, other.m_min);

            if(m_max == -1)
                return Cardinality(min, other.m_max);
            else if(other.m_max == -1)
                return Cardinality(min, m_max);
            else
                return Cardinality(min, qMin(m_max, other.m_max));
        }

        /**
         * Adds two cardinalities, as if two sequences represented by them were concatenated.
         * For example, if this Cardinality allows the range 6-8 and @p other allows
         * 0-1, the return Cardinality has a range of 6-9.
         *
         * @returns the result of the comparison.
         */
        inline Cardinality operator+(const Cardinality &other) const
        {
            Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");
            if(m_max == -1 || other.m_max == -1)
                return Cardinality(m_min + other.m_min, -1);
            else
                return Cardinality(m_min + other.m_min, m_max + other.m_max);
        }

        /**
         * Behaves as operator+() but assigns the result to this Cardinality.
         */
        inline Cardinality &operator+=(const Cardinality &other)
        {
            Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO,
                       "One of the cardinalities are invalid.");
            m_min += other.m_min;

            if(m_max == -1)
                return *this;
            if(other.m_max == -1)
                m_max = -1;
            else
                m_max += other.m_max;

            return *this;
        }

        /**
         * Multiplies this Cardinality with @p other, and returns the result. The minimum and maximum
         * of each Cardinality is multiplied such that the new Cardinality represents the possible
         * range of the two sequences being multiplied, length-wise. For example the Cardinality
         * 4, 5 multiplied with 2, 3 becomes 8, 15.
         */
        inline Cardinality operator*(const Cardinality &other) const
        {
            Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO,
                       "One of the cardinalities are invalid.");
            if(m_max == -1 || other.m_max == -1)
                return Cardinality(m_min * other.m_min, -1);
            else
                return Cardinality(m_min * other.m_min, m_max * other.m_max);
        }

        /**
         * A traditional assignment operator. Behaves as assignment
         * operators typically do.
         */
        inline Cardinality &operator=(const Cardinality &other)
        {
            Q_ASSERT_X(this != &other, Q_FUNC_INFO, "Assigning to oneself makes no sense.");
            m_min = other.m_min;
            m_max = other.m_max;
            return *this;
        }

        /**
         * Determines whether @p other is equal to this Cardinality.
         *
         * For example, empty() is equal to empty(), but zeroOrOne()
         * is not equal to exactlyOne().
         *
         * @returns @c true if @p other is equal to this Cardinality.
         */
        inline bool operator==(const Cardinality &other) const
        {
            return m_min == other.m_min &&
                   m_max == other.m_max;
        }

        /**
         * @returns the opposite of operator==()
         */
        inline bool operator!=(const Cardinality &other) const
        {
            return m_min != other.m_min ||
                   m_max != other.m_max;
        }

    private:
        inline Cardinality(const Count min, const Count max) : m_min(min),
                                                               m_max(max)
        {
        }

        Count m_min;
        Count m_max;
    };
}

Q_DECLARE_TYPEINFO(QPatternist::Cardinality, Q_MOVABLE_TYPE);

QT_END_NAMESPACE

#endif