|
go through the array and use a dictonary<object, int=""> where object is the array element (or some unique identifer for the items in your array) and int is the count of occurances. go through the array once and add items into the dictonary if it doesn't exist, or increment the count value (the dictonary is a hash so it will be really fast in finding the key/seeing if it exists). after going through the array once, iterate each KeyValuePair in the dictionary and look for any int values greater than 1. your complexity is about O(2n + 1).
|
|
|
|
|
Hi,
in pseudo-C#
List myList=empty;
foreach (element elem in inputcollection) {
if myList contains elem throw new FoundDuplicateException(elem)
else add elem to myList
}
|
|
|
|
|
Is this a homework question?
1. Sort the array but keep track of the original positions.
2. Set FOO to size.
3. Compare n with n+1 to find duplicates.
If the original positions of the duplicates are less than FOO,
set FOO the larger of the original locations. This assumes a number can be repeated once,
but it is not hard to modify for multiple occurrences.
4. If FOO == size, there are no duplicates
else FOO is the location of the first duplicate.
Step 1 is O(nlogn)
Step 3 is O(n)
|
|
|
|
|
Hi again
i am working on a C# windows program and i am trying to make some control just like what we have in OS or office 2003. but i can't find the color code that change with changing system default Theme. we have 3 default theme on XP, blue green and Gray, how can i paint my control or their background with for example the special blue gradient we have on background of Windows explorer search pane?
thanks a lot.
|
|
|
|
|
Good question I wondered about that too. My suggestion at this point it too download some of the samples here and see how others did it. It should be in there. My best guess is that the various Explorer bars and outlook 2003 bar implementations should contain just what you need.
WM.
What about weapons of mass-construction?
"What? Its an Apple MacBook Pro. They are sexy!" - Paul Watson
My blog
|
|
|
|
|
|
It depends on what you are trying to change. If you want to change the colors of the toolstrips, menustrips, and statusstrips, you will need to create your own renderer that derives from ToolStripProfessionalRenderer and your own color tables that derive from ProfessionalColorTable . You can then set the ToolStripManager.Renderer property to your renderer. You will need to change the RenderMode property on the toolstrip, menustrip, and statusstrip to ManagerRenderMode as well.
|
|
|
|
|
Hi,
I'm new to Threading..
After learning how to work with threading, I found out that I could use BackgroundWorker since I'm using VS 2005.
I also check on the example and try to do the same like following. However I couldn't click on Cancel Button after I click StartButton. I couldn't interrupt what my program is currenting doing.
I dont' know what I am doing it wrong. I couldnt' find the answer. Please help me. Thank you.
My form has StartButton, Cancel Button, Edit Text To Display the message
1) Add BackgroundWorker to my Form,
2) Set WorkerReportsProgress = False
3) WorkderSupportsCancella = True;
4) Add DoWork and RunWokerCompleted events
5) Add event to StartButton, and Cancel button.
6) Add functions:
Private void BrowseFileFolder(),that will browse the folder given and then get all file information.
private void BrowseFF(BackgroundWorker bk, DoWorkEventArgs e),
7) Add another function: public delegate void BrowseFFDelegate(); because i got error calling BrowseFileFolder() directly from DoWork().
8) OnStartButton() -> myBkWorker.RunWorkderAsync();
9)
<br />
OnMyBkWorker_DoWork(object sender, DoWorkEventArgs e) <br />
{<br />
BackgroundWorkder worker = sender as BackgroundWorkder;<br />
BrowseFF(worker,e);<br />
}<br />
10)
<br />
private void BroswerFF(BackgroundWorker bk, DoWorkEventArgs e)<br />
{<br />
BKWorker = worker;<br />
workerEvent = e;<br />
Invoke(new BrowseFFDelegate(BrowseFileFolder));<br />
}<br />
11)
<br />
private void BrowseFileFolder()<br />
{<br />
if(BKWorker.CancellationPending)<br />
{<br />
workerEvent.Cancel = true;<br />
return;<br />
}<br />
else<br />
{<br />
DirectoryInfo dirfolder = new DirectoryInfo(pathToBrowse);<br />
....<br />
....<br />
....<br />
}<br />
}
Thank you.
|
|
|
|
|
|
Thank you Mike,
I have Invoke Methods.. I do not have any problem updating any of my form controls
only that after I called the invoke method.. ( when the user click on Start Button)
I can no longer click on any of form buttons like Cancel Button.
I would like to enable user interrupting whatever the program is busy working after the Start Button got clicked.
Thanks.
|
|
|
|
|
pnpfriend wrote: I can no longer click on any of form buttons like Cancel Button.
Then you have something "blocking" or code executing in the main thread that you perhaps "think" is executing in a background thread.
|
|
|
|
|
I know.. there might be something is totally wrong on what I am doing. but i just couldnt' see it. Yes.. I believe my function is running the BackgroundWorker thread.. but looks like i am not.
therefore I posted my code at first place. Please refer to my first post.
I worte down exactly what I did..
I'm trying to get the file and folder information using recursive.
since there are lots of files to browse I would like to allow the user interrupts by clicking Cancel Button. However, I can no longer click any button once the program gets into that function.
I used Invoke method too.
So what am I missing? what am I doing it wrong??
Please guide me, point me out my error.
Thanks.
|
|
|
|
|
pnpfriend wrote: So what am I missing? what am I doing it wrong??
Sorry to say but your code is a mess and very difficult to tell what you are doing. Maybe... just maybe this line
Invoke(new BrowseFFDelegate(BrowseFileFolder));<br />
<br />
Is sending you back to the UI thread where you finally do all the directory scanning work and therefore you are not even using the worker thread to do the work.
|
|
|
|
|
Is it returning to the UI Thread?
Invoke(new BrowseIFFDelegate(BrowseFileFolder));
It is working now for me after I added
Application.DoEvent();
I am not so sure if it is in BK worker or not anymore
but I can now click on cancel button.
Thanks for your help mike.
|
|
|
|
|
pnpfriend wrote: I am not so sure if it is in BK worker or not anymore
That's not a good thing. If you are going to put threads in a production application you better know what they and you are doing.
|
|
|
|
|
How can i search for certain word in contents of a file? I did that using File.ReadAllLines() but this takes a lot of time. Is there anyway to do that?
Thanks!
|
|
|
|
|
Streams let you look at a small chunk at a time, which is ideal with reading large files. Problem is, most of the examples I have I'm just reading bytes into a buffer of fixed length. If you're searching for specific text, you'll have to work out a way to read whole words. Depends on what you need to do, but Streams are the way to speed things up, read dependably, and conserve resources. Search google and/or CodeProject articles for information on Streams.
|
|
|
|
|
I have to finish a project in 7 hours and i don't have time to spend with searching on Google. I'll be thankful if you can help me
|
|
|
|
|
Alright, the following is an example of using a Stream to read 0.25 kb at a time.
public void ReadStream(Stream input) {
if (input != null) {
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = input.Read(buffer, 0, Length);
while (bytesRead > 0) {
bytesRead = input.Read(buffer, 0, Length);
}
input.Close();
}
}
But only you know what you need to do with the data, and I don't have time to work out a way to use a variable length buffer to ensure whole words are viewed. I'll leave that to you. Besides, this is either a school project that you need to do to get any benifit out of, or it's a work project that I'm sure you're not planning on sending me a check for doing the work for you. So... Good luck.
|
|
|
|
|
As a novel approach, you could also get a copy of Coco/R and build a simple tokenizer that has streaming built it. Then you just have to ask it for the next token.
|
|
|
|
|
You can put the contents of each file in a HashTable and load the files in at startup or when the first search is being done. Then you can search for words at O(1). This uses up quite a bit of memory though, and you still have to read the file at least once.
Open the file with a stream and read a line at a time, then use string.Split(' ') to split it into individual words and put the words into your HashTable (along with some data-structure that records line num, word num, etc...)
|
|
|
|
|
Ok another simple one here because ive got the XML writing correctly . so as you can see below i have string Value = ""; and in various button events i have string Value = "1";
quite simply i want a way to get the string to be reused so when i click button 1 the value of string to change inside the try { function .
Any ideas ?
private void ChangeStatus(object sender, EventArgs e)
{
try
{
string Value = "";
//xml reading code
CurrentStatus.InnerText = Value;
// xml writing code...
}
catch (System.Exception)
{
}
}
private void 1(object sender, EventArgs e)
{
string Value = "1";
}
private void 2(object sender, EventArgs e)
{
string Value = "2";
}
private void 3(object sender, EventArgs e)
{
string Value = "3";
}
|
|
|
|
|
Umm... Stop setting it to "" before checking to see what's in there. If you explained it effectively. If you set it equal to "" and then check to see what's in there, unless Microsoft needs to go back and do some work, it should always be equal to "".
|
|
|
|
|
I get a compiler error if i use string Value;
Error 1 Use of unassigned local variable 'Value'
Im getting confused here between actionscriptand c# any ideas on what changes i need to make ?
|
|
|
|
|
Hi,
if you want a single variable used by several methods, make it a class member, and dont
define it again and again in every method; like so:
class myClass {
private string myString;
public void Method1() {
myString="value1";
}
public void Method2() {
if (myString=="value1") Console.WriteLine("Method1 has been executed !");
}
}
The way you did it, every method had its own local variable, but they were independent
of each other. Read up on "scope rules".
|
|
|
|