|
when you have three (or N) hor/ver lines inside a rectangle, they cut it in 8 (or 2^N) pieces, no matter what (assuming the lines are all different).
|
|
|
|
|
No they don't!
Rectangle:
+---+
| |
| |
| |
| |
| |
| |
| |
+---+
Add three lines (all different):
+---+
| |
|---|
| |
|---|
| |
|---|
| |
+---+
4 rectangles.
Go back to original and add three other lines (all different)
+---+
| | |
|-+-|
| | |
|-+-|
| | |
| | |
| | |
+-+-+
6 rectangles.
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
you're right of course, as your ASCII artwork makes abundantly clear.
So the algorithm needs a fix, something like: add any of {(7,0), (3,1), (1,3), (0,7)} hor/ver lines at random but different locations in the rectangle.
Homework: find all solutions for an arbitrary number of partitions.
|
|
|
|
|
I don't do homework.
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
refinements: disimpose a minimum width and height for each of them by limiting the range in (2) somewhat. everything as litter.
|
|
|
|
|
I am learning C# and OOP, i just cant understand how a static constructor can be useful... can someone please help me with this?? if possible, can u give a simple example? thank you very much...
Marcus Abrahão
Thank you all very much for your help
modified on Wednesday, January 6, 2010 7:12 AM
|
|
|
|
|
mabrahao wrote: static constructor can be useful
No such thing. static is usefull if you call function without intilizing class. If you intilize, constructor is called
public class Test
{
public Test(){}
public static Color GetColor()
{
return Color.White;
}
}
Color c = Test.GetColor();
|
|
|
|
|
Saksida Bojan wrote: mabrahao wrote:
static constructor can be useful
No such thing. static is usefull if you call function without intilizing class. If you intilize, constructor is called
This turns out not to be the case: Static Constructor[^]
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Thats good to know. I didn't even think it was possible
|
|
|
|
|
Ok, static methods and static class was able to figure out how it works, but not the STATIC CONSTRUCTOR, like so:
public class Test
{
int numTest;
// Static Constructor
static Test()
{
numTest = 5;
}
}
i am reading a book from Andrew Troelsen, about C#, and in the book, he gives a example of a static constructor, but i just could not understand...
anyway, thank u for u help
Marcus Abrahão
|
|
|
|
|
Saksida Bojan wrote: No such thing.
What are you talking about? Of course there is. You might find reviewing this[^] useful.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I didn't know it was even possible.
|
|
|
|
|
I wouldn't use the terminology "static constructor" (as ALL constructors are NECESSARILY static - you cannot invoke an instance method until you've constructed an intance). But there certainly is something that (in C#) uses the keyword "static" followed by same syntax as a constructor - the class initializer.
Class initializers can be used if you want to perform initialization of some kind once before a type can be used. For example, let's say you are making a bookmaker system. The prices (odds) may vary from 1.01 to 1000, but not everything in between is legal. Instead you have something like this:
From To Increment
1.01 2.00 0.01
2.02 4.00 0.02
4.04 10.00 0.05
....
500 1000 10
You could use an Int32 to store the integral number of 1/100 units, i.e. 125 for 1.25, and cover more than the range you need. But if there are only 256 legal prices you could store it in just a byte. This could make a big difference when tens of thousands of transactions per hour are to be handled, especially in terms of writing data to a database.
Anyway, it doesn't really matter for our purposes whether or not the representation is useful! The point is this is a good candidate for a type initializer. Given a byte value of 135, we need to find price number 135 of the legal prices. So we can equip the class with an array and initialize the type before it's used like this:
public class OddsPrice
{
static int[] priceArray;
static OddsPrice()
{
priceArray = new int[256];
int price = 100;
int i = 0;
while ((price += 1) <= 200) priceArray[i++] = price;
while ((price += 2) <= 400) priceArray[i++] = price;
...
}
byte value;
public int Value
{
get { return priceArray[value]; }
}
}
The type initializer will be called automatically at an undefined time (in current implementations of the CLR I believe it happens when the assembly is loaded, but the spec says undefined so don't assume anything) prior to the type being used at run-time.
The advantage is that you do not need to handle any thread-syncing to ensure it runs once and only once and before anything else on the type can be used.
|
|
|
|
|
Have a look on MSDN[^]
Hope this helps.
|
|
|
|
|
|
If you have a static class, or there is init for the class which must be done once per application (rather than done once for each instance of the class) then use a static constructor.
See here: Static Constructor[^]
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Thank you, this was helpful
Marcus Abrahão
|
|
|
|
|
Hi,
I have written a complex program in c#.net.
After the program is running for a long time by user interactions, its memory usage will increase to the limit of the system, then error will occur. How can the program alert the user that the memory usage is nearly close to its limit before it goes into error ?
I have tried memory garbage collection but the program is too complex that garbage collection cannot clean up the memory usage effectively.
Thanks
|
|
|
|
|
Kim0618 wrote: After the program is running for a long time by user interactions, its memory usage will increase to the limit
This is not good, I suspect that somewhere maybe stuff gets put into a collection of some kind and never removed.
I suggest getting a memory profiler to point out these flaws for you.
Hope you figure it out
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
Agreed. Don't understand why anyone would vote that a bad answer.
Something which just keeps growing over time needs reviewing.
Regards,
Rob Philpott.
|
|
|
|
|
You're probably not disposing of something properly, or you're not maintaining a collection of object properly, or any number of other causes.
You need to look at your app run with a profiler to find out what's going on.
This is something that you should have been testing for before it got deployed to production.
Kim0618 wrote: I have tried memory garbage collection but the program is too complex that garbage collection cannot clean up the memory usage effectively.
This has nothing to do with the GC's inability to do it's job. It can still do it's job very effectively, even on the most complex of applications.
|
|
|
|
|
There are many ways to run out of memory. The most obvious one is keeping things around that you no longer need but for some reason still hold on to.
.NET sometimes has a problem with the "large object heap" (LOH) which never gets compacted, and may run out of space. A popular culprit is a collection growing larger and larger. As soon as it hold 20,000+ objects, the reference array is a large object (as it exceeds 80KB); any time it needs more capacity, it gets reallocated with twice its size. The space occupied by the old array is freed, but will not be reused by that same array as it keeps growing.
Of course the objects themselves in the collection also take memory; that may or may not come from the LOH, depending on their size.
If, I don't know you do, but if you are building a cache of some sort, you should keep its size in check, and probably use WeakReference techniques.
|
|
|
|
|
Thanks for your answer !
But what is the weak reference technique that you have mentioned ?
|
|
|
|
|
You're welcome.
You might want and read up on WeakReference class.
|
|
|
|
|
Hi everyone im actually working on an application which migrates the datas that are present in a xml file to my database.. Im coding it on c#.net.. The issue is in my xml file we have customers name which is fetched from the xml file and then inserted into the database.. so most of my customers name have appostrophe(') in their name so during execution of the insert query it throws me odbc exception the insert query that i use is as follows:
insert into EACS_User_1 values('"+uf_Id+"','"+uf_Title+"','"+uf_FirstName+"','"+uf_MiddleName+"','"+uf_LastName+"','"+uf_PhoneNumber+"','"+uf_MobileNumber+"','"+uf_EmailID+"')
here the first name contains appostrophe say for example the name is Moh’d Husam how do i insert such names... Do guide me...
Thanx in advance..
Regards,
Tash
|
|
|
|