Changeset 3347 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2010-06-27T12:22:37+02:00 (14 years ago)
Author:
jttt
Message:

Use RelationMember[] array as storage in Relation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/Relation.java

    r3262 r3347  
    22
    33import java.util.ArrayList;
     4import java.util.Arrays;
    45import java.util.Collection;
    56import java.util.HashSet;
     
    1819public final class Relation extends OsmPrimitive {
    1920
    20     private final List<RelationMember> members = new ArrayList<RelationMember>();
     21    private RelationMember[] members = new RelationMember[0];
    2122
    2223    private BBox bbox;
     
    2829     */
    2930    public List<RelationMember> getMembers() {
    30         return new CopyList<RelationMember>(members.toArray(new RelationMember[members.size()]));
     31        return new CopyList<RelationMember>(members);
    3132    }
    3233
     
    4142        }
    4243
    43         this.members.clear();
    4444        if (members != null) {
    45             this.members.addAll(members);
     45            this.members = members.toArray(new RelationMember[members.size()]);
    4646        }
    4747        for (RelationMember rm:this.members) {
     
    5656     */
    5757    public int getMembersCount() {
    58         return members.size();
     58        return members.length;
    5959    }
    6060
    6161    public RelationMember getMember(int index) {
    62         return members.get(index);
     62        return members[index];
    6363    }
    6464
    6565    public void addMember(RelationMember member) {
    66         members.add(member);
     66        RelationMember[] newMembers = new RelationMember[members.length + 1];
     67        System.arraycopy(members, 0, newMembers, 0, members.length);
     68        newMembers[members.length] = member;
     69        members = newMembers;
    6770        member.getMember().addReferrer(this);
    6871        fireMembersChanged();
     
    7073
    7174    public void addMember(int index, RelationMember member) {
    72         members.add(index, member);
     75        RelationMember[] newMembers = new RelationMember[members.length + 1];
     76        System.arraycopy(members, 0, newMembers, 0, index);
     77        System.arraycopy(members, index, newMembers, index + 1, members.length - index);
     78        newMembers[index] = member;
     79        members = newMembers;
    7380        member.getMember().addReferrer(this);
    7481        fireMembersChanged();
     
    8289     */
    8390    public RelationMember setMember(int index, RelationMember member) {
    84         RelationMember result = members.set(index, member);
    85         if (result.getMember() != member.getMember()) {
     91        RelationMember originalMember = members[index];
     92        members[index] = member;
     93        if (originalMember.getMember() != member.getMember()) {
    8694            member.getMember().addReferrer(this);
    87             result.getMember().removeReferrer(this);
     95            originalMember.getMember().removeReferrer(this);
    8896            fireMembersChanged();
    8997        }
    90         return result;
     98        return originalMember;
    9199    }
    92100
     
    97105     */
    98106    public RelationMember removeMember(int index) {
     107        List<RelationMember> members = getMembers();
    99108        RelationMember result = members.remove(index);
    100         for (RelationMember rm:members) {
    101             // Do not remove referrer if this primitive is used in relation twice
    102             if (rm.getMember() == result.getMember())
    103                 return result;
    104         }
    105         result.getMember().removeReferrer(this);
    106         fireMembersChanged();
     109        setMembers(members);
    107110        return result;
    108111    }
     
    216219            return false;
    217220        Relation r = (Relation)other;
    218         return members.equals(r.members);
     221        return Arrays.equals(members, r.members);
    219222    }
    220223
     
    225228    public RelationMember firstMember() {
    226229        if (isIncomplete()) return null;
    227         return (members.size() == 0) ? null : members.get(0);
     230        return (members.length == 0) ? null : members[0];
    228231    }
    229232    public RelationMember lastMember() {
    230233        if (isIncomplete()) return null;
    231         return (members.size() == 0) ? null : members.get(members.size() -1);
     234        return (members.length == 0) ? null : members[members.length - 1];
    232235    }
    233236
     
    241244            return;
    242245
    243         ArrayList<RelationMember> todelete = new ArrayList<RelationMember>();
     246        List<RelationMember> todelete = new ArrayList<RelationMember>();
    244247        for (RelationMember member: members) {
    245248            if (member.getMember() == primitive) {
     
    247250            }
    248251        }
    249         primitive.removeReferrer(this);
     252        List<RelationMember> members = getMembers();
    250253        members.removeAll(todelete);
    251         fireMembersChanged();
     254        setMembers(members);
    252255    }
    253256
     
    279282            }
    280283        }
     284        List<RelationMember> members = getMembers();
    281285        members.removeAll(todelete);
    282         for (OsmPrimitive primitive:primitives) {
    283             primitive.removeReferrer(this);
    284         }
    285         fireMembersChanged();
     286        setMembers(members);
    286287    }
    287288
     
    314315    @Override
    315316    public BBox getBBox() {
    316         if (members.isEmpty())
     317        if (members.length == 0)
    317318            return new BBox(0, 0, 0, 0);
    318319        if (getDataSet() == null)
     
    333334            return null;
    334335        visitedRelations.add(this);
    335         if (members.isEmpty())
     336        if (members.length == 0)
    336337            return null;
    337338        else {
Note: See TracChangeset for help on using the changeset viewer.