1 registered members (monarch),
1,259
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: Hummel]
#444512
08/13/14 06:56
08/13/14 06:56
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
I feel like this is missing the width and height, no? It's not like you can't just assign things in C++ (you could actually do it by value :p), but according to the specs, the C# rect also has an origin and a size. Where's the size in your example coming from?
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: WretchedSid]
#444533
08/13/14 15:33
08/13/14 15:33
|
Joined: Mar 2006
Posts: 2,252
Hummel
Expert
|
Expert
Joined: Mar 2006
Posts: 2,252
|
dunno. getter/setter magic? there is a C#-rect?
Last edited by Hummel; 08/13/14 15:37.
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: Hummel]
#444538
08/13/14 17:06
08/13/14 17:06
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
So basically you are saying that you pulled that snippet out of your ass without understanding what mine does? It's not too hard to write a really cool snippet with LINQ that actually demonstrates some cool C# stuff. Just saying.
Come on, some one impress me with C#s expression engine and the lazy evaluation of it. Go!
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: Hummel]
#444540
08/13/14 17:41
08/13/14 17:41
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: WretchedSid]
#444541
08/13/14 17:52
08/13/14 17:52
|
Joined: Aug 2003
Posts: 7,439 Red Dwarf
Michael_Schwarz
Senior Expert
|
Senior Expert
Joined: Aug 2003
Posts: 7,439
Red Dwarf
|
Back on topic, I am sure this could have been done much, MUCH better. But since I am really terrible at maths, this is how I avoided loops for calculating the "index" (i.e. the n'th time it happened) of a weekly recurring appointment for a calendar app in javascript:
// Does it occur on target date?
if ((this.pattern.weekdays & p.date.get.dotw(h.target)))
{
// How many week has it been since this appointment started?
var weeksSinceStart = p.date.get.weeksBetween(h.current, h.target);
// Skip this week?
if (weeksSinceStart % this.pattern.periodicity)
{
return null;
}
// How many blind occurrences are there?
// e.g. appointment starts on a WED but the pattern is MO/_WED_/TH/SU = 1 blind (MO)
var blind_occurrences = 0;
// Avoid loop with clever switch
switch ((this.pattern.weekdays & p.date.get.dotw(this.start)))
{
// INTENTIONAL FALL-THROUGHS!!! //
case FLAG_SUNDAY:
(this.pattern.weekdays & FLAG_SATURDAY) && blind_occurrences++;
case FLAG_SATURDAY:
(this.pattern.weekdays & FLAG_FRIDAY) && blind_occurrences++;
case FLAG_FRIDAY:
(this.pattern.weekdays & FLAG_THURSDAY) && blind_occurrences++;
case FLAG_THURSDAY:
(this.pattern.weekdays & FLAG_WEDNESDAY) && blind_occurrences++;
case FLAG_WEDNESDAY:
(this.pattern.weekdays & FLAG_TUESDAY) && blind_occurrences++;
case FLAG_TUESDAY:
(this.pattern.weekdays & FLAG_MONDAY) && blind_occurrences++;
}
// How often has it already occurred *this* week?
var occurrenceIndexThisWeek = -blind_occurrences;
// Avoid loop with clever switch
switch ((this.pattern.weekdays & p.date.get.dotw(h.target)))
{
// INTENTIONAL FALL-THROUGHS!!! //
case FLAG_SUNDAY:
(this.pattern.weekdays & FLAG_SATURDAY) && occurrenceIndexThisWeek++;
case FLAG_SATURDAY:
(this.pattern.weekdays & FLAG_FRIDAY) && occurrenceIndexThisWeek++;
case FLAG_FRIDAY:
(this.pattern.weekdays & FLAG_THURSDAY) && occurrenceIndexThisWeek++;
case FLAG_THURSDAY:
(this.pattern.weekdays & FLAG_WEDNESDAY) && occurrenceIndexThisWeek++;
case FLAG_WEDNESDAY:
(this.pattern.weekdays & FLAG_TUESDAY) && occurrenceIndexThisWeek++;
case FLAG_TUESDAY:
(this.pattern.weekdays & FLAG_MONDAY) && occurrenceIndexThisWeek++;
}
// Set index
var index = (weeksSinceStart * this.occurrencesPerWeek / this.pattern.periodicity) + occurrenceIndexThisWeek;
Last edited by Michael_Schwarz; 08/13/14 17:53.
"Sometimes JCL reminds me of Notch, but more competent" ~ Kiyaku
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: Michael_Schwarz]
#444542
08/13/14 17:59
08/13/14 17:59
|
Joined: Apr 2005
Posts: 4,506 Germany
fogman
Expert
|
Expert
Joined: Apr 2005
Posts: 4,506
Germany
|
using System.Collections.Generic;
no science involved
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: fogman]
#444544
08/13/14 18:20
08/13/14 18:20
|
Joined: Nov 2008
Posts: 946
the_clown
OP
User
|
OP
User
Joined: Nov 2008
Posts: 946
|
@Sid, am I right to assume that the problem with statics only occurs with the use of shared libraries, but not with the use of static libraries? Also, I have no clue what your second snippet does. @Michael, I think I see what you did there, nice workaround. @fog, that's not a snippet.
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: the_clown]
#444548
08/13/14 21:29
08/13/14 21:29
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
@Sid, am I right to assume that the problem with statics only occurs with the use of shared libraries, but not with the use of static libraries? Yes. Also, I have no clue what your second snippet does. It provides a way to execute some code at startup or exit. The macros wrap a static object that is instantiated at startup (or when the library is loaded), and which is destroyed at the end, and depending on which macro is used, it calls a callback in either the constructor or destructor. It exploits the automatic object lifetime in C++. Similar, a scope guard, which can be used for automatic clean up (more code snippet):
namespace RN
{
class ScopeGuard
{
public:
template<typename F>
explicit ScopeGuard(F &&rollback) :
_committed(false),
_rollback(std::move(rollback))
{}
ScopeGuard(ScopeGuard &&other) :
_committed(other._committed),
_rollback(std::move(other._rollback))
{}
ScopeGuard &operator=(ScopeGuard &&other)
{
_rollback = std::move(other._rollback);
_committed = other._committed;
return *this;
}
~ScopeGuard()
{
if(!_committed)
_rollback();
}
void Commit()
{
_committed = true;
}
private:
bool _committed;
Function _rollback;
};
}
Example usage:
void foo()
{
FILE *file = fopen(...);
RN::ScopeGuard guard([&]() {
fclose(file); // Close the file descriptor
});
bar(file); // In this case, bar can throw an exception.
// Either way, exception or normal exit, once the scope guard goes out of scope, the file is closed.
}
PS: Don't use "FILE" in C++
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
|