PHP Classes
elePHPant
Icontem

File: bytearray.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Auslarrie Popier  >  Binary Array  >  bytearray.php  >  Download  
File: bytearray.php
Role: Class source
Content type: text/plain
Description: Keeps small integers (0-255) as single bytes in internal array storage.
Class: Binary Array
Manipulate compact arrays of integers with strings
Author: By
Last change: nc
Date: 4 years ago
Size: 1,484 bytes
 

Contents

Class file image Download
<?php

/**
 * Memory-efficient integer storage class.
 * Behaves like a normal array.
 *
 * This one uses: BINARY STRING
 * and stores: 8 BIT integer.
 *
 *
 * For example: 32768 entries use just their 32768 bytes
 * (plus 1240 byte for the object instance).
 *
 *
 */
class ByteArray IMPLEMENTS ArrayAccess {

    var
$data = "\0";
    var
$len = 1; // 1 byte = 8 bit
   
var $pack = "C";

   
/**
     * Initialize data.
     *
     * @param integer/string create either a fixed size empty array,
     * or use source data (hexstring)
     */
   
function __construct($from=NULL) {
        if (
is_string($from)) {
           
$this->data = $from;
        }
        elseif (
is_int($from)) {
           
$this->data = str_repeat("\0", $this->len * $from);
        }
    }

   
/**
     * Compare array index against data size.
     *
     */
   
function offsetExists ( $offset ) {
        return
strlen($this->data) - 1 >= $offset;
    }

   
/**
     * Retrieve value.
     *
     */
   
function offsetGet ( $offset ) {
        return
ord($this->data[$offset]);
    }

   
/**
     * Update value.
     *
     */
   
function offsetSet ( $offset , $value ) {
# assert($value < 0x100);
       
$this->data[$offset] = chr($value);
    }

   
/**
     * Unsetting not supported.
     *
     */
   
function offsetUnset ( $offset ) {
       
assert(false);
    }

}


?>