Re: optimizing space for array of booleans

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> Generally relationships like the one you describe are stored in three
> separate and related tables: Students, Courses, and Enrollment. The
> latter is a n:m association between the first two. The advantage this
> approach has with regard to storage is that it is a sparse matrix.

I've done that and I'm playing around with the last one, this is where the
problem arise. It's a table with two columns, user id & the 'set' that
stores the enroll information. Since SQL doesn't have 'nice set data type'
(MySQL has it but it's restricted to 64 entries, and it's difficult to use),
I need to cover the problem from PHP side.

> Most students will only enroll in a handful of courses. With this
> approach, you only store a row in the Enrollment table if a student is
> enrolled in a course. Otherwise, you don't need to store anything.

Do you mean that, for example, if student A enrolls course x,y,z the table
would be:

user_id | course
-------+-------
    A    |    x
    A    |    y
    A    |    z

Students often forget (or are lazy) to unenroll after they've finished
studying. Wouldn't the database be bloated? And what would be the primary
key?

> And no, 1.2MB is not that big of a deal. A single floppy high-density
> drive could hold that much data 18 years ago!

Yeah, but now I'm also considering the speed. So, I decided to build a class
that implements bitset operations and I'm gonna store the contents in
database as string. Here's the code: 

class bitset {
  private $bits;

  private function index($bit) { return $bit / 32; }
  private function offset($bit) { return $bit % 32; }

  private function binstr() {
    $temp_bits = $this->bits;
    $str = "";
    for ($i=0;$i<32;$i++) {
      $str = strval($temp_bits & 1) . $str;
      $temp_bits >>= 1;
    }
    return $str;
  }

  public function __construct() {
    $this->bits = array(0,0,0,0,0,0,0,0);
  }

  public function tostring() {
    $str = $this->binstr($this->bits[0]);
    for ($i=1;$i<8;$i++)
      $str .= "," . $this->binstr($this->bits[$i]);
    return $str;
  }

  public function set($bit) {
    $index  = $this->index($bit);
    $offset = $this->offset($bit);
    $this->bits[$index] |= 1 << $offset;
  }

  public function clear($bit) {
    $index  = $this->index($bit);
    $offset = $this->offset($bit);
    $this->bits[$index] &= ~(1 << $offset);
  }

  public function test($bit) {
    $index  = $this->index($bit);
    $offset = $this->offset($bit);
    return $this->bits[$index] & (1 << $offset);
  }
}

little problem: tostring() method always returns 00..001,00..001,..
regardless $bits contents. binstr() has been tested and works fine, what
might be wrong?



-- 
View this message in context: http://www.nabble.com/optimizing-space-for-array-of-booleans-tp22159131p22196477.html
Sent from the PHP - General mailing list archive at Nabble.com.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux