Code optimisation removing duplicates in VB - vb.net

I'm looking to optimise my code. Specifically this process
Calculate a group of locations (basically squares on a grid)
Have a list of all the locations that have been calculated
Then I go through all these locations, 1 at a time.
The issue I'm having is removing or not including duplicate locations in the list. I've tried having a list of integers (integers to represent the location) but it's still very slow. To give you an idea of the numbers: I'm talking at least 15,000 different location calculations and around 1,000,000 possible locations.
Any help on this would be much appreciated!

Here is how I remove duplicates from an string array, perhaps it will be of help to you:
Dim OneDimensionalTable(1000) As String
....
OneDimensionalTable = RemoveDuplicates(OneDimensionalTable)
.....
Private Function RemoveDuplicates(ByVal items As String()) As String()
Dim noDupsArrList As New ArrayList()
For i As Integer = 0 To items.Length - 1
If Not noDupsArrList.Contains(items(i).Trim()) Then
noDupsArrList.Add(items(i).Trim())
End If
Next
Dim uniqueItems As String() = New String(noDupsArrList.Count - 1) {}
noDupsArrList.CopyTo(uniqueItems)
Return uniqueItems
End Function

Related

inquiry Data from data table to datagridview with filters

i have datatable "dataitems" contain 98000 Rows And Datagridview have 11000 row
want to add the quantity form the datatable to the datagridview every item in it's row in the datagrid view
i am using this code but its take too too too much time to run and sometimes stop responding i need ideas to make it faster to run
Dim dt As New DataTable = najrndataset.dataitems
Dim Total As Integer
for x = 0 to datagridview1.rows.count -1
Dim b = datagridview1.rows(x).cells(1)
Dim c = 3
Total = (From r As DataRow In dt.AsEnumerable
Where r.Field(Of String)("Item_Number") = b And r.Field(Of Integer)("SOP_Type") = c
Select r.Field(Of Integer)("Quantity")).Sum
datagridview1.Rows(x).Cells(0).Value = Total
next
Hopefully you appreciate that if you have 11000 rows in a datagridview (terrible idea, by the way) and 98000 rows in a datatable, and you're running a loop that searches the table for each of the 11K items, and it does this by starting at number 1, and searching 98000 items for it, then going to 2 and searching 98000 items for it til you reach 11000 and searching 98000 items for it... At the end of the operation you're going to have performed 11000 x 98000 operations.. i.e. you're going to have performed 1.078 BILLION operations. This is why "its take too too too much time to run and sometimes stop responding" :)
You can speed this up by using a dictionary to track the sums, and it'll probably be fastest to index the 98000 items then set the dictionary from them:
Dim d as New Dictionary(Of String, Integer)
For Each di In najrndataset.dataitems.Where(Function(r) r.SOP_Type = 3)
If Not d.ContainsKey(di.Item_Number) Then
d(di.Item_Number) = di.Quantity
Else
d(di.Item_Number) += di.Quantity
End If
Next di
Then edit your grid (ugh; this isn't how datagridview are supposed to be used)
datagridview1.SuspendDrawing()
For x = 0 to datagridview1.rows.count - 1
Dim b = datagridview1.rows(x).cells(1)
Dim s As Integer
If d.TryGetValue(b, s) Then
datagridview1.rows(x).cells(0) = s
End If
Next x
datagridview1.ResumeDrawing()
You could also have a logic of "loop over the datagrid, putting 11000 items in the dictionary, loop over the datatable accumulating sums into the dictionary if the keys are present in the dictionary, loop over the datagriview putting the sums into the datagridview".
Not an answer, but too long for a traditional comment. I'm working to provide an answer separately.
I can't tell you how many times I've seen code like this:
Dim someVariable As New SomeObject
someVariable = someMethodToReturnSomeObject()
This pattern is bad code!
The New operator in the first line tells the compiler you want to allocate memory and run the constructor for your type. However, the next line assigns a new object... it throws away and forgets the memory and constructor work from the previous line. That's wasteful.
Instead, you want this:
Dim someVariable As SomeObject = someMethodToReturnSomeObject()
Worse, this speaks to a profound lack of understanding of the difference between a reference and an object, and what the computer is doing with your code in memory. This is a core principle of how many programming environments work. Code like the first example gives me big doubts of the capability of the programmer who writes it.

Why does ".Remove" affect all the items in a 2D structure assigned with a list?

I'm currently trying to create a Sudoku Solver, and on the step of assigning some possible values to a box that is not already preoccupied. (Bit of background info for why I'm doing this shebang: Sudoku is a number game based on a 9x9 grid, its contextual rules allow certain boxes in the grid that are not preoccupied to hold possible values during the process of solving )
To do this I created a structure, defined it as two dimensional, and populated it with a predefined list of integers using a for-loop.
Now when I tried to remove one integer from the list of a particular item in the two dimensional structure, I found out that all the lists of the items in the structure have had that integer removed. There's probably a simple solution to this, but I've been really struggling to find it. Hope the code below clarifies the somewhat confusing verbal explanation.
Structure Element
Dim PossibleValues As List(Of Integer)
Dim ElementValue As Integer
End Structure
Sub Main()
Dim List as New List(Of Integer)({1,2,3})
Dim TDP(8,8) as Element
For x as integer = 0 to 8
For y as integer = 0 to 8
TDP(x,y).PossibleValues = List
Next
Next
TDP(0,0).PossibleValues.Remove(1)
End Sub
Now I expect only TDP(0,0) would have a list of "2,3" when print out its list of integers, but when I check other items , i.e. TDP(1,0), its list is of integer is also "2,3"
Look at the assignment here:
TDP(x,y).PossibleValues = List
List(Of T) is a reference type, so this assigns a reference to the same List object to each of the array elements.
If you want each item to have it's own list of possible items, you need to either deep copy the list or create a new list:
Sub Main()
Dim TDP(8,8) as Element
For x as integer = 0 to 8
For y as integer = 0 to 8
TDP(x,y).PossibleValues = New List(Of Integer)({1,2,3})
Next
Next
TDP(0,0).PossibleValues.Remove(1)
End Sub

