-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChordKey.php
83 lines (67 loc) · 1.67 KB
/
ChordKey.php
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
<?php
include('Hash.php');
class ChordKey {
public $keylen = 32;
/**
* @var string
*/
public $identifier = "";
/**
* @var int
*/
public $key = 0;
function __construct($arg)
{
if(is_int($arg)) {
$this->identifier = (string)$arg;
$this->key = $arg;
if(HASHKEYS) {
$hash = new Hash(0);
$this->key = $hash->hash($this->key);
}
}else if(is_string($arg)) {
$this->identifier = $arg;
$hash = new Hash(0);
$this->key = $hash->hash($arg);
}
}
public function isBetween($fromKey, $toKey) {
if ($fromKey->compareTo($toKey) < 0) {
if ($this->compareTo($fromKey) > 0 && $this->compareTo($toKey) < 0) {
return true;
}
} else if ($fromKey->compareTo($toKey) > 0) {
if ($this->compareTo($toKey) < 0 || $this->compareTo($fromKey) > 0) {
return true;
}
}
return false;
}
public function createStartKey($index) {
$newKey = ($this->key + pow(2, $index )) % pow(2, KEYSIZE);
return new ChordKey($newKey);
}
public function compareTo($target) {
if ($this->key == $target->key)
return 0;
if ($this->key > $target->key)
return 1;
if ($this->key < $target->key)
return -1;
}
public function __toString() {
return (string)$this->key;
}
public function getIdentifier() {
return $this->identifier;
}
public function setIdentifier($identifier) {
$this->identifier = $identifier;
}
public function getKey() {
return $this->key;
}
public function setKey($key) {
$this->key = $key;
}
}