How can I swap two elements of a two-linked list(by switching links)? I realized that I need to consider four cases: change with the first / last elements, change neighboring elements, and all other cases.
The cell structure is as follows:
public class Item<T>
{
private T _Data;
private Item<T> _Next;
private Item<T> _Prev;
public T Value
{
get { return _Data; }
set { this._Data = value; }
}
public Item(T Data)
{
this._Data = Data;
}
public Item()
{
this._Data = default;
}
public Item<T> Next
{
get { return this._Next; }
set { this._Next = value; }
}
public Item<T> Prev
{
get { return this._Prev; }
set { this._Prev = value; }
}
}
This is an implementation attempt. What's wrong?(Here I also do insert sort, but it all depends on the exchange of two elements)
public override D_List<T> Sorting(D_List<T> a)
{
for (int top = 1; top < a.Count; top++)
{
int k = top;
while (k > 0 && a[k-1] > a[k])
{
k--;
}
if (k == 0)
{
a[top].Prev.Next = a[top].Next;
a[top].Next.Prev = a[top].Prev;
a[top].Next = a[k];
a[top].Prev = null;
a[k].Prev = a[top];
}
else if(k == a.Count - 2)
{
a[k].Prev.Next = a[top];
a[top].Prev = a[k].Prev;
a[k].Next = null;
a[k].Prev = a[top];
}
else if(k+1 == top)//стоят подряд
{
a[k].Prev.Next = a[top];
a[top].Prev = a[k].Prev;
a[k].Next = a[top].Next;
a[top].Next = a[k];
a[k].Next.Prev = a[k];
}
else
{
a[k].Prev = a[top];
a[top].Prev.Next = a[top].Next;
a[top].Next.Prev = a[top].Prev;
a[top].Prev = a[k].Prev;
a[top].Next = a[k];
a[top].Next.Prev = a[top];
}
}
return a;
}
Related
I have a problem with Xamarin.Forms whereby the View is not refreshing as I would expect it to.
I have bound a View to a ViewModel, and I have a series of methods to perform various functions.
On the successful completion of a function, I want it to execute my LoadResults() method.
The LoadResults() method works fine when I load the view initially. However, when I execute any other method (DeleteScan(id) method shown below) it should re-fire the LoadResults() method and reload the page.
I know it is entering the LoadResults() method as I've put a break point in there and stepped through it. But by the end of it, it doesn't reload the view.
I also know the view can refresh, as I have other simple methods which are simply updating properties in the VM and those changes are reflecting in the UI.
So for whatever reason, LoadResults() is not reloading the page.
Any ideas what I've done wrong here?
View Model Constructor and Properties
public class ResultsProcessViewModel : INotifyPropertyChanged
{
public ICommand AddTimingCommand { get; set; }
public ICommand AddScanCommand { get; set; }
public ICommand InsertTimingCommand { get; set; }
public ICommand InsertScanCommand { get; set; }
public ICommand DeleteTimingCommand { get; set; }
public ICommand DeleteScanCommand { get; set; }
public ICommand PublishCommand { get; set; }
public ICommand LoadResultsCommand { get; set; }
public ICommand CancelMissingFinisherCommand { get; set; }
public ICommand CancelMissingTimingCommand { get; set; }
public int RaceId { get; set; }
public DateTime RaceDate { get; set; }
//public ResultsViewModel(Race race)
public ResultsProcessViewModel(Race race)
{
AddTimingCommand = new Command<string>(AddTiming);
InsertTimingCommand = new Command(InsertTiming);
AddScanCommand = new Command<string>(AddScan);
InsertScanCommand = new Command(InsertScan);
DeleteTimingCommand = new Command<int>(DeleteTiming);
DeleteScanCommand = new Command<int>(DeleteScan);
PublishCommand = new Command(Publish);
LoadResultsCommand = new Command<int>(LoadResults);
CancelMissingFinisherCommand = new Command(CancelMissingFinisher);
CancelMissingTimingCommand = new Command(CancelMissingTiming);
Invalid = false;
PublishStatus = race.ResultStatus;
Published = false;
PublishVisibility = false;
AddTimingVisibility = false;
AddScanVisibility = false;
AddVisibility = true;
IsBusy = false;
RaceId = race.Id;
RaceDate = race.RaceDate;
RaceStartTime = Convert.ToDateTime(race.RaceStartTime);
Scans = ScansAPI.GetScans(RaceId);
Timings = TimingsAPI.GetTimings(RaceId);
Entries = EntriesAPI.GetEntries(RaceId);
LoadResults(RaceId);
}
ObservableCollection<GroupedResultModel> _grouped;
public ObservableCollection<GroupedResultModel> grouped
{
get { return _grouped; }
set
{
if (_grouped == value)
return;
_grouped = value;
OnPropertyChanged("Grouped");
}
}
DateTime _raceStartTime;
public DateTime RaceStartTime
{
get { return _raceStartTime; }
set
{
if (_raceStartTime == value)
return;
_raceStartTime = value;
OnPropertyChanged("RaceStartTime");
}
}
int _timingPosition;
public int TimingPosition
{
get { return _timingPosition; }
set
{
if (_timingPosition == value)
return;
_timingPosition = value;
OnPropertyChanged("TimingPosition");
}
}
int _addScanTimingId;
public int AddScanTimingId
{
get { return _addScanTimingId; }
set
{
if (_addScanTimingId == value)
return;
_addScanTimingId = value;
OnPropertyChanged("AddScanTimingId");
}
}
int _addTimingScanId;
public int AddTimingScanId
{
get { return _addTimingScanId; }
set
{
if (_addTimingScanId == value)
return;
_addTimingScanId = value;
OnPropertyChanged("AddTimingScanId");
}
}
int _scanPosition;
public int ScanPosition
{
get { return _scanPosition; }
set
{
if (_scanPosition == value)
return;
_scanPosition = value;
OnPropertyChanged("ScanPosition");
}
}
bool _isBusy;
public bool IsBusy
{
get { return _isBusy; }
set
{
if (_isBusy == value)
return;
_isBusy = value;
OnPropertyChanged("IsBusy");
}
}
bool _published;
public bool Published
{
get { return _published; }
set
{
if (_published == value)
return;
_published = value;
OnPropertyChanged("Published");
}
}
bool _publishVisibility;
public bool PublishVisibility
{
get { return _publishVisibility; }
set
{
if (_publishVisibility == value)
return;
_publishVisibility = value;
OnPropertyChanged("PublishVisibility");
}
}
bool _error;
public bool Error
{
get { return _error; }
set
{
if (_error == value)
return;
_error = value;
OnPropertyChanged("Error");
}
}
bool _addVisibility;
public bool AddVisibility
{
get { return _addVisibility; }
set
{
if (_addVisibility == value)
return;
_addVisibility = value;
OnPropertyChanged("AddVisibility");
}
}
bool _addTimingVisibility;
public bool AddTimingVisibility
{
get { return _addTimingVisibility; }
set
{
if (_addTimingVisibility == value)
return;
_addTimingVisibility = value;
OnPropertyChanged("AddTimingVisibility");
}
}
bool _addScanVisibility;
public bool AddScanVisibility
{
get { return _addScanVisibility; }
set
{
if (_addScanVisibility == value)
return;
_addScanVisibility = value;
OnPropertyChanged("AddScanVisibility");
}
}
ObservableCollection<Result> _results;
public ObservableCollection<Result> Results
{
get
{
return _results;
}
set
{
if (_results != value)
{
_results = value;
OnPropertyChanged("Results");
}
}
}
ObservableCollection<Scan> _scans;
public ObservableCollection<Scan> Scans
{
get
{
return _scans;
}
set
{
if (_scans != value)
{
_scans = value;
OnPropertyChanged("Scans");
}
}
}
ObservableCollection<Timing> _timings;
public ObservableCollection<Timing> Timings
{
get
{
return _timings;
}
set
{
if (_timings != value)
{
_timings = value;
OnPropertyChanged("Timings");
}
}
}
ObservableCollection<RaceEntry> _entries;
public ObservableCollection<RaceEntry> Entries
{
get
{
return _entries;
}
set
{
if (_entries != value)
{
_entries = value;
OnPropertyChanged("Entries");
}
}
}
bool _invalid;
public bool Invalid
{
get { return _invalid; }
set
{
if (_invalid == value)
return;
_invalid = value;
OnPropertyChanged("Invalid");
}
}
bool _resultsInvalid;
public bool ResultsInvalid
{
get { return _resultsInvalid; }
set
{
if (_resultsInvalid == value)
return;
_resultsInvalid = value;
OnPropertyChanged("ResultsInvalid");
}
}
bool _insertScanVisibility;
public bool InsertScanVisibility
{
get { return _insertScanVisibility; }
set
{
if (_insertScanVisibility == value)
return;
_insertScanVisibility = value;
OnPropertyChanged("InsertScanVisibility");
}
}
string _validationError;
public string ValidationError
{
get { return _validationError; }
set
{
if (_validationError == value)
return;
_validationError = value;
OnPropertyChanged("ValidationError");
}
}
string _resultsValidationError;
public string ResultsValidationError
{
get { return _resultsValidationError; }
set
{
if (_resultsValidationError == value)
return;
_resultsValidationError = value;
OnPropertyChanged("ResultsValidationError");
}
}
string _missingBib;
public string MissingBib
{
get { return _missingBib; }
set
{
if (_missingBib == value)
return;
_missingBib = value;
OnPropertyChanged("MissingBib");
}
}
string _errorMessage;
public string ErrorMessage
{
get { return _errorMessage; }
set
{
if (_errorMessage == value)
return;
_errorMessage = value;
OnPropertyChanged("ErrorMessage");
}
}
public Race SelectedRace { get; set; }
private bool _raceCountZero;
public bool RaceCountZero
{
get { return _raceCountZero; }
set
{
if (_raceCountZero == value)
return;
_raceCountZero = value;
OnPropertyChanged("RaceCountZero");
}
}
string _aboveBelow;
public string AboveBelow
{
get { return _aboveBelow; }
set
{
if (_aboveBelow == value)
return;
_aboveBelow = value;
OnPropertyChanged("AboveBelow");
}
}
string _aboveBelowPosition;
public string AboveBelowPosition
{
get { return _aboveBelowPosition; }
set
{
if (_aboveBelowPosition == value)
return;
_aboveBelowPosition = value;
OnPropertyChanged("AboveBelowPosition");
}
}
string _publishStatus;
public string PublishStatus
{
get { return _publishStatus; }
set
{
if (_publishStatus == value)
return;
_publishStatus = value;
OnPropertyChanged("PublishStatus");
}
}
string _newBatchCode;
public string NewBatchCode
{
get { return _newBatchCode; }
set
{
if (_newBatchCode == value)
return;
_newBatchCode = value;
OnPropertyChanged("NewBatchCode");
}
}
string _addHH;
public string AddHH
{
get { return _addHH; }
set
{
if (_addHH == value)
return;
_addHH = value;
OnPropertyChanged("AddHH");
}
}
string _addMM;
public string AddMM
{
get { return _addMM; }
set
{
if (_addMM == value)
return;
_addMM = value;
OnPropertyChanged("AddMM");
}
}
string _addSS;
public string AddSS
{
get { return _addSS; }
set
{
if (_addSS == value)
return;
_addSS = value;
OnPropertyChanged("AddSS");
}
}
string _scanBatchCode;
public string ScanBatchCode
{
get { return _scanBatchCode; }
set
{
if (_scanBatchCode == value)
return;
_scanBatchCode = value;
OnPropertyChanged("ScanBatchCode");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
DeleteScan method
void DeleteScan(int scanid)
{
//Do stuff when deleting a scan. It needs to do all this in the observable collection. Then when we submit, we'll update all scans, timings etc with whats in the collection
Task.Run(() =>
{
try
{
Device.BeginInvokeOnMainThread(() => IsBusy = true);
var IsThereConnection = GlobalFunctions.CheckForInternetConnection();
if (IsThereConnection == false)
throw new Exception("You cannot delete a scan whilst you are offline");
//We are good to go
else
{
var deletescan = ScansAPI.DeleteScan(scanid);
if (deletescan.Code != "NoContent")
throw new Exception("Error deleting scan");
}
}
catch (Exception ex)
{
Device.BeginInvokeOnMainThread(() =>
{
ValidationError = ex.Message;
Invalid = true;
});
return;
}
finally
{
var newscans = ScansAPI.GetScans(RaceId);
var sortednewscans = new ObservableCollection<Scan>(newscans.OrderBy(c => c.Position));
Scans = sortednewscans;
Device.BeginInvokeOnMainThread(() => Published = false);
Device.BeginInvokeOnMainThread(() => LoadResults(RaceId));
Device.BeginInvokeOnMainThread(() => AddScanVisibility = false);
Device.BeginInvokeOnMainThread(() => IsBusy = false);
}
});
}
LoadResults Method
void LoadResults(int raceid)
{
var results = new ObservableCollection<Result>();
//Start with the timings
foreach (Timing timing in Timings)
{
var result = new Result();
//Basic details
result.RaceId = RaceId;
result.RaceDate = RaceDate;
result.Status = "Processing";
result.BackgroundColour = "White";
result.ScanTrashVisibility = true;
result.TimingTrashVisibility = true;
//Timing Data
result.TimingId = timing.Id;
result.TimingPosition = timing.Position;
result.TimingStatus = timing.Status;
result.StartTime = timing.StartTime;
result.EndTime = timing.EndTime;
result.TimingBatchCode = timing.BatchCode;
result.TimingBatchPosition = timing.BatchCode + timing.Position.ToString();
var elapsed = result.EndTime - result.StartTime;
string elapsedhours = elapsed.Hours.ToString();
string elapsedminutes = elapsed.Minutes.ToString();
string elapsedseconds = elapsed.Seconds.ToString();
string elapsedmillis;
if (elapsed.Milliseconds.ToString().Length > 2)
{
elapsedmillis = elapsed.Milliseconds.ToString().Substring(0, 2);
}
else
{
elapsedmillis = elapsed.Milliseconds.ToString();
}
if (elapsedhours.Length == 1) { elapsedhours = "0" + elapsedhours; }
if (elapsedminutes.Length == 1) { elapsedminutes = "0" + elapsedminutes; }
if (elapsedseconds.Length == 1) { elapsedseconds = "0" + elapsedseconds; }
if (elapsedmillis.Length == 1) { elapsedmillis = "0" + elapsedmillis; }
if((elapsedhours == "00"))
{
result.Elapsed = elapsedminutes + ":" + elapsedseconds + "." + elapsedmillis;
}
else
{
result.Elapsed = elapsedhours + ":" + elapsedminutes + ":" + elapsedseconds + "." + elapsedmillis;
}
results.Add(result);
}
//Add in the scans
foreach (Result result1 in results)
{
var scan = Scans.Where(p => p.BatchCode == result1.TimingBatchCode)
.FirstOrDefault(p => p.Position == result1.TimingPosition);
if (scan != null)
{
result1.ScanId = scan.Id;
result1.ScanPosition = scan.Position;
result1.ScanStatus = scan.Status;
result1.ScanBibNumber = scan.BibNumber;
result1.ScanBatchCode = scan.BatchCode;
result1.ScanBatchPosition = scan.BatchCode + scan.Position.ToString();
}
else
{
result1.ScanId = 0;
result1.ScanPosition = 0;
result1.ScanStatus = 99;
result1.ScanBibNumber = "UNKNOWN";
result1.AddScanButtonVisibility = true;
result1.ScanBatchCode = result1.TimingBatchCode;
}
}
//Add any scans which there are no times for
var notimescans = new ObservableCollection<Scan>();
foreach (Scan scan in Scans)
{
var checkscan = results.FirstOrDefault(s => s.ScanId == scan.Id);
if (checkscan == null)
{
var newresult = new Result();
newresult.RaceId = RaceId;
newresult.RaceDate = RaceDate;
newresult.Status = "Processing";
newresult.ScanId = scan.Id;
newresult.ScanPosition = scan.Position;
newresult.ScanStatus = scan.Status;
newresult.ScanBibNumber = scan.BibNumber;
newresult.ScanBatchCode = scan.BatchCode;
newresult.ScanBatchPosition = scan.BatchCode + scan.Position.ToString();
newresult.ScanTrashVisibility = true;
newresult.AddTimingButtonVisibility = true;
newresult.TimingId = 0;
newresult.TimingPosition = 99999;
newresult.TimingStatus = 99;
newresult.Elapsed = "N/A";
results.Add(newresult);
}
}
//Then add in the entries
foreach (Result result2 in results)
{
var entry = Entries.FirstOrDefault(p => p.BibNumber == result2.ScanBibNumber);
if (entry != null)
{
result2.EntryId = entry.Id;
result2.FirstName = entry.FirstName;
result2.LastName = entry.LastName;
result2.FormattedName = entry.FirstName + " " + entry.LastName.ToUpper();
result2.Gender = entry.Gender;
result2.DateOfBirth = entry.DateOfBirth;
result2.Club = entry.Club;
result2.Team = entry.Team;
result2.EntryBibNumber = entry.BibNumber;
if (result2.Club == null)
{
result2.FormattedClub = "";
}
else
{
result2.FormattedClub = entry.Club.ToUpper();
}
}
else
{
result2.EntryId = 0;
result2.FirstName = "Unknown";
result2.LastName = "ATHLETE";
result2.FormattedName = entry.FirstName + " " + entry.LastName.ToUpper();
result2.Gender = "Unknown";
result2.DateOfBirth = DateTime.Now;
result2.Club = "";
result2.Team = "";
result2.EntryBibNumber = result2.ScanBibNumber + " (Unrecognised)";
}
if(result2.ScanBatchCode == "NULLBATCH")
{
result2.ScanBatchCode = "Default";
}
if (result2.TimingBatchCode == "NULLBATCH")
{
result2.TimingBatchCode = "Default";
}
}
if(Scans.Count() != Timings.Count())
{
ResultsInvalid = true;
ResultsValidationError = "Your scan count and timing count's don't match. Please continue processing your results until these match and you will then be able to publish.";
}
else
{
Invalid = false;
PublishVisibility = true;
}
var sortedresults = new ObservableCollection<Result>(results.OrderBy(c => c.Elapsed));
int newposition = 1;
foreach (Result sortedresult in sortedresults)
{
sortedresult.OverallPosition = newposition;
newposition = newposition + 1;
}
//Create batches
grouped = new ObservableCollection<GroupedResultModel>();
foreach (Result result in results)
{
GroupedResultModel resultGroup = new GroupedResultModel();
var groupcheck = grouped.FirstOrDefault(b => b.BatchCode == result.ScanBatchCode);
if (groupcheck == null)
{
resultGroup.BatchCode = result.ScanBatchCode;
var BatchOfResults = results.Where(r => r.ScanBatchCode == resultGroup.BatchCode).OrderBy(r => r.Elapsed);
int batchposition = 1;
foreach (Result batchedresult in BatchOfResults)
{
batchedresult.OverallPosition = batchposition;
resultGroup.Add(batchedresult);
batchposition = batchposition + 1;
}
grouped.Add(resultGroup);
}
}
Results = sortedresults;
OnPropertyChanged("Invalid");
}
I have a linkedlist class like follows:
public class DbNode<T>
{
private T _data;
private DbNode<T> _prev;
private DbNode<T> _next;
public T Data
{
get { return this._data; }
set { this._data = value; }
}
public DbNode<T> Prev
{
get { return this._prev; }
set { this._prev = value; }
}
public DbNode<T> Next
{
get { return this._next; }
set { this._next = value; }
}
public DbNode(T data, DbNode<T> prev, DbNode<T> next)
{
this._data = data;
this._prev = prev;
this._next = next;
}
public DbNode(T data, DbNode<T> prev)
{
this._data = data;
this._prev = prev;
this._next = null;
}
public DbNode(DbNode<T> next)
{
this._data = default(T);
this._next = next;
this._prev = null;
}
public DbNode(T data)
{
this._data = data;
this._prev = null;
this._next = null;
}
public DbNode()
{
this._data = default(T);
this._prev = null;
this._next = null;
}
}
public class DbLinkedList<T>
{
private DbNode<T> _head;
public DbNode<T> Head
{
get { return this._head; }
set { this._head = value; }
}
public DbLinkedList()
{
Head = null;
}
public T this[int index]
{
get
{
return this.GetItemAt(index);
}
}
public bool IsEmpty()
{
return Head == null;
}
public T GetItemAt(int i)
{
if (IsEmpty())
{
Console.WriteLine("The double linked list is empty.");
return default(T);
}
DbNode<T> p = new DbNode<T>();
p = Head;
if (0 == i)
{
return p.Data;
}
int j = 0;
while (p.Next != null && j < i)
{
j++;
p = p.Next;
}
if (j == i)
{
return p.Data;
}
else
{
Console.WriteLine("The node dose not exist.");
return default(T);
}
}
public int Count()
{
DbNode<T> p = Head;
int length = 0;
while (p != null)
{
length++;
p = p.Next;
}
return length;
}
public void Clear()
{
this.Head = null;
}
public void AddAfter(T item, int i)
{
if (IsEmpty() || i < 0)
{
Console.WriteLine("The double linked list is empty or the position is uncorrect.");
return;
}
if (0 == i)
{
DbNode<T> newNode = new DbNode<T>(item);
newNode.Next = Head.Next;
Head.Next.Prev = newNode;
Head.Next = newNode;
newNode.Prev = Head;
return;
}
DbNode<T> p = Head;
int j = 0;
while (p != null && j < i)
{
p = p.Next;
j++;
}
if (j == i)
{
DbNode<T> newNode = new DbNode<T>(item);
newNode.Next = p.Next;
if (p.Next != null)
{
p.Next.Prev = newNode;
}
newNode.Prev = p;
p.Next = newNode;
}
else
{
Console.WriteLine("The position is uncorrect.");
}
}
public void AddBefore(T item, int i)
{
if (IsEmpty() || i < 0)
{
Console.WriteLine("The double linked list is empty or the position is uncorrect.");
return;
}
if (0 == i)
{
DbNode<T> newNode = new DbNode<T>(item);
newNode.Next = Head;
Head.Prev = newNode;
Head = newNode;
return;
}
DbNode<T> n = Head;
DbNode<T> d = new DbNode<T>();
int j = 0;
while (n.Next != null && j < i)
{
d = n;
n = n.Next;
j++;
}
if (n.Next == null)
{
DbNode<T> newNode = new DbNode<T>(item);
n.Next = newNode;
newNode.Prev = n;
newNode.Next = null;
}
else
{
if (j == i)
{
DbNode<T> newNode = new DbNode<T>(item);
d.Next = newNode;
newNode.Prev = d;
newNode.Next = n;
n.Prev = newNode;
}
}
}
public void AddLast(T item)
{
DbNode<T> newNode = new DbNode<T>(item);
DbNode<T> p = new DbNode<T>();
if (Head == null)
{
Head = newNode;
return;
}
p = Head;
while (p.Next != null)
{
p = p.Next;
}
p.Next = newNode;
newNode.Prev = p;
}
public T RemoveAt(int i)
{
if (IsEmpty() || i < 0)
{
Console.WriteLine("The double linked list is empty or the position is uncorrect.");
return default(T);
}
DbNode<T> q = new DbNode<T>();
if (0 == i)
{
q = Head;
Head = Head.Next;
Head.Prev = null;
return q.Data;
}
DbNode<T> p = Head;
int j = 0;
while (p.Next != null && j < i)
{
j++;
q = p;
p = p.Next;
}
if (i == j)
{
p.Next.Prev = q;
q.Next = p.Next;
return p.Data;
}
else
{
Console.WriteLine("The position is uncorrect.");
return default(T);
}
}
public int IndexOf(T value)
{
if (IsEmpty())
{
Console.WriteLine("The list is empty.");
return -1;
}
DbNode<T> p = new DbNode<T>();
p = Head;
int i = 0;
while (p.Next != null && !p.Data.Equals(value))
{
p = p.Next;
i++;
}
return i;
}
public void Reverse()
{
DbLinkedList<T> tmpList = new DbLinkedList<T>();
DbNode<T> p = this.Head;
tmpList.Head = new DbNode<T>(p.Data);
p = p.Next;
while (p != null)
{
tmpList.AddBefore(p.Data, 0);
p = p.Next;
}
this.Head = tmpList.Head;
tmpList = null;
}
public string ReverseByPrev()
{
DbNode<T> tail = GetNodeAt(Count() - 1);
StringBuilder sb = new StringBuilder();
sb.Append(tail.Data.ToString() + ",");
while (tail.Prev != null)
{
sb.Append(tail.Prev.Data + ",");
tail = tail.Prev;
}
return sb.ToString().TrimEnd(',');
}
private DbNode<T> GetNodeAt(int i)
{
if (IsEmpty())
{
Console.WriteLine("The list is empty.");
return null;
}
DbNode<T> p = new DbNode<T>();
p = this.Head;
if (0 == i)
{
return p;
}
int j = 0;
while (p.Next != null && j < i)
{
j++;
p = p.Next;
}
if (j == i)
{
return p;
}
else
{
Console.WriteLine("The node does not exist.");
return null;
}
}
public T Fisrt()
{
return this.GetItemAt(0);
}
public T Last()
{
return this.GetItemAt(this.Count() - 1);
}
}
And I alse have a class as Product which contains properties:ProductID and ProductValue:
public class Product
{
private byte _productID;
public byte ProductID
{
get { return _productID; }
set
{
_productID = value;
NotifyPropertyChanged("ProductID");
}
}
private UInt16 _productValue;
public UInt16 ProductValue
{
get { return _productValue; }
set
{
_productValue = value;
NotifyPropertyChanged("ProductValue");
}
}
}
I have another listbox to which I want to add the Product when I click the TreeView:
MyLinkList<Product> myLinkList = new MyLinkList<Product>();
private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
var item = this.TreeView.SelectedItem as Product;
listbox.Items.Add(item);
myLinkList.Append(item);
}
Now the problem is I want to modify the item's property selected to add based on the former one. For example: if the former one's ProductID is 1, then this SelectedItem.ProductValue = FormerItem.ProductValue + 1, how I suppose to do this? Thanks!
I want to randomly generate trees (not BST) for unit testing of my code. I have tried it in a number of ways but somehow after generation of 3 -4 trees there is an exception or code goes into infinite loop. I am using random numbers for edges and node values.
I have also tried queue approach where I fill up the queue with random numbers and then dequeue nodes and randomly select the old node from the existing items in the queue and then connect this new node.
Does anybody know how to do this in C# in more better and succinct way?
EDIT
public class Tree
{
public Node Root { get; private set; }
public readonly Dictionary<long, Node> Nodes = new Dictionary<long, Node>();
public readonly Dictionary<string, long> SumDictionary = new Dictionary<string, long>();
public readonly Dictionary<long, long> FDictionary = new Dictionary<long, long>
{
{0, 1},
{1, 1}
};
public long Query(long n)
{
long s = 0;
Dfs();
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
var lvalue = GetLValue(i, j);
if (i != j)
{
lvalue *= 2;
}
s += lvalue;
if (s >= Mod)
{
s %= Mod;
}
}
}
return s;
}
private long GetLValue(int a, int b)
{
var key = string.Format("{0}-{1}", a, b);
if (SumDictionary.ContainsKey(key))
{
return Fvalue(SumDictionary[key]);
}
var aKey = string.Format("{0}-{1}", 1, a);
var bKey = string.Format("{0}-{1}", 1, b);
var sumA = SumDictionary[aKey];
var sumB = SumDictionary[bKey];
var lca = Lca(a, b);
if (lca != a && lca != b)
{
return Fvalue(sumA + sumB - SumDictionary[string.Format("{0}-{1}", lca, lca)]);
}
long bigSum, smallSum;
if (sumA > sumB)
{
bigSum = sumA;
smallSum = sumB;
}
else
{
bigSum = sumB;
smallSum = sumA;
}
var sumAtoB = bigSum - (smallSum - Nodes[smallSum].Value);
return Fvalue(sumAtoB);
}
public void AddNode(long a, long b)
{
Node nodea, nodeb;
if (Nodes.ContainsKey(a))
{
nodea = Nodes[a];
}
else
{
nodea = new Node { Value = a };
Nodes.Add(a, nodea);
}
if (Nodes.ContainsKey(b))
{
nodeb = Nodes[b];
}
else
{
nodeb = new Node { Value = b };
Nodes.Add(b, nodeb);
}
nodea.ReachableNodes.Add(b, nodeb);
nodeb.ReachableNodes.Add(a, nodea);
if (Root == null)
{
Root = nodea;
}
}
public long? Lca(int a, int b)
{
bool found = false;
return TraverseForLca(Root, null, a, b, ref found);
}
private long? TraverseForLca(Node node, Node prev, long a, long b, ref bool found)
{
if (node == null)
{
return null;
}
if (node.Value == a)
{
return a;
}
if (node.Value == b)
{
return b;
}
long? f = null;
foreach (KeyValuePair<long, Node> reachableNode in node.ReachableNodes)
{
var n = reachableNode.Value;
if (prev != null && n.Value == prev.Value)
{
continue;
}
long? lca = TraverseForLca(n, node, a, b, ref found);
if (found)
{
return lca;
}
if (lca != null && f == null)
{
f = lca;
}
else if (lca != null)
{
found = true;
return node.Value;
}
}
return f;
}
public void Dfs()
{
TravelForDfs(Root, null, 0);
}
private void TravelForDfs(Node node, Node prev, long recSum)
{
if (node == null)
{
return;
}
var key = string.Format("{0}-{1}", prev != null ? prev.Value : node.Value, node.Value);
var iKey = string.Format("{0}-{1}", node.Value, node.Value);
var weight = node.Weight;
if (weight >= Mod)
{
weight %= Mod;
}
if (!SumDictionary.ContainsKey(iKey))
{
SumDictionary.Add(iKey, weight);
}
weight = recSum + node.Weight;
if (weight >= Mod)
{
weight %= Mod;
}
foreach (KeyValuePair<long, Node> reachableNode in node.ReachableNodes)
{
var n = reachableNode.Value;
if (prev != null && n.Value == prev.Value)
{
if (!SumDictionary.ContainsKey(key))
{
SumDictionary.Add(key, weight);
}
continue;
}
if (!SumDictionary.ContainsKey(key))
{
SumDictionary.Add(key, weight);
}
TravelForDfs(n, node, weight);
}
}
public long Fvalue(long n)
{
if (n == 1 || n == 0)
{
return 1;
}
long a, b;
if (FDictionary.ContainsKey(n - 1))
{
a = FDictionary[n - 1];
}
else
{
a = Fvalue(n - 1);
if (!FDictionary.ContainsKey(n - 1))
{
FDictionary.Add(n - 1, a);
}
}
if (FDictionary.ContainsKey(n - 2))
{
b = FDictionary[n - 2];
}
else
{
b = Fvalue(n - 2);
if (!FDictionary.ContainsKey(n - 2))
{
FDictionary.Add(n - 2, b);
}
}
if (!FDictionary.ContainsKey(n))
{
FDictionary.Add(n, a + b);
}
var s = a + b;
if (s >= Mod)
{
s %= Mod;
}
return s;
}
}
It should just generate a tree
Does this tree meet your requirements?
public class TreeNode<T>
{
public T Value { get; set; }
public List<TreeNode<T>> Childs { get; }
public TreeNode()
{
Childs = new List<TreeNode<T>>();
}
}
public class TreeGenerator
{
private readonly int maxChilds;
private readonly Random rnd = new Random();
public TreeGenerator(int maxChilds)
{
this.maxChilds = maxChilds;
}
public TreeNode<T> CreateTree<T>(int maxDepth, Func<T> valueGenerator)
{
var node = new TreeNode<T>();
node.Value = valueGenerator();
if (maxDepth > 0)
{
var childsCount = rnd.Next(maxChilds);
for (var i = 0; i < childsCount; ++i)
node.Childs.Add(CreateTree(maxDepth - 1, valueGenerator));
}
return node;
}
public static void Demo()
{
var rnd = new Random();
var generator = new TreeGenerator(3 /* max childs count*/);
var tree = generator.CreateTree(4 /*max depth*/, () => rnd.Next() /*node value*/);
}
}
I am trying to write a List<double[]> into a csv file. Based on my experiments it is not possible using WriteRecords() directly, so I am looking for alternatives.
I cannot create a class to map this data because the number of elements in the arrays varies from execution to execution, and ranges from 1 to 75.
I have tried to go through each element of the array manually with WriteField() but it is really slow., and I have thousands of elements.
Is there anyway to accomplish this fast enough?
Thanks.
Edit:
Code
class ExportData
{
private Dictionary<int, Dictionary<SensorT, ListIndex>> _dict_sensorN;
private List<double[]> _values_list;
private int _current_max_Count=-1;
private static int _max_Count = 3000;
int n_columns = 0;
private CsvWriter csv;
public ExportData(List<SensorControl> sensorsUC)
{
//We create a dictionary with two keys that will store all the info from the sensors
_dict_sensorN = new Dictionary<int, Dictionary<SensorT, ListIndex>>();
foreach (SensorControl SensorC in sensorsUC)
{
Dictionary<SensorT, ListIndex> dict_sensorM = new Dictionary<SensorT, ListIndex>();
if (SensorC.emg)
{
ListIndex Index = new ListIndex();
Index.Column = n_columns;
Index.Row = 0;
dict_sensorM.Add(SensorT.EMG, Index);
n_columns++;
}
if (SensorC.triggers)
{
ListIndex Index = new ListIndex();
Index.Column = n_columns;
Index.Row = 0;
dict_sensorM.Add(SensorT.Trigger1, Index);
n_columns++;
Index.Column = n_columns;
Index.Row = 0;
dict_sensorM.Add(SensorT.Trigger2, Index);
n_columns++;
}
if (SensorC.acc)
{
ListIndex Index = new ListIndex();
Index.Column = n_columns;
Index.Row = 0;
dict_sensorM.Add(SensorT.ACC, Index);
n_columns++;
}
_dict_sensorN.Add(SensorC.sensorNumber, dict_sensorM);
}
//Initialize the array
_values_list = new List<double[]>();
//Initialize the document
DateTime currentDate = DateTime.Now;
string fileName = "exp_" + currentDate.ToString("yyyy-dd-M--HH-mm-ss") + ".csv";
try
{
var textWriter = new StreamWriter(fileName);
csv = new CsvWriter(textWriter);
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot create file.");
return;
}
}
public void AddToBuffer(SensorPoint sp)
{
Dictionary<SensorT, ListIndex> dict_sensorM = new Dictionary<SensorT, ListIndex>();
ListIndex Index = new ListIndex();
if (_dict_sensorN.TryGetValue(sp.ID, out dict_sensorM))
{
SensorT type = ToSensorT(sp.SensorType, sp.Battery);
if(type == SensorT.Trigger1) {
if (dict_sensorM.TryGetValue(type, out Index))
{
if (_current_max_Count < Index.Row)
{
_current_max_Count = Index.Row + 1;
_values_list[Index.Row] = new double[n_columns];
}
_values_list[Index.Row][Index.Column] =sp.Trigger1_int;
Index.Row++;
}
if (dict_sensorM.TryGetValue(SensorT.Trigger2, out Index))
{
if (_current_max_Count < Index.Row)
{
_current_max_Count = Index.Row + 1;
_values_list[Index.Row] = new double[n_columns];
}
_values_list[Index.Row][Index.Column] = sp.Trigger2_int_pos;
Index.Row++;
}
}
else {
if (dict_sensorM.TryGetValue(type, out Index))
{
if (_current_max_Count < Index.Row)
{
_current_max_Count = Index.Row;
_values_list.Add(new double[n_columns]);
}
_values_list[Index.Row][Index.Column] = sp.YPoint;
Index.Row++;
}
}
}
if (_current_max_Count > _max_Count) AddToFile();
}
private void AddToFile() {
csv.WriteRecords(_values_list);
}
private SensorT ToSensorT(SensorType stype, int battery)
{
if (stype == SensorType.EMG)
{
return SensorT.EMG;
}
else if (stype == SensorType.EMG_Trigger )
{
if (battery < 0) return SensorT.Trigger1;
else return SensorT.EMG;
}
else if (stype == SensorType.ACC)
{
return SensorT.ACC;
}
else {
return SensorT.Unknown;
}
}
}
/// <summary>
/// In order to create a multivalue dictionary it is necessary another class
/// </summary>
class ListIndex {
public int Column { get; set; }
public int Row { get; set; }
}
}
In my application, i need to show made calls to the user. The user can arrange some filters, according to what they want to see. The problem is that i find it quite hard to filter the calls without losing performance. This is what i am using now :
private void ProcessFilterChoice()
{
_filteredCalls = ServiceConnector.ServiceConnector.SingletonServiceConnector.Proxy.GetAllCalls().ToList();
if (cboOutgoingIncoming.SelectedIndex > -1)
GetFilterPartOutgoingIncoming();
if (cboInternExtern.SelectedIndex > -1)
GetFilterPartInternExtern();
if (cboDateFilter.SelectedIndex > -1)
GetFilteredCallsByDate();
wbPdf.Source = null;
btnPrint.Content = "Pdf preview";
}
private void GetFilterPartOutgoingIncoming()
{
if (cboOutgoingIncoming.SelectedItem.ToString().Equals("Outgoing"))
for (int i = _filteredCalls.Count - 1; i > -1; i--)
{
if (_filteredCalls[i].Caller.E164.Length > 4 || _filteredCalls[i].Caller.E164.Equals("0"))
_filteredCalls.RemoveAt(i);
}
else if (cboOutgoingIncoming.SelectedItem.ToString().Equals("Incoming"))
for (int i = _filteredCalls.Count - 1; i > -1; i--)
{
if (_filteredCalls[i].Called.E164.Length > 4 || _filteredCalls[i].Called.E164.Equals("0"))
_filteredCalls.RemoveAt(i);
}
}
private void GetFilterPartInternExtern()
{
if (cboInternExtern.SelectedItem.ToString().Equals("Intern"))
for (int i = _filteredCalls.Count - 1; i > -1; i--)
{
if (_filteredCalls[i].Called.E164.Length > 4 || _filteredCalls[i].Caller.E164.Length > 4 || _filteredCalls[i].Caller.E164.Equals("0"))
_filteredCalls.RemoveAt(i);
}
else if (cboInternExtern.SelectedItem.ToString().Equals("Extern"))
for (int i = _filteredCalls.Count - 1; i > -1; i--)
{
if ((_filteredCalls[i].Called.E164.Length < 5 && _filteredCalls[i].Caller.E164.Length < 5) || _filteredCalls[i].Called.E164.Equals("0"))
_filteredCalls.RemoveAt(i);
}
}
private void GetFilteredCallsByDate()
{
DateTime period = DateTime.Now;
switch (cboDateFilter.SelectedItem.ToString())
{
case "Today":
period = DateTime.Today;
break;
case "Last week":
period = DateTime.Today.Subtract(new TimeSpan(7, 0, 0, 0));
break;
case "Last month":
period = DateTime.Today.AddMonths(-1);
break;
case "Last year":
period = DateTime.Today.AddYears(-1);
break;
default:
return;
}
for (int i = _filteredCalls.Count - 1; i > -1; i--)
{
if (_filteredCalls[i].Start < period)
_filteredCalls.RemoveAt(i);
}
}
_filtered calls is a list of "calls". Calls is a class that looks like this :
[DataContract]
public class Call
{
private User caller, called;
private DateTime start, end;
private string conferenceId;
private int id;
private bool isNew = false;
[DataMember]
public bool IsNew
{
get { return isNew; }
set { isNew = value; }
}
[DataMember]
public int Id
{
get { return id; }
set { id = value; }
}
[DataMember]
public string ConferenceId
{
get { return conferenceId; }
set { conferenceId = value; }
}
[DataMember]
public DateTime End
{
get { return end; }
set { end = value; }
}
[DataMember]
public DateTime Start
{
get { return start; }
set { start = value; }
}
[DataMember]
public User Called
{
get { return called; }
set { called = value; }
}
[DataMember]
public User Caller
{
get { return caller; }
set { caller = value; }
}
Can anyone direct me to a better solution or make some suggestions.
Try using a foreach loop instead of an explicit for loop with an iterator variable. If _filteredCalls is a List, then _filteredCalls[i] will take O(n) time. Using a foreach will use the built in iterator of the list and will likely get you a speed increase.
Edit: I see you are actually looping backward through _filteredCalls, and also modifying the list.
So something like this would be better:
_filteredCalls = _filteredCalls.Where(c => c.Caller.E164.Length > 4 || c.Caller.E164.Equals("0"))