vb.net Pair Combinations to Create All Possible Sets

I need help figuring out how to go about programming this problem. I have an unknown number of pairs. Each pair is a Length x Width. I want to create sets of every possible combination of either a Length or Width from each pair. Here is an example of what I'm trying to do:
If I input 3 pairs, (1x2) (3x4) (5x6) I would get the following sets:
(1,3,5) (1,3,6) (1,4,5) (1,4,6) (2,3,5) (2,3,6) (2,4,5) (2,4,6)
So if I had 4 pairs, it would create a total of 16 sets, etc. I need to be able to input each pair and after all pairs have been entered, I need it to print out the sets. It can never include both numbers from a given pair in the same set. How do I create a loop or is there a built in math function that could produce all possible sets from a given number of pair inputs? I hope I described the problem well enough but if not, please ask questions. Thanks
This is called Cartesian product.
For example, if we have two sets A and B, such that
A = {1,2}
B = {3,4}
Then the result of the Cartesian product A x B is equal to
A x B = {(1,3),(1,4),(2,3),(2,4)}
If now we want to make the Cartesian product between the result obtained above and a new set, for example:
N = {5,6}
The result of the Cartesian product A x B x N, is equal to
A x B = {(1,3),(1,4),(2,3),(2,4)}
N = {5,6}
──────────────────────────────────────────────────
A x B x N = {(1,3,5),(1,3,6),(1,4,5),(1,4,6),(2,3,5),(2,3,6),(2,4,5),(2,4,6)}
Each element of the first set must be paired with each element of the second set.
I have developed 4 solutions to the Cartesian product:
Using a mathematical model, but without recursion. This solution
vectors using calculating for each combination number.
Using recursion, with the Collections class.
using the List (Of ...) class, also with recursion.
These three solutions seemed to me difficult to explain to you.
Furthermore, it is very difficult for me to explain my thoughts in English, because my native language is Castilian.
So I made the effort to create another solution that does not use recursion, which was more simple and friendly for any programmer.
Finally, I could create a satisfactory solution. Easy to understand and without recursion.
It is also very versatile. Any number of sets is accepted, as required, from 2 onwards.
You can also use any number of items. This depends on the requirements of each developer.
I hope this 4th. solution I devised, will please you, esteemed colleagues.
Only need a ListBox1 within Form4. Here it is:
Public Class Form4
Private Sub Form4_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'┌─────────── Temporary code for example ───────────┐
Dim Set_1 As List(Of String) = New List(Of String)
Dim Set_2 As List(Of String) = New List(Of String)
Dim Set_3 As List(Of String) = New List(Of String)
Set_1.Add("1")
Set_1.Add("2")
Set_2.Add("3")
Set_2.Add("4")
Set_3.Add("5")
Set_3.Add("6")
'└──────────────────────────────────────────────────┘
Dim Sets As List(Of Object) = New List(Of Object)
Sets.Add(Set_1)
Sets.Add(Set_2)
Sets.Add(Set_3)
Dim product As List(Of String) = Sets(0)
For i = 1 To Sets.Count - 1
product = CartesianProduct(product, Sets(i))
Next
For Each element As String In product
Me.ListBox1.Items.Add(element)
Next
End Sub
Private Function CartesianProduct(ByVal Set_A As List(Of String), ByVal Set_B As List(Of String)) As List(Of String)
Dim product As List(Of String) = New List(Of String)
For Each a As String In Set_A
For Each b As String In Set_B
product.Add(a & b)
Next
Next
Return product
End Function
End Class
Have a nice day! :)

