From c7725fe681b271a0ce14530751b433e55a52ec50 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 31 Jan 2019 19:35:01 -0600 Subject: [PATCH] AlgoStudio - hook up flat definition favorites --- .../Trading/AlgoStudio/AlgoStudioView.xaml | 84 ++++++++--- .../AlgoStudio/VM/AlgoDefinitionTemplateVM.cs | 3 +- .../Trading/AlgoStudio/VM/AlgoDefinitionVM.cs | 126 +++++++++++----- .../Trading/AlgoStudio/VM/AlgoStudioVM.cs | 17 ++- .../AlgoTemplateMetadata.cs | 142 ++++++++++-------- .../spark/Orders/OrderPropertiesEditorVM.cs | 51 ++++++- .../Collections/ObservableKeyedCollection.cs | 2 +- .../GenericCollectionExtensions.cs | 7 + src/client/spark/core/Utility/EditStack.cs | 1 + .../spark/core/Utility/VersionContainer.cs | 4 +- .../AlgoTemplateFavoriteButtonsView.xaml | 13 +- .../Controls/Order/AlgoTemplateToolBar.xaml | 8 +- .../Order/AlgoTemplateToolBar.xaml.cs | 23 ++- .../Order/OrderDetailsPopupControl.xaml | 2 +- .../spark/view/Resources/Converters.xaml | 4 + .../spark/view/Spark/Ladder/LadderSettings.cs | 40 ++++- .../view/Spark/Ladder/LadderSettingsVM.cs | 74 ++++++++- .../view/Spark/Ladder/LadderSettingsView.xaml | 58 +++++++ .../Spark/Ladder/LadderSettingsView.xaml.cs | 3 +- .../spark/view/Spark/Ladder/LadderVM.cs | 5 +- .../spark/view/Spark/Ladder/LadderView.xaml | 12 +- .../Spark/Utilities/WindowSettingsDialogVM.cs | 2 +- .../spark_api/utils/EqualityComparer.cs | 3 + .../Collections/Extensions/ListExtensions.cs | 7 + src/client/spark_core/utils/ArrayUtils.cs | 24 +++ src/client/spark_core/utils/ListUtils.cs | 44 ++++++ 26 files changed, 603 insertions(+), 156 deletions(-) create mode 100644 src/client/spark_core/utils/ListUtils.cs diff --git a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml index 8fcaa1446..a780d03a3 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml +++ b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml @@ -34,6 +34,12 @@ + AbbreviationActive + AbbreviationInactive + + + + \ No newline at end of file diff --git a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml index cedb353c1..559134d6c 100644 --- a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml +++ b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml @@ -190,9 +190,8 @@ @@ -238,6 +237,9 @@ + + + diff --git a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs index 92595f4dc..9a966d53a 100644 --- a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs +++ b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs @@ -54,10 +54,7 @@ namespace mbts.ui.spark.algo { BeginRedoDefinitionExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._redo_definition()), BeginUndoDefinitionExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._undo_definition()), BeginRemoveExecuted = WPFHelper.create_cmd_executed((tb, t) => tb._remove(t)), - BeginToggleFavoriteExecuted = WPFHelper.create_cmd_executed((tb, param) => { - if (param is AlgoDefinitionTemplateVM t) - t.is_favorite = !t.is_favorite; - }); + BeginToggleFavoriteExecuted = WPFHelper.create_cmd_executed((tb, param) => tb.ToggleFavoriteCommand.TryExecuteIfCan(param)); ComboBox _templates_combo; EditableTextBlock _definition_name_text; @@ -74,6 +71,12 @@ namespace mbts.ui.spark.algo { public bool ShowTemplatesDropDown { get => (bool)GetValue(ShowTemplatesDropDownProperty); set => SetValue(ShowTemplatesDropDownProperty, value); } #endregion + #region ShowToggleFavorite + public static readonly DependencyProperty ShowToggleFavoriteProperty = + WPFHelper.create_dp(nameof(ShowToggleFavorite)); + public bool ShowToggleFavorite { get => (bool)GetValue(ShowToggleFavoriteProperty); set => SetValue(ShowToggleFavoriteProperty, value); } + #endregion + #region ShowDefinitionName public static readonly DependencyProperty ShowDefinitionNameProperty = WPFHelper.create_dp(nameof(ShowDefinitionName)); @@ -191,6 +194,18 @@ namespace mbts.ui.spark.algo { public ICommand RedoDefinitionCommand { get => GetValue(RedoDefinitionCommandProperty) as ICommand; set => SetValue(RedoDefinitionCommandProperty, value); } #endregion + #region ToggleFavorite + public static readonly DependencyProperty ToggleFavoriteCommandProperty = + WPFHelper.create_dp(nameof(ToggleFavoriteCommand)); + public ICommand ToggleFavoriteCommand { get => GetValue(ToggleFavoriteCommandProperty) as ICommand; set => SetValue(ToggleFavoriteCommandProperty, value); } + #endregion + + #region ToggleFavorite Parameter + public static readonly DependencyProperty ToggleFavoriteCommandParameterProperty = + WPFHelper.create_dp(nameof(ToggleFavoriteCommandParameter)); + public object ToggleFavoriteCommandParameter { get => GetValue(ToggleFavoriteCommandParameterProperty); set => SetValue(ToggleFavoriteCommandParameterProperty, value); } + #endregion + #region IsEdited /// /// This property indicates whether or not the order definition held by the is 'dirty' - different from the one that would be created by the OrderPropertiesEditor at this time. diff --git a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml index 6c99a61e3..464f0f2a2 100644 --- a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml +++ b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml @@ -110,6 +110,7 @@ RedoDefinitionCommand="{Binding redo_definition_cmd}" SaveCommand="{Binding save_template_cmd}" SaveDefinitionCommand="{Binding save_definition_cmd}" + ToggleFavoriteCommand="{Binding toggle_favorite_cmd}" SelectedItem="{Binding selected_algo_template, Mode=TwoWay}" ItemContainerStyle="{StaticResource AlgoTemplateItemContainerStyle}" > @@ -143,7 +144,6 @@ - + + + + 0 diff --git a/src/client/spark/view/Spark/Ladder/LadderSettings.cs b/src/client/spark/view/Spark/Ladder/LadderSettings.cs index 4b4adfbca..981d4ce2a 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettings.cs +++ b/src/client/spark/view/Spark/Ladder/LadderSettings.cs @@ -174,6 +174,36 @@ namespace mbts.spark { (int)color; } + [ProtoContract] + struct LadderFavoriteAlgosSettings : IEquatable { + [ProtoMember(1)] + readonly AlgoPersistedKey[] _favorite_algos; + [ProtoMember(2)] + public readonly int column_count; + + public LadderFavoriteAlgosSettings(AlgoPersistedKey[] favorite_algos, int column_count) { + _favorite_algos = favorite_algos; + this.column_count = column_count; + } + + public IReadOnlyList favorite_algos => _favorite_algos ?? Array.Empty(); + + public bool Equals(LadderFavoriteAlgosSettings other) => + column_count == other.column_count && + favorite_algos.SequenceEqual(other.favorite_algos); + + public override bool Equals(object obj) => + EqualityComparer.equals_obj(this, obj); + + public override int GetHashCode() => + EqualityComparer.aggregate_hash( + column_count, + EqualityComparer.aggregate_hash_limited(favorite_algos)); + + public static bool operator ==(LadderFavoriteAlgosSettings a, LadderFavoriteAlgosSettings b) => a.Equals(b); + public static bool operator !=(LadderFavoriteAlgosSettings a, LadderFavoriteAlgosSettings b) => !a.Equals(b); + } + // IMPORTANT: BE SURE TO ADD ANY NEW PROPERTIES TO THE EQUALS FUNCTION! // 1 important reason at this time: // From the settings dialog, default settings will only be persisted when they are NOT EQUAL to the current default settings. @@ -206,6 +236,7 @@ namespace mbts.spark { order_entry_panel_initial_width = _default_order_entry_panel_width; zoom_factor = _default_zoom_factor; use_defaults = true; + favorite_algos = new LadderFavoriteAlgosSettings(null, 2); } public static LadderSettings Default { @@ -374,6 +405,9 @@ namespace mbts.spark { [ProtoMember(46), DefaultValue(_default_zoom_factor)] public double zoom_factor { get; set; } + [ProtoMember(47)] + public LadderFavoriteAlgosSettings favorite_algos { get; set; } + public bool Equals(LadderSettings other) => other != null && row_height == other.row_height && @@ -418,7 +452,8 @@ namespace mbts.spark { order_entry_panel_initial_width == other.order_entry_panel_initial_width && zoom_slider_visible == other.zoom_slider_visible && zoom_factor == other.zoom_factor && - use_defaults == other.use_defaults; + use_defaults == other.use_defaults && + favorite_algos == other.favorite_algos; public override bool Equals(object obj) => Equals(obj as LadderSettings); @@ -466,7 +501,8 @@ namespace mbts.spark { order_entry_panel_initial_width.GetHashCode(), zoom_slider_visible.GetHashCode(), zoom_factor.GetHashCode(), - use_defaults.GetHashCode()); + use_defaults.GetHashCode(), + favorite_algos.GetHashCode()); public struct WindowSpecifics { public readonly double order_entry_panel_width, zoom_factor; diff --git a/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs b/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs index 4a3c0dfb5..c247a842f 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs +++ b/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs @@ -1,19 +1,24 @@ using bts.api; +using bts.utils; using mbts.collections; using mbts.ui; +using mbts.ui.controls; using mbts.ui.controls.color_picker; using mbts.ui.shell; using mbts.ui.spark; +using mbts.ui.utility; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Reactive.Linq; using System.Windows; +using System.Windows.Input; using System.Windows.Media; using TextUtils = mbts.ui.utility.text.TextUtils; namespace mbts.spark { - class LadderSettingsVM : ViewModelBase, IWindowSettingsVM, ITemplateSelectorKey { + class LadderSettingsVM : ViewModelBase, IWindowSettingsVM, ITemplateSelectorKey, IDisposable { readonly Property _badge_style; readonly Property _alternate_order_size; readonly DelegateProperty _row_height; @@ -52,8 +57,9 @@ namespace mbts.spark { _cancel_sells_visibility = new OrderEntryPanelItemVisibilityVM("Cancel Sells"), _cancel_all_visibility = new OrderEntryPanelItemVisibilityVM("Cancel All"); - public LadderSettingsVM(bool is_synthetic, Func get_left_panel_width, Func get_zoom_factor) { + public LadderSettingsVM(OrdersModel model, bool is_synthetic, Func get_left_panel_width, Func get_zoom_factor) { this.is_synthetic = is_synthetic; + favorite_algos_vm = new FavoriteAlgosVM(model); size_presets = new ObservableEditableCollection>(_create_size_preset_vm); size_presets.CollectionChanged += (s, e) => RaisePropertyChanged(nameof(size_presets_text)); _badge_style = backing(nameof(badge_style)); @@ -91,6 +97,7 @@ namespace mbts.spark { } public MarketSizeDisplayVM size_display { get; } = new MarketSizeDisplayVM(); + public FavoriteAlgosVM favorite_algos_vm { get; } public double row_height => _row_height.Value; public bool is_synthetic { get; } @@ -158,6 +165,7 @@ namespace mbts.spark { _current = settings; badge_style = settings.badge_style; alternate_order_size = settings.alternate_order_size; + favorite_algos_vm.load_from(settings.favorite_algos); size_display.load_from(settings.size_display); size_presets.SetItems(settings.calculator_buttons.empty_if_null().Select(s => _create_size_preset_vm((uint)s))); _orders.assign_from(_current.orders_column); @@ -199,6 +207,7 @@ namespace mbts.spark { var clone = SparkUtils.clone_proto(_current); clone.badge_style = badge_style; clone.alternate_order_size = alternate_order_size; + clone.favorite_algos = favorite_algos_vm.create(); clone.size_display = size_display.create(); clone.calculator_buttons = size_presets.Select(vm => (int)vm.value).ToArray(); clone.orders_column = _orders.get_settings(); @@ -236,6 +245,8 @@ namespace mbts.spark { return clone; } + public void Dispose() => favorite_algos_vm.Dispose(); + IMutableValueWrapperVM _create_size_preset_vm() => _create_size_preset_vm(0); IMutableValueWrapperVM _create_size_preset_vm(uint init) { var vm = PropertyWrapperVMUtils.create_mutable_vm(init); @@ -486,5 +497,64 @@ namespace mbts.spark { show_numbers = show_numbers }; } + + public class FavoriteAlgosVM : DisposableViewModelBase, IValueFactoryVM { + readonly ObservableKeyedCollection _algo_definitions = new ObservableKeyedCollection(d => d.vm_key); + readonly ObservableCollection _raw_items = new ObservableCollection(); + int _column_count = 2; + + public FavoriteAlgosVM(OrdersModel model) { + items = new ObservableAggregateCollection(_raw_items, new ObservableCollection { FavoriteAlgoItem.AddButtonPlaceholderItem }); + add_item_cmd = new DelegateCommand(() => _raw_items.Add(_item_factory())); + remove_item_cmd = new DelegateCommand(fai => _raw_items.Remove(fai)); + _dispose = model.algo_model.definitions.OnDispatcher().Subscribe(update => { + AlgoDefinitionVMCollectionHelper.update_persisted_definitions(_algo_definitions, update, d => AlgoDefinitionVM.create_persisted(model, d, Observable.Return(null), false)); + }); + } + + public ObservableCollection items { get; } + public ICommand add_item_cmd { get; } + public ICommand remove_item_cmd { get; } + public int column_count { get => _column_count; set => Set(ref _column_count, value); } + public ObservableCollection column_count_options { get; } = new ObservableCollection(Enumerable.Range(1, 4)); + + public void load_from(LadderFavoriteAlgosSettings value) { + _raw_items.Clear(); + column_count = value.column_count; + _raw_items.AddRange(value.favorite_algos.Select(k => { + var fai = _item_factory(); + fai.selector.try_select(new AlgoVMKey(k)); + return fai; + })); + } + + public LadderFavoriteAlgosSettings create() => + new LadderFavoriteAlgosSettings(_raw_items.Where(fai => fai.algo_definition != null).Select(fai => fai.algo_definition.vm_key.persisted_id).ToArray(), column_count); + + FavoriteAlgoItem _item_factory() => + new FavoriteAlgoItem(_algo_definitions, new ListCollectionView(_algo_definitions) { + CustomSort = AlgoDefinitionVMCollectionHelper.default_comparer.to_non_generic() + }); + } + + public class FavoriteAlgoItem : ViewModelBase { + public static readonly FavoriteAlgoItem AddButtonPlaceholderItem = new _AddButtonPlaceholderItem(); + + internal readonly KeyedItemSelector selector; + + protected FavoriteAlgoItem() { } + + public FavoriteAlgoItem(ObservableKeyedCollection algo_definitions, ICollectionView algo_definitions_view) { + selector = new KeyedItemSelector(this, nameof(algo_definition), algo_definitions, d => d.vm_key); + this.algo_definitions_view = algo_definitions_view; + } + + public ICollectionView algo_definitions_view { get; } + public AlgoDefinitionVM algo_definition { get => selector.value; set => selector.value = value; } + + class _AddButtonPlaceholderItem : FavoriteAlgoItem, ITemplateSelectorKey { + public string key => LadderSettingsView.FavoriteAlgoAddButtonTemplateKey; + } + } } } \ No newline at end of file diff --git a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml index 732551d00..ac78253aa 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml @@ -24,6 +24,7 @@ 24 + @@ -597,6 +598,63 @@ + + + + + + + + + + + + + + Number of Columns + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml.cs b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml.cs index 44108667c..da00027a1 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml.cs +++ b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml.cs @@ -11,7 +11,8 @@ namespace mbts.ui.spark { partial class LadderSettingsView : UserControl { public const string OrdersColumnSampleTemplateKey = nameof(OrdersColumnSampleTemplateKey), - ColumnVisibilityToggleTemplateKey = nameof(ColumnVisibilityToggleTemplateKey); + ColumnVisibilityToggleTemplateKey = nameof(ColumnVisibilityToggleTemplateKey), + FavoriteAlgoAddButtonTemplateKey = nameof(FavoriteAlgoAddButtonTemplateKey); public LadderSettingsView() => InitializeComponent(); diff --git a/src/client/spark/view/Spark/Ladder/LadderVM.cs b/src/client/spark/view/Spark/Ladder/LadderVM.cs index 121e145dc..22aa62c1e 100644 --- a/src/client/spark/view/Spark/Ladder/LadderVM.cs +++ b/src/client/spark/view/Spark/Ladder/LadderVM.cs @@ -105,10 +105,10 @@ namespace mbts.spark { show_book_command = new DelegateCommand(_show_book); show_ticket_command = new DelegateCommand(_show_ticket); settings_command = new DialogCommand(() => { - var vm = new WindowSettingsDialogVM( + var vm = new WindowSettingsDialogVM( settings_manager, s => s.ladder, UserSettings.FileExtensions.LADDER, s => { - var svm = new LadderSettingsVM(security.ContractCase == Security.ContractOneofCase.Synthetic, () => left_panel_width, () => zoom_factor); + var svm = new LadderSettingsVM(model.order_gateway, security.ContractCase == Security.ContractOneofCase.Synthetic, () => left_panel_width, () => zoom_factor); svm.load(s); return svm; }, s => s.with_window_specifics(get_save_state().get_window_specifics())); @@ -125,6 +125,7 @@ namespace mbts.spark { _dispose = Disposable.Create( settings_command, non_shared_wsm, + settings.OnDispatcher().Subscribe(s => order_properties_vm.on_favorite_algos(s.favorite_algos)), order_properties_vm, order_properties_vm.observe_account(true).Select(a => a?.name ?? string.Empty).Subscribe(account_positions), account_positions, diff --git a/src/client/spark/view/Spark/Ladder/LadderView.xaml b/src/client/spark/view/Spark/Ladder/LadderView.xaml index 225e23fc6..ad9035dc2 100644 --- a/src/client/spark/view/Spark/Ladder/LadderView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderView.xaml @@ -483,9 +483,15 @@ + ItemsSource="{Binding order_properties_vm.favorite_algo_definitions_view}" + SelectedItem="{Binding order_properties_vm.selected_favorite_algo_definition}" + > + + + + + + diff --git a/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs b/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs index c62bebabc..d8c45d21f 100644 --- a/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs +++ b/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs @@ -11,7 +11,7 @@ namespace mbts.spark { T get_result(); } - class WindowSettingsDialogVM : ViewModelBase, IRequestViewClose where T : class, IEquatable where TVM : IWindowSettingsVM { + class WindowSettingsDialogVM : ViewModelBase, IRequestViewClose, IDisposable where T : class, IEquatable where TVM : IWindowSettingsVM { readonly Property _use_default; readonly DialogSkirtItem _ok_item, _apply_item, _cancel_item, _apply_to_defaults_item; readonly DelegateProperty> _left_skirt_items; diff --git a/src/client/spark_api/utils/EqualityComparer.cs b/src/client/spark_api/utils/EqualityComparer.cs index 10c40d5bc..799fc111f 100644 --- a/src/client/spark_api/utils/EqualityComparer.cs +++ b/src/client/spark_api/utils/EqualityComparer.cs @@ -98,6 +98,9 @@ namespace bts.utils { return hash; } + public static int aggregate_hash_limited(IReadOnlyList items, int max_count = 16) => + items.Count < max_count ? aggregate_hash(items) : items.Count; + // WARNING: be sure to call this within in an unchecked scope static void _accumulate_hash(ref int aggregate_hash, ref int item_hash) => aggregate_hash = aggregate_hash * _hash_multiplier + item_hash; diff --git a/src/client/spark_core/Collections/Extensions/ListExtensions.cs b/src/client/spark_core/Collections/Extensions/ListExtensions.cs index 9af7c4177..bbc8a5f7c 100644 --- a/src/client/spark_core/Collections/Extensions/ListExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/ListExtensions.cs @@ -43,6 +43,13 @@ namespace bts.collections { list.RemoveAt(index); } + public static int IndexOf(IReadOnlyList list, T value) { + for (int i = 0; i < list.Count; i++) + if (value.Equals(list[i])) + return i; + return -1; + } + class SubRangeWrapper : IList, IReadOnlyList { readonly IList _source; readonly int _start; diff --git a/src/client/spark_core/utils/ArrayUtils.cs b/src/client/spark_core/utils/ArrayUtils.cs index 30a34a840..b6c09b7fe 100644 --- a/src/client/spark_core/utils/ArrayUtils.cs +++ b/src/client/spark_core/utils/ArrayUtils.cs @@ -31,5 +31,29 @@ namespace bts.utils { Array.Copy(array, rv, array.Length); return rv; } + + public static T[] remove(T[] source, T value) { + int i = Array.IndexOf(source, value); + return i < 0 ? source : remove_at(source, i); + } + + public static T[] remove_at(T[] array, int index) { + T[] clone = new T[array.Length - 1]; + Array.Copy(array, 0, clone, 0, index); + Array.Copy(array, index + 1, clone, index, clone.Length - index); + return clone; + } + + public static T[] add(T[] array, T value) { + T[] clone = new T[array.Length + 1]; + Array.Copy(array, clone, array.Length); + clone[array.Length] = value; + return clone; + } + + public static T[] add_distinct(T[] array, T value) { + int i = Array.IndexOf(array, value); + return i < 0 ? add(array, value) : array; + } } } \ No newline at end of file diff --git a/src/client/spark_core/utils/ListUtils.cs b/src/client/spark_core/utils/ListUtils.cs new file mode 100644 index 000000000..085d2b773 --- /dev/null +++ b/src/client/spark_core/utils/ListUtils.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; + +namespace bts.utils { + public static class ListUtils { + public static IReadOnlyList add(IReadOnlyList list, T value) { + var clone = new T[list.Count + 1]; + for (int i = 0; i < list.Count; i++) + clone[i] = list[i]; + clone[list.Count] = value; + return clone; + } + + public static IReadOnlyList add_distinct(IReadOnlyList list, T value) { + int i = index_of(list, value); + return i >= 0 ? list : add(list, value); + } + + public static int index_of(IReadOnlyList list, T value) { + for (int i = 0; i < list.Count; i++) + if (value.Equals(list[i])) + return i; + return -1; + } + + public static IReadOnlyList remove(IReadOnlyList list, T value) { + int i = index_of(list, value); + return i < 0 ? list : remove_at(list, i); + } + + public static IReadOnlyList remove_at(IReadOnlyList list, int index) { + T[] clone = new T[list.Count - 1]; + for (int i = 0; i < index; i++) + clone[i] = list[i]; + for (int i = index + 1; i < clone.Length; i++) + clone[i] = list[i]; + return clone; + } + + static void _copy(IReadOnlyList source, int source_index, ref IList target, int target_index, int length) { + for (int i = 0; i < length; i++) + target[target_index + i] = source[source_index + i]; + } + } +} \ No newline at end of file