OpenCurves  0.9
stringitems.h
1 //
2 // author Kazys Stepanas
3 //
4 // Copyright (c) CSIRO 2015
5 //
6 #ifndef STRINGITEMS_H
7 #define STRINGITEMS_H
8 
9 #include "ocurvesconfig.h"
10 
11 #include <cstddef>
12 #include <cstdint>
13 #include <cstring>
14 
20 template <typename CHAR>
22 {
23 public:
25  StringItems();
27  ~StringItems();
28 
31  inline size_t size() const { return _count; }
32 
35  inline size_t count() const { return _count; }
36 
39  inline size_t capacity() const { return _capacity; }
40 
45  const CHAR *operator[](size_t index) const { return _items[index]; }
47  const CHAR *operator[](int index) const { return _items[index]; }
49  const CHAR *operator[](int64_t index) const { return _items[index]; }
50 
52  inline void reset() { _count = 0; }
53 
57  void resize(size_t size);
58 
64  void reserve(size_t capacity);
65 
69  void push_back(const CHAR *item);
70 
71 private:
72  const CHAR **_items;
73  size_t _count;
74  size_t _capacity;
75 };
76 
77 
78 template <typename CHAR>
80  : _items(nullptr)
81  , _count(0)
82  , _capacity(0)
83 {
84 }
85 
86 
87 template <typename CHAR>
89 {
90  delete[] _items;
91 }
92 
93 
94 template <typename CHAR>
95 void StringItems<CHAR>::resize(size_t size)
96 {
97  reserve(size);
98  _count = size;
99 }
100 
101 
102 template <typename CHAR>
103 void StringItems<CHAR>::reserve(size_t capacity)
104 {
105  if (_capacity < capacity)
106  {
107  // Reallocate.
108  const CHAR **newItems = new const CHAR *[capacity];
109  if (_count)
110  {
111  memcpy(newItems, _items, sizeof(*newItems) * _count);
112  }
113  delete[] _items;
114  _items = newItems;
115  _capacity = capacity;
116  }
117 }
118 
119 
120 template <typename CHAR>
121 void StringItems<CHAR>::push_back(const CHAR *item)
122 {
123  if (_count == _capacity)
124  {
125  reserve((_capacity) ? _capacity << 1 : 8);
126  }
127  _items[_count++] = item;
128 }
129 
130 
131 #endif // STRINGITEMS_H
size_t capacity() const
Return the current capacity of the array.
Definition: stringitems.h:39
StringItems()
Constructor.
Definition: stringitems.h:79
size_t count() const
Return the number of items in the array.
Definition: stringitems.h:35
const CHAR * operator[](int index) const
Definition: stringitems.h:47
void resize(size_t size)
Resizes such that the count matches size.
Definition: stringitems.h:95
void reserve(size_t capacity)
Ensures that the capacity is at least equal to capacity.
Definition: stringitems.h:103
const CHAR * operator[](int64_t index) const
Definition: stringitems.h:49
const CHAR * operator[](size_t index) const
Array accessor.
Definition: stringitems.h:45
void reset()
Resets the number of items to zero, leaving the capacity unchanged.
Definition: stringitems.h:52
An array of string pointers (into another array) using the given character type.
Definition: stringitems.h:21
size_t size() const
Return the number of items in the array.
Definition: stringitems.h:31
~StringItems()
Destructor.
Definition: stringitems.h:88
void push_back(const CHAR *item)
Adds an item to the array, incrementing the count and adjusting the capacity if required.
Definition: stringitems.h:121