Permalink
Browse files

Implements union and intersection on ObjectMask. This is likely to mi…

…grate to a pure Bitfield class eventually, with the Object mapping deferred to a different class.
  • Loading branch information...
1 parent 004451e commit 962b056708f953b140ed917e1221d111662ad795 @alecmce committed Nov 7, 2011
View
4 as3/src/ember/core/ComponentMaskFactory.as
@@ -7,12 +7,14 @@ package ember.core
final internal class ComponentMaskFactory
{
private var _map:Dictionary;
+
private var _objectMask:ObjectMask;
public function ComponentMaskFactory(objectMask:ObjectMask)
{
- _map = new Dictionary();
_objectMask = objectMask;
+
+ _map = new Dictionary();
}
public function getMask(klass:Class):Vector.<uint>
View
31 as3/src/ember/core/ObjectMask.as
@@ -65,12 +65,41 @@ package ember.core
if (b.length < length)
length = b.length;
- var result:Vector.<uint> = new Vector.<uint>();
+ var result:Vector.<uint> = new Vector.<uint>(length, false);
for (var i:int = 0; i < length; i++)
result[i] = a[i] & b[i];
return result;
}
+ public function union(a:Vector.<uint>, b:Vector.<uint>):Vector.<uint>
+ {
+ var short:uint = a.length;
+ var long:uint = b.length;
+ if (short > long)
+ {
+ short ^= long;
+ long ^= short;
+ short ^= long;
+ }
+
+ var result:Vector.<uint> = new Vector.<uint>(long, false);
+ for (var i:int = 0; i < short; i++)
+ result[i] = a[i] | b[i];
+
+ if (long == a.length)
+ {
+ for (i; i < long; i++)
+ result[i] = a[i];
+ }
+ else
+ {
+ for (i; i < long; i++)
+ result[i] = b[i];
+ }
+
+ return result;
+ }
+
}
}
View
57 as3/test/ember/core/ObjectMaskTests.as
@@ -1,7 +1,6 @@
package ember.core
{
import org.hamcrest.assertThat;
- import org.hamcrest.object.equalTo;
import org.hamcrest.object.isFalse;
import org.hamcrest.object.isTrue;
@@ -125,7 +124,61 @@ package ember.core
var a:Vector.<uint> = Vector.<uint>([3]);
var b:Vector.<uint> = Vector.<uint>([5]);
- assertThat(_map.intersection(a, b)[0], equalTo(1));
+ assertThat(_map.intersection(a, b), matchesVector(1));
+ }
+
+ [Test]
+ public function can_get_intersection_of_sets_with_multiple_values():void
+ {
+ var a:Vector.<uint> = Vector.<uint>([5, 3]); // 101, 011
+ var b:Vector.<uint> = Vector.<uint>([3, 2]); // 011, 010
+
+ assertThat(_map.intersection(a, b), matchesVector(1, 2)); // 001, 010
+ }
+
+ [Test]
+ public function can_get_intersection_of_sets_where_first_set_is_longer():void
+ {
+ var a:Vector.<uint> = Vector.<uint>([3, 1]); // 001, 011
+ var b:Vector.<uint> = Vector.<uint>([6]); // 110, 000
+
+ assertThat(_map.intersection(a, b), matchesVector(2)); // 000, 010
+ }
+
+ [Test]
+ public function can_get_intersection_of_sets_where_second_set_is_longer():void
+ {
+ var a:Vector.<uint> = Vector.<uint>([3]); // 000, 011
+ var b:Vector.<uint> = Vector.<uint>([6, 1]); // 110, 001
+
+ assertThat(_map.intersection(a, b), matchesVector(2)); // 000, 010
+ }
+
+ [Test]
+ public function can_get_union_of_sets():void
+ {
+ var a:Vector.<uint> = Vector.<uint>([3]); // 011
+ var b:Vector.<uint> = Vector.<uint>([5]); // 101
+
+ assertThat(_map.union(a, b), matchesVector(7)); // 111
+ }
+
+ [Test]
+ public function can_get_union_of_state_where_first_set_is_longer():void
+ {
+ var a:Vector.<uint> = Vector.<uint>([5, 4]); // 101, 100
+ var b:Vector.<uint> = Vector.<uint>([1]); // 001, 000
+
+ assertThat(_map.union(a, b), matchesVector(5, 4)); // 101, 100
+ }
+
+ [Test]
+ public function can_get_union_of_state_where_second_set_is_longer():void
+ {
+ var a:Vector.<uint> = Vector.<uint>([2]); // 010, 000
+ var b:Vector.<uint> = Vector.<uint>([5, 1]); // 101, 001
+
+ assertThat(_map.union(a, b), matchesVector(7, 1)); // 111, 001
}
}

0 comments on commit 962b056

Please sign in to comment.