implement method to trim Entries

This commit is contained in:
REAndroid 2023-02-27 12:34:59 -05:00
parent 94e6772e75
commit 64c08b592a
5 changed files with 64 additions and 0 deletions

View File

@ -45,6 +45,14 @@ public class TypeBlockArray extends BlockArray<TypeBlock>
public void sort(){ public void sort(){
sort(this); sort(this);
} }
public boolean removeNullEntries(int startId){
boolean result = true;
for(TypeBlock typeBlock:listItems()){
boolean removed = typeBlock.removeNullEntries(startId);
result = result && removed;
}
return result;
}
public void removeEmptyBlocks(){ public void removeEmptyBlocks(){
List<TypeBlock> allTypes=new ArrayList<>(listItems()); List<TypeBlock> allTypes=new ArrayList<>(listItems());
boolean foundEmpty=false; boolean foundEmpty=false;

View File

@ -24,6 +24,34 @@ public abstract class BlockArray<T extends Block> extends BlockContainer<T> impl
elementData= newInstance(0); elementData= newInstance(0);
} }
public void removeAllNull(int start){
removeAll(start, true);
}
public void removeAll(int start){
removeAll(start, false);
}
private void removeAll(int start, boolean check_null){
List<T> removeList = subList(start);
if(removeList.size()==0 || (check_null && !isAllNull(removeList))){
return;
}
T[] itemArray = this.elementData;
for(T item:removeList){
if(item==null){
continue;
}
if(!item.isNull()){
item.setNull(true);
}
int index = item.getIndex();
if(index>=0 && itemArray[index]==item){
item.setIndex(-1);
item.setParent(null);
itemArray[index] = null;
}
}
setChildesCount(start);
}
public List<T> subList(int start){ public List<T> subList(int start){
return subList(start, -1); return subList(start, -1);
} }
@ -363,6 +391,15 @@ public abstract class BlockArray<T extends Block> extends BlockContainer<T> impl
return "count="+ childesCount(); return "count="+ childesCount();
} }
public static boolean isAllNull(Collection<? extends Block> itemsList){
for(Block item:itemsList){
if(item!=null && !item.isNull()){
return false;
}
}
return true;
}
private class BlockIterator implements Iterator<T> { private class BlockIterator implements Iterator<T> {
private int mCursor; private int mCursor;
private final int mMaxSize; private final int mMaxSize;

View File

@ -53,6 +53,12 @@ public class TypeBlock extends Chunk<TypeHeader>
addChild(entryOffsets); addChild(entryOffsets);
addChild(mEntryArray); addChild(mEntryArray);
} }
public boolean removeNullEntries(int startId){
startId = 0x0000ffff & startId;
EntryArray entryArray = getEntryArray();
entryArray.removeAllNull(startId);
return entryArray.childesCount() == startId;
}
public PackageBlock getPackageBlock(){ public PackageBlock getPackageBlock(){
SpecTypePair specTypePair = getParent(SpecTypePair.class); SpecTypePair specTypePair = getParent(SpecTypePair.class);
if(specTypePair!=null){ if(specTypePair!=null){

View File

@ -65,6 +65,15 @@ public class SpecTypePair extends BlockContainer<Block>
public void sortTypes(){ public void sortTypes(){
getTypeBlockArray().sort(); getTypeBlockArray().sort();
} }
public boolean removeNullEntries(int startId){
startId = 0x0000ffff & startId;
boolean removed = getTypeBlockArray().removeNullEntries(startId);
if(!removed){
return false;
}
getSpecBlock().setEntryCount(startId);
return true;
}
public void removeEmptyTypeBlocks(){ public void removeEmptyTypeBlocks(){
getTypeBlockArray().removeEmptyBlocks(); getTypeBlockArray().removeEmptyBlocks();
} }

View File

@ -35,4 +35,8 @@ public class ReferenceBlock<T extends Block> implements ReferenceItem{
public int get() { public int get() {
return BlockItem.getInteger(this.block.getBytes(), this.offset); return BlockItem.getInteger(this.block.getBytes(), this.offset);
} }
@Override
public String toString(){
return get()+":"+this.block;
}
} }