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

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Array.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2011 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 {
18 public:
19 ArrayData(int32_t size)
20 {
21 data = NULL;
22 resize(size);
23 }
24
25 ~ArrayData()
26 {
27 resize(0);
28 }
29
30 public:
31 TYPE* data;
32 int32_t size;
33
34 public:
35 void resize(int32_t size)
36 {
37 if (size == 0)
38 {
39 FreeMemory(data);
40 data = NULL;
41 }
42 else if (data == NULL)
43 data = (TYPE*)AllocMemory(size * sizeof(TYPE));
44 else
45 data = (TYPE*)ReallocMemory(data, size * sizeof(TYPE));
46 this->size = size;
47 }
48 };
49
51 template <typename TYPE>
52 class Array
53 {
54 public:
55 typedef Array<TYPE> this_type;
56 typedef ArrayData<TYPE> array_type;
57
58 Array()
59 {
60 array = NULL;
61 }
62
63 protected:
64 boost::shared_ptr<array_type> container;
65 array_type* array;
66
67 public:
68 static this_type newInstance(int32_t size)
69 {
70 this_type instance;
71 instance.container = Lucene::newInstance<array_type>(size);
72 instance.array = instance.container.get();
73 return instance;
74 }
75
76 void reset()
77 {
78 resize(0);
79 }
80
81 void resize(int32_t size)
82 {
83 if (size == 0)
84 container.reset();
85 else if (!container)
86 container = Lucene::newInstance<array_type>(size);
87 else
88 container->resize(size);
89 array = container.get();
90 }
91
92 TYPE* get() const
93 {
94 return array->data;
95 }
96
97 int32_t size() const
98 {
99 return array->size;
100 }
101
102 bool equals(const this_type& other) const
103 {
104 if (array->size != other.array->size)
105 return false;
106 return (std::memcmp(array->data, other.array->data, array->size) == 0);
107 }
108
109 int32_t hashCode() const
110 {
111 return (int32_t)(int64_t)array;
112 }
113
114 TYPE& operator[] (int32_t i) const
115 {
116 BOOST_ASSERT(i >= 0 && i < array->size);
117 return array->data[i];
118 }
119
120 operator bool () const
121 {
122 return container;
123 }
124
125 bool operator! () const
126 {
127 return !container;
128 }
129
130 bool operator== (const Array<TYPE>& other)
131 {
132 return (container == other.container);
133 }
134
135 bool operator!= (const Array<TYPE>& other)
136 {
137 return (container != other.container);
138 }
139 };
140
141 template <class TYPE>
142 inline std::size_t hash_value(const Array<TYPE>& value)
143 {
144 return (std::size_t)value.hashCode();
145 }
146
147 template <class TYPE>
148 inline bool operator== (const Array<TYPE>& value1, const Array<TYPE>& value2)
149 {
150 return (value1.hashCode() == value2.hashCode());
151 }
152 }
153
154 #endif

clucene.sourceforge.net