Lucene++ - a full-featured, c++ search engine
API Documentation


Array.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
6 
7 #ifndef ARRAY_H
8 #define ARRAY_H
9 
10 #include <cstring>
11 #include "Lucene.h"
12 
13 namespace Lucene {
14 
15 template <typename TYPE>
16 class ArrayData {
17 public:
18  ArrayData(int32_t size_) {
19  data = NULL;
20  resize(size_);
21  }
22 
24  resize(0);
25  }
26 
27 public:
28  TYPE* data;
29  int32_t size;
30 
31 public:
32  void resize(int32_t size_) {
33  if (size_ == 0) {
35  data = NULL;
36  } else if (data == NULL) {
37  data = (TYPE*)AllocMemory(size_ * sizeof(TYPE));
38  } else {
39  data = (TYPE*)ReallocMemory(data, size_ * sizeof(TYPE));
40  }
41  this->size = size_;
42  }
43 };
44 
46 template <typename TYPE>
47 class Array {
48 public:
51 
52  Array() {
53  array = NULL;
54  }
55 
56 protected:
57  boost::shared_ptr<array_type> container;
59 
60 public:
61  static this_type newInstance(int32_t size) {
62  this_type instance;
63  instance.container = Lucene::newInstance<array_type>(size);
64  instance.array = instance.container.get();
65  return instance;
66  }
67 
68  void reset() {
69  resize(0);
70  }
71 
72  void resize(int32_t size) {
73  if (size == 0) {
74  container.reset();
75  } else if (!container) {
76  container = Lucene::newInstance<array_type>(size);
77  } else {
78  container->resize(size);
79  }
80  array = container.get();
81  }
82 
83  TYPE* get() const {
84  return array->data;
85  }
86 
87  int32_t size() const {
88  return array->size;
89  }
90 
91  bool equals(const this_type& other) const {
92  if (array->size != other.array->size) {
93  return false;
94  }
95  return (std::memcmp(array->data, other.array->data, array->size) == 0);
96  }
97 
98  int32_t hashCode() const {
99  return (int32_t)(int64_t)array;
100  }
101 
102  TYPE& operator[] (int32_t i) const {
103  BOOST_ASSERT(i >= 0 && i < array->size);
104  return array->data[i];
105  }
106 
107  operator bool () const {
108  return container.get() != NULL;
109  }
110 
111  bool operator! () const {
112  return !container;
113  }
114 
115  bool operator== (const Array<TYPE>& other) {
116  return (container == other.container);
117  }
118 
119  bool operator!= (const Array<TYPE>& other) {
120  return (container != other.container);
121  }
122 };
123 
124 template <class TYPE>
125 inline std::size_t hash_value(const Array<TYPE>& value) {
126  return (std::size_t)value.hashCode();
127 }
128 
129 template <class TYPE>
130 inline bool operator== (const Array<TYPE>& value1, const Array<TYPE>& value2) {
131  return (value1.hashCode() == value2.hashCode());
132 }
133 
134 }
135 
136 #endif
Definition: Array.h:16
ArrayData(int32_t size_)
Definition: Array.h:18
void resize(int32_t size_)
Definition: Array.h:32
~ArrayData()
Definition: Array.h:23
int32_t size
Definition: Array.h:29
TYPE * data
Definition: Array.h:28
Utility template class to handle sharable arrays of simple data types.
Definition: Array.h:47
bool operator!() const
Definition: Array.h:111
bool operator!=(const Array< TYPE > &other)
Definition: Array.h:119
ArrayData< TYPE > array_type
Definition: Array.h:50
void reset()
Definition: Array.h:68
Array()
Definition: Array.h:52
void resize(int32_t size)
Definition: Array.h:72
bool operator==(const Array< TYPE > &other)
Definition: Array.h:115
bool equals(const this_type &other) const
Definition: Array.h:91
int32_t size() const
Definition: Array.h:87
static this_type newInstance(int32_t size)
Definition: Array.h:61
array_type * array
Definition: Array.h:58
Array< TYPE > this_type
Definition: Array.h:49
boost::shared_ptr< array_type > container
Definition: Array.h:57
int32_t hashCode() const
Definition: Array.h:98
TYPE * get() const
Definition: Array.h:83
TYPE & operator[](int32_t i) const
Definition: Array.h:102
Definition: AbstractAllTermDocs.h:12
LPPAPI void * ReallocMemory(void *memory, size_t size)
Reallocate a given block of memory.
LPPAPI void * AllocMemory(size_t size)
Allocate block of memory.
std::size_t hash_value(const Array< TYPE > &value)
Definition: Array.h:125
LPPAPI void FreeMemory(void *memory)
Release a given block of memory.
bool operator==(const Array< TYPE > &value1, const Array< TYPE > &value2)
Definition: Array.h:130

clucene.sourceforge.net