Zero  0.1.0
vec_t.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-MT -- Multi-threaded port of the SHORE storage manager
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
24 /*<std-header orig-src='shore' incl-file-exclusion='VEC_T_H'>
25 
26  $Id: vec_t.h,v 1.67 2010/12/08 17:37:34 nhall Exp $
27 
28 SHORE -- Scalable Heterogeneous Object REpository
29 
30 Copyright (c) 1994-99 Computer Sciences Department, University of
31  Wisconsin -- Madison
32 All Rights Reserved.
33 
34 Permission to use, copy, modify and distribute this software and its
35 documentation is hereby granted, provided that both the copyright
36 notice and this permission notice appear in all copies of the
37 software, derivative works or modified versions, and any portions
38 thereof, and that both notices appear in supporting documentation.
39 
40 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
41 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
42 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
43 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
44 
45 This software was developed with support by the Advanced Research
46 Project Agency, ARPA order number 018 (formerly 8230), monitored by
47 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
48 Further funding for this work was provided by DARPA through
49 Rome Research Laboratory Contract No. F30602-97-2-0247.
50 
51 */
52 
53 #ifndef __VEC_T_H
54 #define __VEC_T_H
55 
56 #include "w_defines.h"
57 
58 /* -- do not edit anything above this line -- </std-header>*/
59 
60 /* NB: you must already have defined the type size_t,
61  * (which is defined include "basics.h") before you include this.
62  */
63 
64 typedef const unsigned char* CADDR_T;
65 
66 #define MAX_SMALL_VEC_SIZE 8
67 
68 class w_keystr_t;
69 /*
70  * Newer c++ compilers require
71  * that copy constructors be available for classes which use anonymous
72  * temporary variables. However, vec_t are non-copyable, so you
73  * must create named temporaries of these structures.
74  */
75 
78 struct vec_pair_t {
80  size_t len;
81 };
82 
85 struct VEC_t {
86  int _cnt;
87  size_t _size;
88  vec_pair_t* _base; // pointer to beginning of _pair or malloced
89  // space
91 };
92 
95 class cvec_t : protected VEC_t {
96  friend class vec_t; // so vec_t can look at VEC_t
97 protected:
98  static CADDR_T zero_location; // see zvec_t, which is supposed
99  // to be for the server-side only
100 public:
103  };
104 public:
105  cvec_t() {
106  _cnt = 0;
107  _size = 0;
108  _base = &_pair[0];
109  }
110 
111  cvec_t(const cvec_t& v1, const cvec_t& v2) {
112  _base = &_pair[0];
113  set(v1, v2);
114  }
115 
116  cvec_t(const void* p, size_t l) {
117  _base = &_pair[0];
118  set(p, l);
119  }
120 
121  cvec_t(const cvec_t& v, size_t offset, size_t limit) {
122  _base = &_pair[0];
123  set(v, offset, limit);
124  }
125 
126  ~cvec_t();
127 
128  void split(size_t l1, cvec_t& v1, cvec_t& v2) const;
129 
132  cvec_t& put(const cvec_t& v, size_t offset, size_t nbytes);
133 
135  cvec_t& put(const void* p, size_t l);
136 
138  cvec_t& put(const cvec_t& v);
139 
141  cvec_t& put(const w_keystr_t& keystr);
142 
144  cvec_t& put(const w_keystr_t& keystr, size_t offset, size_t nbytes);
145 
148  _cnt = _size = 0;
149  return *this;
150  }
151 
153  cvec_t& set(const cvec_t& v1, const cvec_t& v2) {
154  return reset().put(v1).put(v2);
155  }
156 
158  cvec_t& set(const cvec_t& v) {
159  return reset().put(v);
160  }
161 
163  cvec_t& set(const void* p, size_t l) {
164  return reset().put(p, l);
165  }
166 
169  cvec_t& set(const cvec_t& v, size_t offset, size_t limit) {
170  return reset().put(v, offset, limit);
171  }
172 
174  size_t size() const {
175  return _size;
176  }
177 
179  size_t copy_to(void* p, size_t limit = 0x7fffffff) const;
180 
182  size_t copy_to(std::basic_string<unsigned char>& buffer, size_t limit = 0x7fffffff) const;
183 
189  int cmp(const cvec_t& v, size_t* common_size = 0) const;
190 
191  int cmp(const void* s, size_t len) const;
192 
193  static int cmp(const cvec_t& v1,
194  const cvec_t& v2, size_t* common_size = 0) {
195  return v1.cmp(v2, common_size);
196  }
197 
199  int count() const {
200  return _cnt;
201  }
202 
203  int checksum() const;
204 
205  void calc_kvl(uint32_t& h) const;
206 
207  void init() {
208  _cnt = _size = 0;
209  } // re-initialize the vector
210  // Creator of the vec has responsibility for delete[]ing anything that
211  // was dynamically allocated in the array. These are convenience methods
212  // for holders of vec_ts that dynamically allocated all parts and want
213  // them delete[]-ed.
214  // vecdelparts() calls delete[] on all parts.
215  // delparts() calls delete on all parts.
216  // Both leave the vector re-initialized (0 parts)
217  void vecdelparts() {
218  while (_cnt-- > 0) {
219  delete[] _base[_cnt].ptr;
220  _base[_cnt].ptr = nullptr;
221  _base[_cnt].len = 0;
222  }
223  init();
224  }
225 
226  void delparts() {
227  while (_cnt-- > 0) {
228  delete _base[_cnt].ptr;
229  _base[_cnt].ptr = nullptr;
230  _base[_cnt].len = 0;
231  }
232  init();
233  }
234 
235  bool is_pos_inf() const {
236  return this == &pos_inf;
237  }
238 
239  bool is_neg_inf() const {
240  return this == &neg_inf;
241  }
242 
243  bool is_null() const {
244  return size() == 0;
245  }
246 
247  friend inline bool operator<(const cvec_t& v1, const cvec_t& v2);
248 
249  friend inline bool operator<=(const cvec_t& v1, const cvec_t& v2);
250 
251  friend inline bool operator>=(const cvec_t& v1, const cvec_t& v2);
252 
253  friend inline bool operator>(const cvec_t& v1, const cvec_t& v2);
254 
255  friend inline bool operator==(const cvec_t& v1, const cvec_t& v2);
256 
257  friend inline bool operator!=(const cvec_t& v1, const cvec_t& v2);
258 
259  friend ostream& operator<<(ostream&, const cvec_t& v);
260 
261  friend istream& operator>>(istream&, cvec_t& v);
262 
263  static cvec_t pos_inf;
264 
265  static cvec_t neg_inf;
266 
267 private:
268  // disabled
269  cvec_t(const cvec_t& v);
270 
271  // determine if this is a large vector (one where extra space
272  // had to be malloc'd
273  bool _is_large() const {
274  return _base != &_pair[0];
275  }
276 
277  // determine max number of elements in the vector
278  int _max_cnt() const {
279  return (int)(_is_large() ? _pair[0].len : (int)max_small);
280  }
281 
282  // grow vector to have total_cnt elements
283  void _grow(int total_cnt);
284 
285  // disabled
286  // cvec_t(const cvec_t& v);
288 
289  size_t recalc_size() const;
290 
291  bool check_size() const;
292 
293 public:
294  bool is_zvec() const {
295 #if W_DEBUG_LEVEL > 2
296  if(count()>0) {
297  if(_pair[0].ptr == zero_location) {
298  w_assert3(count() == 1);
299  }
300  }
301 #endif
302  return (count() == 0)
303  ||
304  (count() == 1 && _pair[0].ptr == zero_location);
305  }
306 };
307 
313 class vec_t : public cvec_t {
314 public:
316  vec_t() : cvec_t() {};
317 
319  vec_t(const cvec_t& v1, const cvec_t& v2) : cvec_t(v1, v2) {};
320 
322  vec_t(const void* p, size_t l) : cvec_t(p, l) {};
323 
325  vec_t(const vec_t& v, size_t offset, size_t limit)
326  : cvec_t(v, offset, limit) {};
327 
333  const vec_t& copy_from(
334  const void* p,
335  size_t limit,
336  size_t offset = 0) const; // offset tells where
337  //in the vec to begin to copy
338 
344  vec_t& copy_from(const cvec_t& v);
345 
352  vec_t& copy_from(
353  const cvec_t& v,
354  size_t offset, // offset in v
355  size_t limit, // # bytes
356  size_t myoffset = 0); // offset in this
357 
359  CADDR_T ptr(int index) const {
360  return (index >= 0 && index < _cnt) ?
361  _base[index].ptr : (CADDR_T)nullptr;
362  }
363 
365  size_t len(int index) const {
366  return (index >= 0 && index < _cnt) ?
367  _base[index].len : 0;
368  }
369 
371  // chunks.
373  void mkchunk(int maxsize, // max size of result vec
374  int skip, // # skipped in *this
375  vec_t& result // provided by the caller
376  ) const;
379  static vec_t& pos_inf;
381 
383  static vec_t& neg_inf;
384 
385 private:
386  // disabled
387  vec_t(const vec_t&) : cvec_t() {
388  cerr << "vec_t: disabled member called" << endl;
389  cerr << "failed at \"" << __FILE__ << ":" << __LINE__ << "\"" << endl;
390  W_FATAL (fcINTERNAL);
391  }
392 
393 private:
394  // disabled
395  vec_t& operator=(vec_t);
396 };
397 
398 inline bool operator<(const cvec_t& v1, const cvec_t& v2) {
399  return v1.cmp(v2) < 0;
400 }
401 
402 inline bool operator<=(const cvec_t& v1, const cvec_t& v2) {
403  return v1.cmp(v2) <= 0;
404 }
405 
406 inline bool operator>=(const cvec_t& v1, const cvec_t& v2) {
407  return v1.cmp(v2) >= 0;
408 }
409 
410 inline bool operator>(const cvec_t& v1, const cvec_t& v2) {
411  return v1.cmp(v2) > 0;
412 }
413 
414 inline bool operator==(const cvec_t& v1, const cvec_t& v2) {
415  return (&v1 == &v2) || v1.cmp(v2) == 0;
416 }
417 
418 inline bool operator!=(const cvec_t& v1, const cvec_t& v2) {
419  return !(v1 == v2);
420 }
421 
422 
423 /*<std-footer incl-file-exclusion='VEC_T_H'> -- do not edit anything below this line -- */
424 
425 #endif // __VEC_T_H /*</std-footer>*/
int cmp(const cvec_t &v, size_t *common_size=0) const
int count() const
return number of {p,l} pairs
Definition: vec_t.h:199
bool _is_large() const
Definition: vec_t.h:273
A helper class for VEC_t.
Definition: vec_t.h:78
cvec_t & put(const cvec_t &v, size_t offset, size_t nbytes)
bool is_zvec() const
Definition: vec_t.h:294
cvec_t(const cvec_t &v1, const cvec_t &v2)
Definition: vec_t.h:111
bool is_null() const
Definition: vec_t.h:243
static int cmp(const cvec_t &v1, const cvec_t &v2, size_t *common_size=0)
Definition: vec_t.h:193
void delparts()
Definition: vec_t.h:226
static CADDR_T zero_location
Definition: vec_t.h:98
void vecdelparts()
Definition: vec_t.h:217
cvec_t & reset()
Clear this vector.
Definition: vec_t.h:147
bool check_size() const
bool operator<=(const cvec_t &v1, const cvec_t &v2)
Definition: vec_t.h:402
cvec_t(const cvec_t &v, size_t offset, size_t limit)
Definition: vec_t.h:121
#define MAX_SMALL_VEC_SIZE
Definition: vec_t.h:66
static cvec_t neg_inf
Definition: vec_t.h:265
Definition: vec_t.h:102
vec_pair_t _pair[MAX_SMALL_VEC_SIZE]
Definition: vec_t.h:90
cvec_t()
Definition: vec_t.h:105
static cvec_t pos_inf
Definition: vec_t.h:263
#define w_assert3(x)
Level 3 definitely adds significant time.
Definition: w_base.h:214
std::istream & operator>>(std::istream &is, ConfigFile &cf)
Definition: confparser.cpp:94
dummy_enumid
Definition: vec_t.h:101
Key string class which can represent a few special values.
Definition: w_key.h:47
bool is_neg_inf() const
Definition: vec_t.h:239
A constant vec_t (meaning things pointed to cannot be changed).
Definition: vec_t.h:95
vec_t & operator=(vec_t)
size_t copy_to(void *p, size_t limit=0x7fffffff) const
bool operator==(const cvec_t &v1, const cvec_t &v2)
Definition: vec_t.h:414
vec_t(const vec_t &v, size_t offset, size_t limit)
Construct a vector from a memory location + offset and a length.
Definition: vec_t.h:325
A base class for vec_t.
Definition: vec_t.h:85
void split(size_t l1, cvec_t &v1, cvec_t &v2) const
bool operator!=(const cvec_t &v1, const cvec_t &v2)
Definition: vec_t.h:418
void init()
Definition: vec_t.h:207
bool is_pos_inf() const
Definition: vec_t.h:235
size_t size() const
returns # bytes this vector references
Definition: vec_t.h:174
vec_t(const cvec_t &v1, const cvec_t &v2)
Construct a vector that combines two others.
Definition: vec_t.h:319
size_t recalc_size() const
std::ostream & operator<<(std::ostream &os, const ConfigFile &cf)
Definition: confparser.cpp:83
CADDR_T ptr
Definition: vec_t.h:79
bool operator>=(const cvec_t &v1, const cvec_t &v2)
Definition: vec_t.h:406
bool operator>(const cvec_t &v1, const cvec_t &v2)
Definition: vec_t.h:410
bool operator<(const cvec_t &v1, const cvec_t &v2)
Definition: vec_t.h:398
vec_pair_t * _base
Definition: vec_t.h:88
void _grow(int total_cnt)
static vec_t & pos_inf
A constant vector representing infinity. Used for key-value pairs, scans.
Definition: vec_t.h:380
Vector: a set of {pointer,length} pairs for memory manipulation.
Definition: vec_t.h:313
size_t _size
Definition: vec_t.h:87
size_t len
Definition: vec_t.h:80
int _max_cnt() const
Definition: vec_t.h:278
vec_t(const vec_t &)
Definition: vec_t.h:387
int checksum() const
static vec_t & neg_inf
A constant vector representing negative infinity. Used for key-value pairs, scans.
Definition: vec_t.h:383
vec_t(const void *p, size_t l)
Construct a vector from a memory location and a length.
Definition: vec_t.h:322
void calc_kvl(uint32_t &h) const
cvec_t(const void *p, size_t l)
Definition: vec_t.h:116
size_t len(int index) const
Return the length from the {pointer, length} pair at the given index.
Definition: vec_t.h:365
CADDR_T ptr(int index) const
Return the pointer from the {pointer, length} pair at the given index.
Definition: vec_t.h:359
#define W_FATAL(e)
Croak with the error code e.
Definition: w_rc.h:378
vec_t()
Construct empty vector.
Definition: vec_t.h:316
int _cnt
Definition: vec_t.h:86
const unsigned char * CADDR_T
Definition: vec_t.h:64