-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashIndex.php
More file actions
165 lines (152 loc) · 3.99 KB
/
HashIndex.php
File metadata and controls
165 lines (152 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Class for supporting hashindex
*
* @package search_in_memory
* @author Rafał Piekarski
*/
class HashIndex implements Countable {
/**
* Index PHP hash
*
* @var array
*/
protected $_list = array();
/**
* Index key name
*
* @var string
*/
protected $_keyName = null;
/**
* Constructor
*
* @param array $data
* @author Rafał Piekarski
*/
function __construct(array $data = array()) {
if (!empty($data)) {
$this->index($data);
}
}
/**
* Basic indexing function
*
* @param array $data
* @return bool
* @author Rafał Piekarski
*/
public function index(array $data = array()) {
$this->_list = array();
foreach ($data as $pkey => $key) {
$this->_list[$key][] = $pkey;
}
return true;
}
/**
* Indexing data with defined key which would be specified from single row
*
* @param string $key Indexed field nme
* @param array $data
* @return bool
* @author Rafał Piekarski
*/
public function indexWithKey($key, array $data = array()) {
$this->_list = array();
$this->_keyName = $key;
foreach ($data as $pkey => $record) {
if (is_array($record[$key])) {
foreach ($record[$key] as $word) {
$this->_list[$word][] = $pkey;
}
} else {
$this->_list[$record[$key]][] = $pkey;
}
}
return true;
}
/**
* Indexing with defined key only a other value from single data row
*
* @param string $key Index field
* @param string $pkey Data indexed field
* @param array $data
* @return bool
* @author Rafał Piekarski
*/
public function indexWithKeyAndPKey($key, $pkey, array $data = array()) {
$this->_list = array();
$this->_keyName = $key;
foreach ($data as $record) {
if (is_array($record[$key])) {
foreach ($record[$key] as $word) {
$this->_list[$word][] = $record[$pkey];
}
} else {
$this->_list[$record[$key]][] = $record[$pkey];
}
}
return true;
}
/**
* Count of all elements in index
*
* @return int
* @author Rafał Piekarski
*/
public function count() {
return count($this->_list);
}
/**
* Indexing whole data base on defined main key field
*
* @param string $pkey
* @param array $data
* @return bool
* @author Rafał Piekarski
*/
public function indexWithPKey($pkey, array $data = array()) {
$this->_list = array();
$this->_keyName = $pkey;
foreach ($data as $record) {
if (is_array($record[$pkey])) {
foreach ($record[$pkey] as $word) {
$this->_list[$word][] = $record;
}
} else {
$this->_list[$record[$pkey]] = $record;
}
}
return true;
}
/**
* Checks that data exists in index on specified key value
*
* @param string $keyValue
* @return bool
* @author Rafał Piekarski
*/
public function exists($keyValue) {
return $this->search($keyValue)?true:false;
}
/**
* Returns founded value from index.
*
* @param string $keyValue
* @return mixed
* @author Rafał Piekarski
*/
public function search($keyValue) {
$results = $this->_list[$keyValue];
return $results?$results:false;
}
/**
* Returns all keys from index
*
* @return array
* @author Rafał Piekarski
*/
public function keys() {
return array_keys($this->_list);
}
}