Skip to content

Commit

Permalink
Merge pull request #95 from astreet/lazy_children
Browse files Browse the repository at this point in the history
Lazy init children array in CSSNode, update removeChildAt to return removed child
  • Loading branch information
astreet committed Jul 29, 2015
2 parents ae56a1d + 7104f7c commit 93d6f84
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/java/src/com/facebook/csslayout/CSSNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,40 @@ public static interface MeasureFunction {

public int lineIndex = 0;

// 4 is kinda arbitrary, but the default of 10 seems really high for an average View.
private final ArrayList<CSSNode> mChildren = new ArrayList<CSSNode>(4);

private @Nullable ArrayList<CSSNode> mChildren;
private @Nullable CSSNode mParent;
private @Nullable MeasureFunction mMeasureFunction = null;
private LayoutState mLayoutState = LayoutState.DIRTY;

public int getChildCount() {
return mChildren.size();
return mChildren == null ? 0 : mChildren.size();
}

public CSSNode getChildAt(int i) {
Assertions.assertNotNull(mChildren);
return mChildren.get(i);
}

public void addChildAt(CSSNode child, int i) {
if (child.mParent != null) {
throw new IllegalStateException("Child already has a parent, it must be removed first.");
}
if (mChildren == null) {
// 4 is kinda arbitrary, but the default of 10 seems really high for an average View.
mChildren = new ArrayList<>(4);
}

mChildren.add(i, child);
child.mParent = this;
dirty();
}

public void removeChildAt(int i) {
mChildren.remove(i).mParent = null;
public CSSNode removeChildAt(int i) {
Assertions.assertNotNull(mChildren);
CSSNode removed = mChildren.remove(i);
removed.mParent = null;
dirty();
return removed;
}

public @Nullable CSSNode getParent() {
Expand All @@ -94,6 +100,7 @@ public void removeChildAt(int i) {
* @return the index of the given child, or -1 if the child doesn't exist in this node.
*/
public int indexOf(CSSNode child) {
Assertions.assertNotNull(mChildren);
return mChildren.indexOf(child);
}

Expand Down

0 comments on commit 93d6f84

Please sign in to comment.