Proper bitmasks

sdboyer - October 9, 2008 - 18:03
Project:Version Control API
Version:5.x-2.x-dev
Component:API module
Category:task
Priority:normal
Assigned:sdboyer
Status:active
Description

So, one thing. I believe I mentioned this before, but there are a few points where I think we could benefit - in terms of code brevity and (some, minor) speed - by switching to proper bitmasks for some of these constants. Quick example:

<?php
// VCS item types.
define('VERSIONCONTROL_ITEM_EMPTY',             0);
define('VERSIONCONTROL_ITEM_FILE',              1);
define('VERSIONCONTROL_ITEM_DIRECTORY',         2);
// "Deleted" item types are only used for items that don't exist in the
// repository (anymore), at least not in the given revision. That is mostly
// the case with items that were deleted by a commit and are returned as result
// by versioncontrol_get_operation_items(). A "deleted file" can also be
// returned by directory listings for CVS, representing "dead files".
define('VERSIONCONTROL_ITEM_FILE_DELETED',      3);
define('VERSIONCONTROL_ITEM_DIRECTORY_DELETED', 4);
?>

Could become:

<?php
define
('VERSIONCONTROL_ITEM_EMPTY',             0); // equivalent to {0x000}
define('VERSIONCONTROL_ITEM_FILE',              1); // equivalent to {0x001, 1 << 0}
define('VERSIONCONTROL_ITEM_DIRECTORY',         2); // equivalent to {0x002, 1 << 1}
define('VERSIONCONTROL_ITEM_DELETED',           4); // equivalent to {0x004, 1 << 2}
?>

I'll need to see where it's actually of real use, but the bottom line is that we can divorce whether or not something's deleted from whether it's a file or directory. It can have both attributes at once - $type |= VERSIONCONTROL_ITEM_FILE | VERSIONCONTROL_ITEM_DELETED - and it can help to granularize some operations. So unless you horribly object, I'll keep an eye out for places where this can be done and do it in stages :)

#1

jpetso - October 21, 2008 - 13:42

Oh hey, why didn't I notice this issue? Anyways, YEAH, you're totally right. The classical example where bitmasks make a lot of sense. No objections at all.

 
 

Drupal is a registered trademark of Dries Buytaert.