Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 962b056708f953b140ed917e1221d111662ad795 1 parent 004451e
Alec McEachran authored
4 as3/src/ember/core/ComponentMaskFactory.as
@@ -7,12 +7,14 @@ package ember.core
7 7 final internal class ComponentMaskFactory
8 8 {
9 9 private var _map:Dictionary;
  10 +
10 11 private var _objectMask:ObjectMask;
11 12
12 13 public function ComponentMaskFactory(objectMask:ObjectMask)
13 14 {
14   - _map = new Dictionary();
15 15 _objectMask = objectMask;
  16 +
  17 + _map = new Dictionary();
16 18 }
17 19
18 20 public function getMask(klass:Class):Vector.<uint>
31 as3/src/ember/core/ObjectMask.as
@@ -65,12 +65,41 @@ package ember.core
65 65 if (b.length < length)
66 66 length = b.length;
67 67
68   - var result:Vector.<uint> = new Vector.<uint>();
  68 + var result:Vector.<uint> = new Vector.<uint>(length, false);
69 69 for (var i:int = 0; i < length; i++)
70 70 result[i] = a[i] & b[i];
71 71
72 72 return result;
73 73 }
74 74
  75 + public function union(a:Vector.<uint>, b:Vector.<uint>):Vector.<uint>
  76 + {
  77 + var short:uint = a.length;
  78 + var long:uint = b.length;
  79 + if (short > long)
  80 + {
  81 + short ^= long;
  82 + long ^= short;
  83 + short ^= long;
  84 + }
  85 +
  86 + var result:Vector.<uint> = new Vector.<uint>(long, false);
  87 + for (var i:int = 0; i < short; i++)
  88 + result[i] = a[i] | b[i];
  89 +
  90 + if (long == a.length)
  91 + {
  92 + for (i; i < long; i++)
  93 + result[i] = a[i];
  94 + }
  95 + else
  96 + {
  97 + for (i; i < long; i++)
  98 + result[i] = b[i];
  99 + }
  100 +
  101 + return result;
  102 + }
  103 +
75 104 }
76 105 }
57 as3/test/ember/core/ObjectMaskTests.as
... ... @@ -1,7 +1,6 @@
1 1 package ember.core
2 2 {
3 3 import org.hamcrest.assertThat;
4   - import org.hamcrest.object.equalTo;
5 4 import org.hamcrest.object.isFalse;
6 5 import org.hamcrest.object.isTrue;
7 6
@@ -125,7 +124,61 @@ package ember.core
125 124 var a:Vector.<uint> = Vector.<uint>([3]);
126 125 var b:Vector.<uint> = Vector.<uint>([5]);
127 126
128   - assertThat(_map.intersection(a, b)[0], equalTo(1));
  127 + assertThat(_map.intersection(a, b), matchesVector(1));
  128 + }
  129 +
  130 + [Test]
  131 + public function can_get_intersection_of_sets_with_multiple_values():void
  132 + {
  133 + var a:Vector.<uint> = Vector.<uint>([5, 3]); // 101, 011
  134 + var b:Vector.<uint> = Vector.<uint>([3, 2]); // 011, 010
  135 +
  136 + assertThat(_map.intersection(a, b), matchesVector(1, 2)); // 001, 010
  137 + }
  138 +
  139 + [Test]
  140 + public function can_get_intersection_of_sets_where_first_set_is_longer():void
  141 + {
  142 + var a:Vector.<uint> = Vector.<uint>([3, 1]); // 001, 011
  143 + var b:Vector.<uint> = Vector.<uint>([6]); // 110, 000
  144 +
  145 + assertThat(_map.intersection(a, b), matchesVector(2)); // 000, 010
  146 + }
  147 +
  148 + [Test]
  149 + public function can_get_intersection_of_sets_where_second_set_is_longer():void
  150 + {
  151 + var a:Vector.<uint> = Vector.<uint>([3]); // 000, 011
  152 + var b:Vector.<uint> = Vector.<uint>([6, 1]); // 110, 001
  153 +
  154 + assertThat(_map.intersection(a, b), matchesVector(2)); // 000, 010
  155 + }
  156 +
  157 + [Test]
  158 + public function can_get_union_of_sets():void
  159 + {
  160 + var a:Vector.<uint> = Vector.<uint>([3]); // 011
  161 + var b:Vector.<uint> = Vector.<uint>([5]); // 101
  162 +
  163 + assertThat(_map.union(a, b), matchesVector(7)); // 111
  164 + }
  165 +
  166 + [Test]
  167 + public function can_get_union_of_state_where_first_set_is_longer():void
  168 + {
  169 + var a:Vector.<uint> = Vector.<uint>([5, 4]); // 101, 100
  170 + var b:Vector.<uint> = Vector.<uint>([1]); // 001, 000
  171 +
  172 + assertThat(_map.union(a, b), matchesVector(5, 4)); // 101, 100
  173 + }
  174 +
  175 + [Test]
  176 + public function can_get_union_of_state_where_second_set_is_longer():void
  177 + {
  178 + var a:Vector.<uint> = Vector.<uint>([2]); // 010, 000
  179 + var b:Vector.<uint> = Vector.<uint>([5, 1]); // 101, 001
  180 +
  181 + assertThat(_map.union(a, b), matchesVector(7, 1)); // 111, 001
129 182 }
130 183
131 184 }

0 comments on commit 962b056

Please sign in to comment.
Something went wrong with that request. Please try again.