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


Loading...
Searching...
No Matches
Array.h
Go to the documentation of this file.
1
2// 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
13namespace Lucene {
14
15template <typename TYPE>
16class ArrayData {
17public:
18 ArrayData(int32_t size_) {
19 data = NULL;
20 resize(size_);
21 }
22
24 resize(0);
25 }
26
27public:
28 TYPE* data;
29 int32_t size;
30
31public:
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
46template <typename TYPE>
47class Array {
48public:
51
53 array = NULL;
54 }
55
56protected:
57 boost::shared_ptr<array_type> container;
59
60public:
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 return array->data[i];
104 }
105
106 operator bool () const {
107 return container.get() != NULL;
108 }
109
110 bool operator! () const {
111 return !container;
112 }
113
114 bool operator== (const Array<TYPE>& other) {
115 return (container == other.container);
116 }
117
118 bool operator!= (const Array<TYPE>& other) {
119 return (container != other.container);
120 }
121};
122
123template <class TYPE>
124inline std::size_t hash_value(const Array<TYPE>& value) {
125 return (std::size_t)value.hashCode();
126}
127
128template <class TYPE>
129inline bool operator== (const Array<TYPE>& value1, const Array<TYPE>& value2) {
130 return (value1.hashCode() == value2.hashCode());
131}
132
133}
134
135#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:110
bool operator!=(const Array< TYPE > &other)
Definition Array.h:118
ArrayData< TYPE > array_type
Definition Array.h:50
void reset()
Definition Array.h:68
TYPE & operator[](int32_t i) const
Definition Array.h:102
Array()
Definition Array.h:52
void resize(int32_t size)
Definition Array.h:72
bool operator==(const Array< TYPE > &other)
Definition Array.h:114
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
Definition AbstractAllTermDocs.h:12
LPPAPI void * AllocMemory(size_t size)
Allocate block of memory.
std::size_t hash_value(const Array< TYPE > &value)
Definition Array.h:124
LPPAPI void * ReallocMemory(void *memory, size_t size)
Reallocate a given block of memory.
LPPAPI void FreeMemory(void *memory)
Release a given block of memory.
bool operator==(const Array< TYPE > &value1, const Array< TYPE > &value2)
Definition Array.h:129

clucene.sourceforge.net