public void Add( ushort id, Adjacent item ) { if ( table[id] == null ) { table[id] = new Adjacent[1] { item }; } else { Adjacent[] newtable = new Adjacent[table[id].Length+1]; table[id].CopyTo( newtable, 0 ); newtable[table[id].Length] = item; table[id] = newtable; } }
public Adjacent[] this[int id] { get { if ( id < 0 || id > Province.Count ) throw new ArgumentOutOfRangeException(); return table[id]; } set { if ( id < 0 || id > Province.Count ) throw new ArgumentOutOfRangeException(); table[id] = new Adjacent[value.Length]; value.CopyTo( table[id], 0 ); } }
public void ReadFrom( BinaryReader reader ) { // Read offsets first int[] offsets = new int[Province.MaxValue+1]; int realcount = Province.MaxValue+1; Adjacent first = null; for ( int i=0; i<=Province.MaxValue; ++i ) { offsets[i] = reader.ReadInt32(); if ( (uint)offsets[i] == 0xCDCDCDCD ) { // No longer reading offsets... Stop! first = new Adjacent( (ushort)offsets[i-2], (AdjacencyType)offsets[i-1] ); offsets[i] = offsets[i-1] = offsets[i-2] = 0; realcount = i-2; break; } } // Read adjacency lists for each province for ( int i=0; i<realcount-1; ++i ) { int length = offsets[i+1]-offsets[i]; int a0 = 0; table[i] = new Adjacent[length]; if ( first != null && length > 0 ) { table[i][0] = first; first = null; a0 = 1; } for ( int a=a0; a<length; ++a ) { table[i][a] = new Adjacent( reader ); } } }
public ushort[] Merge( AdjacencyTable other ) { int[] tagged = new int[Province.InternalCount]; for ( int fromId=1; fromId<other.table.Length; ++fromId ) { if ( other.table[fromId] == null || other.table[fromId].Length == 0 ) continue; ++tagged[fromId]; table[fromId] = new Adjacent[other.table[fromId].Length]; other.table[fromId].CopyTo( table[fromId], 0 ); for ( int t=0; t<table[fromId].Length; ++t ) ++tagged[table[fromId][t].ID]; } // Count affected ids int count = 0; for ( ushort i=0; i<tagged.Length; ++i ) { if ( tagged[i] > 0 ) count++; } ushort[] affected = new ushort[count]; count = 0; for ( ushort i=0; i<tagged.Length; ++i ) { if ( tagged[i] > 0 ) affected[count++] = i; } return affected; }