Skip to content

Commit

Permalink
Add dimension.remove for removing dimensions.
Browse files Browse the repository at this point in the history
Calling dimension.remove() removes its event listeners and clears its
filters so that it can be garbage collected.  This also removes all
groups associated with a dimension.

Fixes square#10.
  • Loading branch information
jasondavies committed Mar 18, 2013
1 parent fce6d24 commit 77c9993
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
27 changes: 23 additions & 4 deletions crossfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ function crossfilter() {

var data = [], // the records
n = 0, // the number of records; data.length
m = 0, // number of dimensions in use
m = 0, // a bit mask representing which dimensions are in use
M = 8, // number of dimensions that can fit in `filters`
filters = crossfilter_array8(0), // M bits per record; 1 is filtered out
filterListeners = [], // when the filters change
Expand Down Expand Up @@ -573,10 +573,11 @@ function crossfilter() {
top: top,
bottom: bottom,
group: group,
groupAll: groupAll
groupAll: groupAll,
remove: remove
};

var one = 1 << m++, // bit mask, e.g., 00001000
var one = ~m & -~m, // lowest unset bit as mask, e.g., 00001000
zero = ~one, // inverted one, e.g., 11110111
values, // sorted, cached array
index, // value rank ↦ object id
Expand All @@ -585,6 +586,7 @@ function crossfilter() {
sort = quicksort_by(function(i) { return newValues[i]; }),
refilter = crossfilter_filterAll, // for recomputing filter
indexListeners = [], // when data is added
dimensionGroups = [],
lo0 = 0,
hi0 = 0;

Expand All @@ -596,7 +598,10 @@ function crossfilter() {

// Incorporate any existing data into this dimension, and make sure that the
// filter bitset is wide enough to handle the new dimension.
if (m > M) filters = crossfilter_arrayWiden(filters, M <<= 1);
m |= one;
if (M >= 32 ? !one : m & (1 << M) - 1) {
filters = crossfilter_arrayWiden(filters, M <<= 1);
}
preAdd(data, 0, n);
postAdd(data, 0, n);

Expand Down Expand Up @@ -785,6 +790,9 @@ function crossfilter() {
remove: remove
};

// Ensure that this group will be removed when the dimension is removed.
dimensionGroups.push(group);

var groups, // array of {key, value}
groupIndex, // object id ↦ group id
groupWidth = 8,
Expand Down Expand Up @@ -1082,6 +1090,17 @@ function crossfilter() {
return g;
}

function remove() {
dimensionGroups.forEach(function(group) { group.remove(); });
var i = dataListeners.indexOf(preAdd);
if (i >= 0) dataListeners.splice(i, 1);
i = dataListeners.indexOf(postAdd);
if (i >= 0) dataListeners.splice(i, 1);
for (i = 0; i < n; ++i) filters[i] &= zero;
m &= zero;
return dimension;
}

return dimension;
}

Expand Down
2 changes: 1 addition & 1 deletion crossfilter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 77c9993

Please sign in to comment.