|
Probably the quick/simple in-memory data structure would be a Dictionary< string, List< int>>. Dictionarys are a hashtable, so lookups are log-n. I'm assuming from your example that there is a small number of ints.
As for parsing the file, I'd ignore the other suggestions to use regexs if the file is large. Its said that when a man is faced with a problem and decides to solve it with a regex, then he now has two problems Just string.Split on the space - it'll be orders of magnitude faster than the regex.
|
|
|
|
|
Right answer, but hash-table lookups are O(1).
|
|
|
|
|
Ooops. Good spot
Both insertion and retrieval should be close to O(1). However the default size of the Dictionary class is 3 - and increasing the capacity is O(n), which is done by increasing the capacity to the next prime number (not sure on the reasoning behind that). This shouldn't affect the use of the Dictionary for this task - we always pick suitable initialization sizes for our collections right guys?
|
|
|
|
|
duta wrote: I'm very new to C# but I have some experience in C++. There a struct array was an alternative but I don't know how to do that in C#.
A struct in C++ is a different thing from a struct in C#. In C++ a struct is a definition for either an object or a value type, but in C# a struct is always a value type.
To efficiently search the data, you should use a data structure like Dictionary<string, List<int>> .
Something like:
Dictionary<string, List<int>> items = new Dictionary<string, List<int>>();
foreach (string line in File.ReadAllLines(pathName)) {
string[] data = line.Split('\t');
List<int> list;
if (!items.TryGetValue(data[0], out list) {
list = new List<int>();
items.Add(data[0], list);
}
list.Add(int.Parse(data[2]));
}
List<int> aaabb = items["aaabb"];
aaabb.Sort();
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Personally I would read the data into a datatable using CSVtoTable[^] and then use either linq or a dataview to manupulate the data.
This is not the "fastest" method as the datatable carries a fair overhead.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Good Afternoon All
Happy Sunday
I have the Following SP, that keeps track of the users Role in my ASP.NET app.
Create Proc Roles
(
@Username varchar(55),
@Role int OUTPUT
)
as
set @Role = (Select User_Type from dbo.Logins
where Username = @Username)
Select Case @Role
when 1 then 1
when 2 then 2
else
0
End
Now this SP will only Bring Back 1,2 or 0
when i test it from like this
declare @role int
exec dbo.get_role 'dfdf',@Role out
its Fine, now lets go to my Problem. Am using it in my BLL and DAL like this ,
DAL
int Results;
con = new SqlConnection(strcon);
cmdselect = new sqlcommand();
cmdselect.CommandText= "dbo.Get_Role";
cmdselect.CommandType = CommandType.StoredProcedure;
cmdselect.Connection = con;
cmdselect.Parameter.Add("@Username,SqlDBType.Varchar,55).Value= Username;
cmdselect.Parameters.Add("@Role",SqlDbType.Int);
cmdselect.Parameters["@Role"].Direction= ParameterDirection.OutPut;
try
{
Con.Open();
cmdselect.ExecuteNonQuery();
Results = (int) cmdselect.Parameters["@Role"].Value;}
catch(SqlException)
{
throw;
}
Finally
{
con.Close();
}
return Results;
}
The Problem is in the Casting bolded part. My SP Returns "0", if there is no Role Found.
i get this Error on my App
Specified cast is not Valid
What is Wrong
Thanks
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
|
Well, int is an alias for System.Int32, so that's what he's using now...
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
You're right. Didn't notice the declaration
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Vuyiswa Maseko wrote: My SP Returns "0", if there is no Role Found.
Yes, that is what it returns in the result, but you throw away the result.
What you are using is the output parameter @Role, and that does not return zero if there is no role found, it returns null.
There are three possible solutions for you:
1. Use the result instead of the parameter.
2. Change the stored procedure so that the parameter gets the value zero if no role was found.
3. Check if the parameter value is DbNull before you try to cast it.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thank you very much , i took the 3rd Option
I have changed it to this and it works fine
if(System.Convert.IsDBNull(cmdselect.Parameters["@Role"].Value))
{
Results = 0;
}
else
{
Results = (int) cmdselect.Parameters["@Role"].Value;
}
Thank you it worked.
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
Ok, then you can remove the last half of your stored procedure.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks i did
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
|
Fer christ's sake, dude, try google!
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
I did it , I couldn't find anything special
|
|
|
|
|
We don't do homework. Because, if we did, you would not learn and then we'd have to teach you on the job some day, or just do your work to meet our deadlines.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
I have a test and I'd like to learn it just for my test.
Thanks anyway.
|
|
|
|
|
Christian Graus wrote: We don't do homework. Because, if we did, you would not learn and then we'd have to teach you on the job some day, or just do your work to meet our deadlines.
What a drag and waste of time that would be
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
i have written the following code to databind a pictureBox control to Northwind database(Employees table.But the photos looks hazy(not distinct). Any idea to solve this issue?
----------------------------
string connString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";
string data = "Select * from Employees";
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter da = new SqlDataAdapter(data, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Employees");
pictureBox1.DataBindings.Add("EditValue", ds, "Employees.Photo");
dataNavigator1.DataMember = "Employees";
dataNavigator1.DataSource = ds;
-----------------------------
I can navigate through the images and can see the safe-mode like quality of the images. Can anybody solve this issue?
thanaks
|
|
|
|
|
Hi all,
I'm kinda new in image processing. I now need to convert a 32bit depth image to 24bit depth image in order for a function to work which I got from AForge.Net API off Google. It only works on 24bit depth image, can I know if there's anyway I can convert a 32bits depth image I've got like 3 days left and I've researched online for samples but I can't seem to get anything to work. Thanks in advance! ^^
|
|
|
|
|
Have you tried FreeImage?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
32 and 24 bit are EXACTLY the same. Just create a new image at 24 bit, same size, create a Graphics object from that image, and then use DrawImageUnscaled to draw your 32 bit image into the graphics object ( thus converting it to 24 bit on the bitmap you created )
My image processing articles should give a good start to converting the long way, but what I just said is the easy way.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
I'm trying to make a function which returns a type-value that can be specified by the user, or determined by the type of object passed through it.
For example:
public (T) function(this (T) input)
{
}
where the user can specify what type of control is being passed through the function, and so the function will return a control of that type. Otherwise, perhaps where the function *determines* the type of control input is (ie, typeof) and returns of that type.
Is it possible? Thanks.
|
|
|
|
|
Epoque wrote: Is it possible?
Yes, it is. It's called Generics.
|
|
|
|