This repository has been archived by the owner on May 27, 2022. It is now read-only.
forked from tuner/intarray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
90 lines (61 loc) · 2.24 KB
/
README
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
Intarray
--------
PHP arrays are overkill for storing primitive integer values. They are slow and
consume huge amounts of memory. Did I mention that serialization of PHP arrays
adds a lot of overhead?
**Intarray** (Integer array) PHP extension is a space and time efficient tool for
handling large int32_t arrays and performing lookups and set operations. Basically
each intarray is just a PHP binary string and Intarray extension provides a
bunch of functions for performing operations on it.
Some real world use cases with user ids:
- Is user online? (binary search to array of online users)
- Gimme common friends of me and some other user (intersection of two intarrays)
- Store members of a group to cache in an efficient manner
- And so on..
Usage
~~~~~
Intarray has two modes of operation:
1. Array - Values in random order. May contain duplicates.
2. Set - Unique values in ascending order.
The latter mode enables use of binary search and set operations that utilize
merge algorithm. It is up to the user to ensure that appropriate functions are
used on each type of array.
Examples
~~~~~~~~
::
<?php // Binary search
// Create an empty array full of zeroes
$a = intarray_create(100);
// Fill it with some values in ascending order
for ($n = 0; $n < 100; $n++) intarray_set($a, $n, $n * 4);
// Perform an O(log n) search and find index of value 188
$index = intarray_binarysearch($a, 188);
?>
... and we got 47::
<?php // Union
// Create two intarrays
$a = intarray_create_from_array(array(1, 2, 3));
$b = intarray_create_from_array(array(3, 4, 5));
// Get a union of them
$u = intarray_union($a, $b);
// Dump to screen
intarray_dump($u);
?>
... and we got { 1, 2, 3, 4, 5 }
API Doc
~~~~~~~
See intarray.php (and intarray.c)
How to install
~~~~~~~~~~~~~~
==== Compiling ====
1. phpize
If you use GCC:
2. ./configure CFLAGS="-O2 -g" --enable-intarray
If you use ICC (Intel C Compiler)
2. ./configure CFLAGS=" -no-prec-div -O3 -xO -unroll2 -g" CC=icc --enable-intarray
3. make
4. ( make test)
5. make install
6. intarray.so is installed in the default extensions directory
==== Add to php.ini ====
extension=intarray.so