Use BigMap instances in PHP

Source: Internet
Author: User

Use BigMap instances in PHP

This article mainly introduces the use of BigMap instances in PHP. This article provides the implementation code directly. The Code contains detailed comments. For more information, see

 

 

?

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

<? Php

// The so-called Bit-map uses a bit to mark the Value corresponding to an element, and the Key is the element. Because Bit is used to store data, the storage space can be greatly reduced.

 

/*

 

If N = 1; the applied memory space is int a [2];

Assuming that the total number of tasks to be sorted or queried is N = 10000000, we need to apply for the memory size to be int a [1 + N/32], where: a [0] 32 in memory can correspond to 0-31 decimal numbers, and so on:

 

1. Calculate the subscript corresponding to decimal 0-n in array a: n/32

 

2. Calculate the number in 0-31 corresponding to 0-N: N % 32 = M

 

3. Use shift 0-31 to set the corresponding 32bit to 1: 1 <M, and set it to 1;

 

Example:

 

If you want to store 3

(1) a subscript 30/32 = 0; put it in a [0;

(2) 3% 32 = 30;

(3) shift 30 digits left 01000000 00000000 00000000 00000000 corresponding value $ a [0] = 1073741824;

 

 

1. Calculate the subscript corresponding to decimal 0-N in array:

Decimal 0-31, corresponding to a [0], first converted from the decimal number n to the remainder of 32 can be converted to the subscript corresponding to array. For example, if n = 24, then n/32 = 0, then the subscript of 24 in array a is 0. For example, if n = 60, then n/32 = 1, 60 corresponds to the subscript of 1 in array a. Similarly, 0-n in array a can be computed.

 

2. Calculate the number 0-N corresponding to 0-31:

 

0-31 corresponds to 0-31, while 32-63 corresponds to 0-31. Given a number n, the number corresponding to 0-31 can be obtained through modulo 32.

 

3. Use shift 0-31 to set the corresponding 32bit to 1.

 

Find the number of M corresponding to 0-31, and move M to the left: 2 ^ M. Then set 1.

 

Therefore, we can calculate the space occupied by 10000000 bits:

 

1 byte = 8bit

 

1kb = 1024 bytes

 

1 mb = 1024kb

The occupied space is 10000000/8/1024/1024 mb.

 

About 1 MB.

 

*/

 

Class bigMap {

// Save in two bytes

Private $ mask = 0x1f;

Private $ bitsperword = 32;

// The number of shifts is 5 pow () = 32

Private $ shift = 5;

// Array of stored data

Public $ bitArray = array ();

 

/**

$ I returns to zero.

*/

Function clearbit ($ I ){

/// The specified bit in the Current byte is taken as 0, and the bit in the other array of the other side will inevitably remain unchanged. This is a wonderful use of 1.

// $ I> SHIFT here is equivalent to intval ($ I/32 );

// $ I & $ this-> mask is equivalent to $ I % $ this-> mask here, taking the remainder

@ $ This-> bitArray [$ I >>$ this-> shift] & = ~ (1 <($ I & $ this-> mask ));

}

 

/**

$ I corresponds to 1

*/

Function setbit ($ I ){

@ $ This-> bitArray [$ I >$ this-> shift] | = (1 <($ I & $ this-> mask ));

}

 

// Test whether the bit of the test is 1

Function testbit ($ I ){

Return $ this-> bitArray [$ I >$ this-> shift] & (1 <($ I & $ this-> mask ));

}

}

 

 

$ OBig = new bigMap ();

 

$ OBig-> setbit (30 );

 

Var_dump ($ oBig-> testbit (2 ));

Var_dump ($ oBig-> bitArray );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.