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 #include "stri_stringi.h"
34 #include "stri_container_base.h"
35 
36 
37 /**
38  * Default constructor
39  *
40  */
StriContainerBase()41 StriContainerBase::StriContainerBase()
42 {
43     this->n = 0;
44     this->nrecycle = 0;
45     this->sexp = (SEXP)NULL;
46 #ifndef NDEBUG
47     this->isShallow = true;
48 #endif
49 }
50 
51 
52 /**
53  * Initialize object data
54  *
55  */
init_Base(R_len_t _n,R_len_t _nrecycle,bool _shallowrecycle,SEXP _sexp)56 void StriContainerBase::init_Base(R_len_t _n, R_len_t _nrecycle, bool _shallowrecycle, SEXP _sexp)
57 {
58 #ifndef NDEBUG
59     if (this->n != 0 || this->nrecycle != 0 || this->sexp != (SEXP)NULL)
60         throw StriException("StriContainerBase::init_Base(...): already initialized");
61     this->isShallow = _shallowrecycle;
62 #endif
63 
64     if (_n == 0 || _nrecycle == 0) {
65         this->nrecycle = 0;
66         this->n = 0;
67         this->sexp = _sexp;
68     }
69     else {
70         this->nrecycle = _nrecycle;
71         this->n = (_shallowrecycle)?_n:_nrecycle;
72         this->sexp = _sexp;
73 
74 #ifndef NDEBUG
75         if (this->n < _n)
76             throw StriException("StriContainerBase::init_Base(...): this->n < _n");
77         if (this->n > this->nrecycle)
78             throw StriException("StriContainerBase::init_Base(...): this->n > this->nrecycle");
79 #endif
80     }
81 }
82