1 /* This file is part of the 'stringi' project.
2  * Copyright (c) 2013-2021, Marek Gagolewski <https://www.gagolewski.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
21  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 
33 #ifndef __stri_container_base_h
34 #define __stri_container_base_h
35 
36 #include "stri_external.h"
37 #include "stri_exception.h"
38 
39 
40 
41 /**
42  * Base class for all StriContainers
43  *
44  * @version 0.1-?? (Marek Gagolewski)
45  *
46  * @version 0.1-?? (Marek Gagolewski)
47  *          removed ucnvNative, ucnvLatin1 (not needed per-object)
48  *
49  * @version 0.1-?? (Marek Gagolewski)
50  *          removed enc array
51  *
52  * @version 0.2-1 (Marek Gagolewski, 2014-03-22)
53  *          added sexp field
54  */
55 class StriContainerBase {
56 
57 protected:
58 
59     R_len_t n;                 ///< number of strings (size of \code{str})
60     R_len_t nrecycle;          ///< number of strings for the recycle rule (can be > \code{n})
61     SEXP sexp;                 ///<
62 
63 #ifndef NDEBUG
64     bool isShallow;            ///< have we made only shallow copy of the strings? (=> read only)
65 #endif
66 
67     StriContainerBase();
68     // StriContainerBase(StriContainerBase& container); // use default (shallow copy)
69     //~StriContainerBase(); // use default
70 
71     void init_Base(R_len_t n, R_len_t nrecycle, bool shallowrecycle, SEXP sexp=NULL);
72 
73 
74 public:
75     //StriContainerBase& operator=(StriContainerBase& container); // use default (shallow)
76 
get_n()77     inline R_len_t get_n() {
78         return n;
79     }
get_nrecycle()80     inline R_len_t get_nrecycle() {
81         return nrecycle;
82     }
set_nrecycle(R_len_t nval)83     inline void set_nrecycle(R_len_t nval) {
84         nrecycle = nval;
85     }
86 
87 
88     /** Loop over vectorized container - init */
vectorize_init()89     inline R_len_t vectorize_init() const {
90         if (n <= 0) return nrecycle;
91         else return 0;
92     }
93 
94     /** Loop over vectorized container - end iterator */
vectorize_end()95     inline R_len_t vectorize_end() const {
96         return nrecycle;
97     }
98 
99     /** Loop over vectorized container - next iteration */
vectorize_next(R_len_t i)100     inline R_len_t vectorize_next(R_len_t i) const {
101         if (i == nrecycle - 1 - (nrecycle%n))
102             return nrecycle; // this is the end
103         i = i + n;
104         if (i >= nrecycle)
105             return (i % n) + 1;
106         else
107             return i;
108     }
109 };
110 
111 #endif
112