Sum of elements (4) on mulitple lines in 2d array (txt file)

I have a text file that reads:
Left Behind,Lahaye,F,7,11.25
A Tale of Two Cities,Dickens,F,100,8.24
Hang a Thousand Trees with Ribbons,Rinaldi,F,30,16.79
Saffy's Angel,McKay,F,20,8.22
Each Little Bird that Sings,Wiles,F,10,7.70
Abiding in Christ,Murray,N,3,12.20
Bible Prophecy,Lahaye and Hindson,N,5,14.95
Captivating,Eldredge,N,12,16
Growing Deep in the Christian Life,Swindoll,N,11,19.95
Prayers that Heal the Heart,Virkler,N,4,12.00
Grow in Grace,Ferguson,N,3,11.95
The Good and Beautiful God,Smith,N,7,11.75
Victory Over the Darkness,Anderson,N,12,16
The last element of each line is a price. I would like to add up all the prices. I've been searching for so many hours now and cannot find a thing to answer my question. This seems soooo easy but I cannot figure it out!!! Please help out. BTW, this list is bound to change (adding of lines, deletion of lines, altering of lines) so if you can, please nothing concrete but instead leave the code open to changes. Thanks!!!
Just so you can see my pooooorrrr work, here is what I have (I think I deleted my code and rewrote a different way for several hours now.):
Dim Inv() As String = IO.File.ReadAllLines("Books.txt")
Dim t As Integer = Inv.Count - 1
Dim a As Integer = 0 to t
Dim sumtotal As String = sumtotal + Inv(4)
also,
for each line has either an "F" or an "N". how do I add up all the F's and all the N's. Do I do it via if statements?
First, you'll be better off using Double as your type instead of String. Second, observe how I use the Split function on each line, cast its last element as a double, and add it to the total. Yes, using an If Statement is how you can determine whether or not to add to the count of F or the count of N.
Dim lstAllLines As List(Of String) = IO.File.ReadAllLines("Books.txt").ToList()
Dim dblTotal As Double = 0.0
Dim intCountOfF As Integer = 0
Dim intCountOfN As Integer = 0
For Each strLine As String In lstAllLines
Dim lstCells As List(Of String) = strLine.Split(",").ToList()
dblTotal += CDbl(lstCells(3))
If lstCells(2) = "F" Then
intCountOfF += 1
Else
intCountOfN += 1
End If
Next

Reorder a list of String into a new list

I want to reorder a list of strings into a different list of strings. I am creating multiple loops to do this, however i was wondering if there was a better way to do this.
Dim values As New List(Of String)
For Each val As String In vals
If val.Contains("10") And val.Contains("Year 1") Then
values.Add(val)
End If
Next
For Each val As String In vals
If val.Contains("20") And val.Contains("Year 1") Then
values.Add(val)
End If
Next
There are going to be quite a lot of these loops to do what I want can anyone offer any help or a better way to go about doing this? Note that I want the values list to return 10,10,10,20,20,20 so that lines that have 10 should be added first and lines with 20 then afterwards.
thanks,
Stefan
If you want it to be ordered by the year, this will work:
Dim result = (From item In values
Where item.ToLower Like "*year #*"
Let year = Regex.Match(item.Substring(item.ToLower.IndexOf("year ")), "\d+").Value
Order By year).ToList()
But you've said that you want it to be ordered by 10,20,30 and so on. But i don't see how this should work.
The value could be 8,25,4711 instead and how do want to find it at all in the String?
Contains only checks whether the line contains this substring or not, but you might have this number also in the year part(f.e. year 10) or elsewhere.
So your requirement looks very error-prone and vague.
Edit: If the text is so strict as in your comment's samples, you could simplify it to this, splitting each line into tokens separated by comma:
Dim result = (From item In values
Let Tokens = item.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Let Count = Int32.Parse(Tokens(3))
Let Year = Int32.Parse(Regex.Match(Tokens(5), "\d+").Value)
Order By Count, Year
Select item).ToList()
Dim values As List(Of String) = vals.OrderBy(Function(s)
If s.Contains("Year 1") Then
If s.Contains("10") Then Return 0
If s.Contains("20") Then Return 1
End If
Return Integer.MaxValue
End Function).ToList()
It sounds like you may also want to create a regular expression for this string, to pull out the fields you care about. That should make this perform a lot better. To help you with the correct expression we'll need to know a lot more about what the strings look like.