files and streams
This commit is contained in:
parent
86e57edbc8
commit
f575f977b0
@ -6,16 +6,22 @@ public class ArrayGenObj<T> : ICollectionGenObj<T>
|
|||||||
{
|
{
|
||||||
// Массив объектов, которые храним
|
// Массив объектов, которые храним
|
||||||
private T?[] _collection;
|
private T?[] _collection;
|
||||||
public int Count => _collection.Length;
|
|
||||||
|
// Максимально допустимое число объектов в массиве
|
||||||
|
private int _maxCount;
|
||||||
|
public int Count => _collection.Count(s => s != null);
|
||||||
|
|
||||||
public int MaxCount
|
public int MaxCount
|
||||||
{
|
{
|
||||||
get { return _collection.Length; }
|
get { return _maxCount; }
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (value > 0)
|
if (value > 0)
|
||||||
{
|
{
|
||||||
if (_collection.Length > 0) Array.Resize(ref _collection, value);
|
if (_collection.Length == 0) _collection = new T?[value];
|
||||||
else _collection = new T?[value];
|
else Array.Resize(ref _collection, value);
|
||||||
|
|
||||||
|
_maxCount = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -28,7 +34,6 @@ public class ArrayGenObj<T> : ICollectionGenObj<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// methods :
|
// methods :
|
||||||
|
|
||||||
public T? GetItem(int index)
|
public T? GetItem(int index)
|
||||||
{
|
{
|
||||||
if (index > Count || index < 0)
|
if (index > Count || index < 0)
|
||||||
@ -40,8 +45,13 @@ public class ArrayGenObj<T> : ICollectionGenObj<T>
|
|||||||
|
|
||||||
public int Insert(T? item)
|
public int Insert(T? item)
|
||||||
{
|
{
|
||||||
// any empty place
|
if (Count >= _maxCount || item == null)
|
||||||
for (int i = 0; i < Count; i++)
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// any empty place -> fill immediately
|
||||||
|
for (int i = 0; i < _collection.Length; i++)
|
||||||
{
|
{
|
||||||
if (_collection[i] == null)
|
if (_collection[i] == null)
|
||||||
{
|
{
|
||||||
@ -49,53 +59,56 @@ public class ArrayGenObj<T> : ICollectionGenObj<T>
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T? item, int index)
|
public int Insert(T? item, int index)
|
||||||
{
|
{
|
||||||
|
if (index >= _maxCount || Count >= _maxCount ||
|
||||||
|
index < 0 || _collection[index] != null
|
||||||
|
|| item == null)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (_collection[index] == null)
|
if (_collection[index] == null)
|
||||||
{
|
{
|
||||||
_collection[index] = item;
|
_collection[index] = item;
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
int min_diff = 100, firstNullIndex = 100;
|
||||||
|
|
||||||
|
for (int i = 0; i < Count; i++)
|
||||||
{
|
{
|
||||||
int min_diff = 100, min_index = 100;
|
if (_collection[i] == null
|
||||||
|
&& min_diff > Math.Abs(index - i))
|
||||||
for (int i = 0; i < Count; i++)
|
|
||||||
{
|
{
|
||||||
if (_collection[i] == null
|
min_diff = Math.Abs(index - i);
|
||||||
&& min_diff > Math.Abs(index - i))
|
firstNullIndex = i;
|
||||||
{
|
|
||||||
min_diff = Math.Abs(index - i);
|
|
||||||
min_index = i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_collection[min_index] = item;
|
|
||||||
return min_index;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
_collection[firstNullIndex] = item;
|
||||||
|
return firstNullIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T? Remove(int index)
|
public T? Remove(int index)
|
||||||
{
|
{
|
||||||
T? item;
|
if (index >= Count || index < 0)
|
||||||
if (index < Count && index >= 0)
|
// on the other positions items don't exist
|
||||||
{
|
{
|
||||||
item = _collection[index];
|
return null;
|
||||||
_collection[index] = null;
|
|
||||||
return item;
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
T? item = _collection[index];
|
||||||
|
_collection[index] = null;
|
||||||
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<T?> GetItems()
|
public IEnumerable<T?> GetItems()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < _collection.Length; ++i)
|
for (int i = 0; i < MaxCount; ++i)
|
||||||
{
|
{
|
||||||
yield return _collection[i];
|
yield return _collection[i];
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
namespace ProjectCruiser.CollectionGenericObj;
|
namespace ProjectCruiser.CollectionGenericObj;
|
||||||
@ -8,7 +10,7 @@ public class ListGenObj<T> : ICollectionGenObj<T>
|
|||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
// Список объектов, которые храним
|
// Список объектов, которые храним
|
||||||
private readonly List<T?> _collection;
|
private List<T?> _collection;
|
||||||
|
|
||||||
// Максимально допустимое число объектов в списке
|
// Максимально допустимое число объектов в списке
|
||||||
private int _maxCount;
|
private int _maxCount;
|
||||||
@ -16,8 +18,17 @@ public class ListGenObj<T> : ICollectionGenObj<T>
|
|||||||
|
|
||||||
public int MaxCount
|
public int MaxCount
|
||||||
{
|
{
|
||||||
get { return _collection.Count; }
|
get { return _maxCount; }
|
||||||
set { if (value > 0) { _maxCount = value; } }
|
set
|
||||||
|
{
|
||||||
|
if (value > 0)
|
||||||
|
{
|
||||||
|
if (_collection.Count == 0) _collection = new List<T>(value);
|
||||||
|
else _collection.Capacity = value; // instead of resizing
|
||||||
|
|
||||||
|
_maxCount = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public CollectionType GetCollectionType => CollectionType.List;
|
public CollectionType GetCollectionType => CollectionType.List;
|
||||||
@ -37,7 +48,7 @@ public class ListGenObj<T> : ICollectionGenObj<T>
|
|||||||
return _collection[position];
|
return _collection[position];
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T obj)
|
public int Insert(T? obj)
|
||||||
{
|
{
|
||||||
if (Count >= _maxCount || obj == null)
|
if (Count >= _maxCount || obj == null)
|
||||||
{
|
{
|
||||||
@ -48,7 +59,7 @@ public class ListGenObj<T> : ICollectionGenObj<T>
|
|||||||
return Count;
|
return Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T obj, int position)
|
public int Insert(T? obj, int position)
|
||||||
{
|
{
|
||||||
if (position >= _maxCount || Count >= _maxCount ||
|
if (position >= _maxCount || Count >= _maxCount ||
|
||||||
position < 0 || _collection[position] != null
|
position < 0 || _collection[position] != null
|
||||||
|
@ -4,7 +4,7 @@ using ProjectCruiser.DrawningSamples;
|
|||||||
namespace ProjectCruiser.CollectionGenericObj;
|
namespace ProjectCruiser.CollectionGenericObj;
|
||||||
|
|
||||||
public class StorageCollection<T>
|
public class StorageCollection<T>
|
||||||
where T : DrawningBase
|
where T : DrawningBase // class
|
||||||
{
|
{
|
||||||
// Разделитель для записи ключа и значения элемента словаря
|
// Разделитель для записи ключа и значения элемента словаря
|
||||||
private readonly string _separatorForKeyValue = "|";
|
private readonly string _separatorForKeyValue = "|";
|
||||||
@ -36,14 +36,6 @@ public class StorageCollection<T>
|
|||||||
|
|
||||||
ICollectionGenObj<T> collection = CreateCollection(collType);
|
ICollectionGenObj<T> collection = CreateCollection(collType);
|
||||||
_storages.Add(name, collection);
|
_storages.Add(name, collection);
|
||||||
|
|
||||||
/*
|
|
||||||
switch (collType)
|
|
||||||
{
|
|
||||||
case CollectionType.List: _storages.Add(name, new ListGenObj<T>()); break;
|
|
||||||
case CollectionType.Array: _storages.Add(name, new ArrayGenObj<T>()); break;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Удаление коллекции ( по ключу-строке - её имени )
|
// Удаление коллекции ( по ключу-строке - её имени )
|
||||||
@ -53,7 +45,7 @@ public class StorageCollection<T>
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Доступ к коллекции ( по ключу-строке - её имени )
|
// Доступ к коллекции ( по ключу-строке - её имени ) - индексатор [!!!]
|
||||||
public ICollectionGenObj<T>? this[string name]
|
public ICollectionGenObj<T>? this[string name]
|
||||||
{
|
{
|
||||||
get => _storages.ContainsKey(name) ? _storages[name] : null;
|
get => _storages.ContainsKey(name) ? _storages[name] : null;
|
||||||
@ -72,25 +64,41 @@ public class StorageCollection<T>
|
|||||||
|
|
||||||
sb.Append(_collectionKey); // const
|
sb.Append(_collectionKey); // const
|
||||||
|
|
||||||
foreach (KeyValuePair<string, ICollectionGenObj<T>> value in _storages)
|
foreach (KeyValuePair<string, ICollectionGenObj<T>> pair in _storages)
|
||||||
{
|
{
|
||||||
sb.Append(Environment.NewLine); // не сохраняем пустые коллекции
|
sb.Append(Environment.NewLine); // не сохраняем пустые коллекции
|
||||||
if (value.Value.Count == 0) { continue; }
|
if (pair.Value.Count == 0) { continue; }
|
||||||
|
|
||||||
sb.Append(value.Key);
|
sb.Append(pair.Key);
|
||||||
sb.Append(_separatorForKeyValue);
|
sb.Append(_separatorForKeyValue);
|
||||||
sb.Append(value.Value.GetCollectionType);
|
sb.Append(pair.Value.GetCollectionType);
|
||||||
sb.Append(_separatorForKeyValue);
|
sb.Append(_separatorForKeyValue);
|
||||||
sb.Append(value.Value.MaxCount);
|
sb.Append(pair.Value.MaxCount);
|
||||||
sb.Append(_separatorForKeyValue);
|
sb.Append(_separatorForKeyValue);
|
||||||
|
|
||||||
foreach (T? item in value.Value.GetItems())
|
foreach (T? item in pair.Value.GetItems())
|
||||||
{
|
{
|
||||||
string data = item?.GetDataForSave() ?? string.Empty;
|
string data = item?.GetDataForSave() ?? string.Empty;
|
||||||
|
|
||||||
|
/*
|
||||||
|
string n = item.GetType().Name;
|
||||||
|
string data = null;
|
||||||
|
|
||||||
|
if (n != null && n == "DrawningCruiser")
|
||||||
|
{
|
||||||
|
data = ExtentionDrShip.GetDataForSave(item);
|
||||||
|
}
|
||||||
|
else if (n != null && n == "DrawningBase")
|
||||||
|
{
|
||||||
|
data = ExtentionDrShip.GetDataForSave(item);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(data))
|
if (string.IsNullOrEmpty(data))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.Append(data);
|
sb.Append(data);
|
||||||
sb.Append(_separatorItems);
|
sb.Append(_separatorItems);
|
||||||
}
|
}
|
||||||
@ -146,18 +154,22 @@ public class StorageCollection<T>
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_storages.Clear();
|
string[] companies = new string[strs.Length - 1];
|
||||||
foreach (string data in strs)
|
for (int k = 1; k < strs.Length; k++)
|
||||||
{
|
{
|
||||||
string[] record = data.Split(_separatorForKeyValue,
|
companies[k - 1] = strs[k];
|
||||||
StringSplitOptions.RemoveEmptyEntries);
|
}
|
||||||
|
|
||||||
|
_storages.Clear();
|
||||||
|
foreach (string data in companies)
|
||||||
|
{
|
||||||
|
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (record.Length != 4) // >
|
if (record.Length != 4) // >
|
||||||
// key | collType | maxcount | all next inf > 4
|
// key | collType | maxcount | all next inf > 4
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
CollectionType collectionType =
|
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
||||||
(CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
|
||||||
ICollectionGenObj<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
ICollectionGenObj<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
||||||
if (collection == null)
|
if (collection == null)
|
||||||
{
|
{
|
||||||
@ -167,11 +179,12 @@ public class StorageCollection<T>
|
|||||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||||
string[] set = record[3].Split(_separatorItems,
|
string[] set = record[3].Split(_separatorItems,
|
||||||
StringSplitOptions.RemoveEmptyEntries);
|
StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
foreach (string elem in set)
|
foreach (string elem in set)
|
||||||
{
|
{
|
||||||
if (elem?.CreateDrawningCar() is T car)
|
if (elem?.CreateDrawningCar() is T ship)
|
||||||
{
|
{
|
||||||
if (!(collection.Insert(car) == -1))
|
if (collection.Insert(ship) == -1)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ public static class ExtentionDrShip
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Получение данных для сохранения в файл
|
// Получение данных для сохранения в файл - - - - - - -
|
||||||
public static string GetDataForSave(this DrawningBase drShip)
|
public static string GetDataForSave(this DrawningBase drShip)
|
||||||
// метод расширения за счёт ключевого слова 'this'
|
// метод расширения за счёт ключевого слова 'this'
|
||||||
// вызов метода достигается не через имя класса,
|
// вызов метода достигается не через имя класса,
|
||||||
|
@ -25,7 +25,7 @@ public class EntityCruiser : EntityBase
|
|||||||
// Получение массива строк со значениями свойств
|
// Получение массива строк со значениями свойств
|
||||||
// объекта : тип (название класса), скорость, вес, осн. цвет [*],
|
// объекта : тип (название класса), скорость, вес, осн. цвет [*],
|
||||||
// доп. цвет, истинность наличия площадки и (,) ангаров.
|
// доп. цвет, истинность наличия площадки и (,) ангаров.
|
||||||
public virtual string[] GetStringRepresentation()
|
public override string[] GetStringRepresentation() // :O
|
||||||
{
|
{
|
||||||
return new[] { nameof(EntityCruiser), Speed.ToString(),
|
return new[] { nameof(EntityCruiser), Speed.ToString(),
|
||||||
Weight.ToString(), MainColor.Name, AdditionalColor.Name,
|
Weight.ToString(), MainColor.Name, AdditionalColor.Name,
|
||||||
|
@ -12,7 +12,6 @@ namespace ProjectCruiser
|
|||||||
// see https://aka.ms/applicationconfiguration.
|
// see https://aka.ms/applicationconfiguration.
|
||||||
ApplicationConfiguration.Initialize();
|
ApplicationConfiguration.Initialize();
|
||||||
Application.Run(new ServiceForm2());
|
Application.Run(new ServiceForm2());
|
||||||
// -> OceanForm1() inside*
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user