@@ -24,6 +24,8 @@ authority from the Government may result in criminal liability under U.S. laws.
24
24
using System . IO ;
25
25
using System . Diagnostics ;
26
26
using System . Data ;
27
+ using System . Collections ;
28
+ using System . ComponentModel ;
27
29
28
30
namespace CAMInputOutput
29
31
{
@@ -213,8 +215,8 @@ protected enum EfficiencyPointParameterLocation
213
215
private const UInt16 headerSize = 0x800 ;
214
216
private const UInt16 blockHeaderSize = 0x30 ;
215
217
216
- IList < byte [ ] > lines ;
217
- IList < byte [ ] > nucs ;
218
+ List < byte [ ] > lines ;
219
+ List < byte [ ] > nucs ;
218
220
219
221
IList < Line > fileLines ;
220
222
IList < Nuclide > fileNuclides ;
@@ -1282,7 +1284,7 @@ public void CreateFile(string filePath)
1282
1284
//numRecords = numRecords + startRecord > lines.Count ? lines.Count - startRecord : (int)numLineRecords;
1283
1285
blockNo = startRecord + numRecords > lines . Count ? ( UInt16 ) 0U : ( UInt16 ) ( blockNo + 1U ) ;
1284
1286
//blockList[fileIndex] = GenerateBlock(CAMBlock.NLINES, loc, ((List<byte[]>)lines).GetRange(startRecord, numRecords), blockNo, i == 1);
1285
- byte [ ] block = GenerateBlock ( CAMBlock . NLINES , loc , ( ( List < byte [ ] > ) lines ) . GetRange ( startRecord , lines . Count - startRecord ) , blockNo , startRecord == 0 ) ;
1287
+ byte [ ] block = GenerateBlock ( CAMBlock . NLINES , loc , ( lines . ToList < byte [ ] > ( ) ) . GetRange ( startRecord , lines . Count - startRecord ) , blockNo , startRecord == 0 ) ;
1286
1288
numRecords = ( int ) BitConverter . ToUInt16 ( block , 0x1E ) ;
1287
1289
startRecord += numRecords ;
1288
1290
blockList . Add ( block ) ;
@@ -1346,7 +1348,7 @@ public void AddNuclide(string name, double halfLife, double halfLifeUnc, string
1346
1348
}
1347
1349
1348
1350
/// <summary>
1349
- /// Add a nuclide to the file
1351
+ /// Add a nuclide to the file after all the lines have been added
1350
1352
/// </summary>
1351
1353
/// <param name="nuc">Nuclide to add</param>
1352
1354
/// <param name="nucNo">The nuclide record number</param>
@@ -1386,6 +1388,8 @@ public void AddNuclide(Nuclide nuc)
1386
1388
lines . Where ( line => line [ 0x1B ] == BitConverter . GetBytes ( nucNo ) [ 0 ] ) . Select ( l => ( UInt16 ) ( lines . IndexOf ( l ) + 1U ) ) . ToArray ( ) :
1387
1389
new UInt16 [ 0 ] ;
1388
1390
1391
+ //IComparer lnNumComparer = new LineNumberComparer();
1392
+ Array . Sort ( lineNums ) ;
1389
1393
nucs . Add ( GenerateNuclide ( nuc . Name , nuc . HalfLife , nuc . HalfLifeUncertainty , nuc . HalfLifeUnit . ToUpper ( ) , lineNums ) ) ;
1390
1394
}
1391
1395
@@ -1414,14 +1418,21 @@ public void AddLine(Line line)
1414
1418
if ( nucNo > 255 )
1415
1419
throw new ArgumentException ( "Cannot have more than 255 nuclides" ) ;
1416
1420
1417
- //if the nuclide alredy exists add lines to it
1418
- if ( nucs != null && nucs . Count > nucNo )
1419
- AddLinesToNuclide ( nucs . ElementAt ( nucNo ) , new byte [ ] { BitConverter . GetBytes ( lines . Count ) [ 0 ] } ) ;
1420
1421
//initilize the lines
1421
1422
if ( lines == null || lines . Count < 1 )
1422
1423
lines = new List < byte [ ] > ( ) ;
1424
+ //insert in the correct position
1425
+ byte [ ] lineBytes = GenerateLine ( line . Energy , line . EnergyUncertainty , line . Abundance , line . AbundanceUncertainty , line . IsKeyLine , BitConverter . GetBytes ( nucNo ) [ 0 ] ) ;
1426
+ int lnIndex = ~ lines . BinarySearch ( lineBytes , new LineComparer ( ) ) ;
1427
+ if ( lnIndex < 0 )
1428
+ lines . Insert ( ~ lnIndex , lineBytes ) ;
1429
+ else
1430
+ lines . Insert ( lnIndex , lineBytes ) ;
1423
1431
1424
- lines . Add ( GenerateLine ( line . Energy , line . EnergyUncertainty , line . Abundance , line . AbundanceUncertainty , line . IsKeyLine , BitConverter . GetBytes ( nucNo ) [ 0 ] ) ) ;
1432
+ //if the nuclide alredy exists add lines to it
1433
+ if ( nucs != null && nucs . Count > nucNo )
1434
+ AddLinesToNuclide ( nucs . ElementAt ( nucNo ) , new byte [ ] { BitConverter . GetBytes ( lnIndex ) [ 0 ] } ) ;
1435
+ //lines.Add(GenerateLine(line.Energy, line.EnergyUncertainty, line.Abundance, line.AbundanceUncertainty, line.IsKeyLine, BitConverter.GetBytes(nucNo)[0]));
1425
1436
}
1426
1437
/// <summary>
1427
1438
/// Generate the contents of a CAM file file
@@ -1704,7 +1715,7 @@ protected byte[] GenerateNuclide(string name, double halfLife, double halfLifeUn
1704
1715
/// Add lines to an existing nuclide
1705
1716
/// </summary>
1706
1717
/// <param name="nuc">The nuclide</param>
1707
- /// <param name="lineNums">he record number of the lines to be associated with the nuclide</param>
1718
+ /// <param name="lineNums">the record number of the lines to be associated with the nuclide</param>
1708
1719
/// <returns></returns>
1709
1720
protected byte [ ] AddLinesToNuclide ( byte [ ] nuc , byte [ ] lineNums )
1710
1721
{
@@ -1716,6 +1727,8 @@ protected byte[] AddLinesToNuclide(byte[] nuc, byte[] lineNums)
1716
1727
//Create a byte array and fill it
1717
1728
byte [ ] linesList = new byte [ numLines * 0x3U ] ;
1718
1729
1730
+ Array . Sort ( lineNums ) ;
1731
+
1719
1732
for ( UInt16 i = 0 ; i < numLines ; i ++ )
1720
1733
{
1721
1734
UInt32 offset = i * 0x3U ;
@@ -1759,7 +1772,7 @@ public byte[] GenerateLine(double energy, double enUnc, double yield, double yie
1759
1772
/// <param name="pos">The position of the value to covert</param>
1760
1773
/// <param name="data">The array of bytes to be converted</param>
1761
1774
/// <returns>A date time</returns>
1762
- private DateTime ConvertDateTime ( byte [ ] data , UInt32 pos )
1775
+ public static DateTime ConvertDateTime ( byte [ ] data , UInt32 pos )
1763
1776
{
1764
1777
if ( data . Length < 0x08 )
1765
1778
throw new ArgumentException ( "The data input array is not long enough" ) ;
@@ -1777,7 +1790,7 @@ private DateTime ConvertDateTime(byte[] data, UInt32 pos)
1777
1790
/// </summary>
1778
1791
/// <param name="input">DateTime to convert</param>
1779
1792
/// <returns>byte repesetnation of hte DateTime in CAM format</returns>
1780
- private byte [ ] DateTimeToCAM ( DateTime input )
1793
+ public static byte [ ] DateTimeToCAM ( DateTime input )
1781
1794
{
1782
1795
//get the unix epoch
1783
1796
DateTime epoch = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , 0 , System . DateTimeKind . Utc ) ;
@@ -1796,7 +1809,7 @@ private byte[] DateTimeToCAM(DateTime input)
1796
1809
/// <param name="pos">The position of the value to covert</param>
1797
1810
/// <param name="data">The array of bytes to be converted</param>
1798
1811
/// <returns>The number of seconds</returns>
1799
- private double ConvertTimeSpan ( byte [ ] data , UInt32 pos )
1812
+ public static double ConvertTimeSpan ( byte [ ] data , UInt32 pos )
1800
1813
{
1801
1814
//check the inputs
1802
1815
if ( data . Length < 0x08 )
@@ -1820,7 +1833,7 @@ private double ConvertTimeSpan(byte[] data, UInt32 pos)
1820
1833
/// </summary>
1821
1834
/// <param name="input">Time span in seconds to convert</param>
1822
1835
/// <returns>byte represnentaion of the timespan in CAM format</returns>
1823
- private byte [ ] TimeSpanToCAM ( double input )
1836
+ public static byte [ ] TimeSpanToCAM ( double input )
1824
1837
{
1825
1838
Int64 tspan ;
1826
1839
byte [ ] span ;
@@ -1856,7 +1869,7 @@ private byte[] TimeSpanToCAM(double input)
1856
1869
/// <param name="pos">The start index of the value to covert</param>
1857
1870
/// <param name="data">The array of bytes to be converted</param>
1858
1871
/// <returns>The floating point value </returns>
1859
- private double ConvertFloat ( byte [ ] data , UInt32 pos )
1872
+ public static double ConvertFloat ( byte [ ] data , UInt32 pos )
1860
1873
{
1861
1874
if ( data . Length < 0x04 )
1862
1875
throw new ArgumentException ( "The data input array is not long enough" ) ;
@@ -1879,7 +1892,7 @@ private double ConvertFloat(byte[] data, UInt32 pos)
1879
1892
/// </summary>
1880
1893
/// <param name="input">number to be converted</param>
1881
1894
/// <returns>byte representation in PDP-11 format</returns>
1882
- private byte [ ] FloatToCAM ( double input )
1895
+ public static byte [ ] FloatToCAM ( double input )
1883
1896
{
1884
1897
byte [ ] word1 = new byte [ 0x2 ] , word2 = new byte [ 0x2 ] ;
1885
1898
byte [ ] inpByte = BitConverter . GetBytes ( Convert . ToSingle ( input * 4 ) ) ;
@@ -1892,6 +1905,31 @@ private byte[] FloatToCAM(double input)
1892
1905
#endregion
1893
1906
1894
1907
}
1908
+ class LineComparer : IComparer < byte [ ] >
1909
+ {
1910
+ public int Compare ( byte [ ] x , byte [ ] y )
1911
+ {
1912
+ return Comparer < double > . Default . Compare ( CAMIO . ConvertFloat ( x , 0x01 ) , CAMIO . ConvertFloat ( y , 0x01 ) ) ;
1913
+ }
1914
+ }
1895
1915
1916
+ //class LineNumberComparer : IComparer<byte>
1917
+ //{
1918
+ // public int Compare(byte x, byte y)
1919
+ // {
1920
+ // return Comparer<int>.Default.Compare(BitConverter.ToUInt16(x,0), BitConverter.ToUInt16(y, 0));
1921
+ // }
1922
+ //}
1923
+ //class SortedList : List<byte[]>
1924
+ //{
1925
+ // IComparer comparer;
1926
+ // public SortedList(IComparer comparer)
1927
+ // {
1928
+ // this.comparer = comparer;
1929
+ // }
1930
+
1931
+ // public override void Add(object obj)
1932
+
1933
+ //}
1896
1934
1897
1935
}
0 commit comments