Why is '397' used for ReSharper GetHashCode override? - hash

Like many of you, I use ReSharper to speed up the development process. When you use it to override the equality members of a class, the code-gen it produces for GetHashCode() looks like:
public override int GetHashCode()
{
unchecked
{
int result = (Key != null ? Key.GetHashCode() : 0);
result = (result * 397) ^ (EditableProperty != null ? EditableProperty.GetHashCode() : 0);
result = (result * 397) ^ ObjectId;
return result;
}
}
Of course I have some of my own members in there, but what I am wanting to know is why 397?
EDIT: So my question would be better worded as, is there something 'special' about the 397 prime number outside of it being a prime number?

Probably because 397 is a prime of sufficient size to cause the result variable to overflow and mix the bits of the hash somewhat, providing a better distribution of hash codes. There's nothing particularly special about 397 that distinguishes it from other primes of the same magnitude.

The hash that resharper uses looks like a variant of the FNV hash. FNV is frequently implemented with different primes. There's a discussion on the appropriate choice of primes for FNV here.

Related

Can Sal annotate that parameter members may be mutated?

I am writing a reference-counted linked list of characters data structure in C for practice. I want to try using Sal in it to annotate function parameters for this practice.
I have an input paremeter(named This), which I want to annotate to make it clear that the specified parameter's members must be mutable in order for the function to behave as expected.
The situation is analogous to the code below.
#include <Windows.h>
typedef struct Box {
ULONG val;
} Box;
ULONG Box_decrement(_In_ Box *This) {
return InterlockedDecrement(&(This->val));
}
int main(int argc, char **argv) {
Box b = {2};
Box_decrement(&b);
return (BYTE)b.val;
};
Is there an existing Sal annotation that can be used to annotate the This parameter of the Box_increment function to make it clear from the function signature that the function modifies one or more members of the Box that has been passed to it?
Something like _InternallyMutable_(but exist):
#include <Windows.h>
typedef struct Box {
ULONG val;
} Box;
ULONG Box_decrement(_InternallyMutable_ _In_ Box *This) {
return InterlockedDecrement(&(This->val));
}
int main(int argc, char **argv) {
Box b = {2};
Box_decrement(&b);
return (BYTE)b.val;
};
Best solution so far(unfortunately, there does not seem to be any equivelent in SAL to denote Internally_mutable, there is Unchanged which is the opposite):
#include <Windows.h>
#define _Internally_mutable_(expr) _At_(expr, _Out_range_(!=, _Old_(expr)))
typedef struct Box {
ULONG val;
} Box;
ULONG Box_decrement(_In_ _InternallyMutable_(This) Box *This) {
return InterlockedDecrement(&(This->val));
}
int main(int argc, char **argv) {
Box b = {2};
Box_decrement(&b);
return (BYTE)b.val;
};
Yes! You can. SAL is a wonderful DSL that lets you do basically anything you want if you're psychic enough to infer it from the little bits in the Windows SDK. I've even in the past been able to write super simple custom annotations to detect invalid HANDLE usage with _Post_satisfies_ and friends.
This code seems to work:
_At_(value, _Out_range_(!=, _Old_(value)))
void change_value_supposed_to(int& value) noexcept {
//value += 1;
}
...Running with all native rules in code analysis, I get a warning like this:
Warning C28196 The requirement that '_Param_(1)!=(("pre"), _Param_(1))' is not satisfied. (The expression does not evaluate to true.)
(there, substitute value with your variable)
For _Internally_mutable_, I can do it in the "above the function" style of SAL:
#define _Internally_mutable_(expr) _At_(expr, _Out_range_(!=, _Old_(expr)))
_Internally_mutable_(value)
void change_value_supposed_to_internally_mutable(int& value) noexcept {
//value += 1;
(void)value;
}
...but not inline WITHOUT being repetitive, as you wanted. Not sure why right now - _Curr_ doesn't seem to be working? - I may need another layer of indirection or something. Here's what it looks like:
#define _Internally_mutable_inline_(value) _Out_range_(!=, _Old_(value))
void change_value_supposed_to_internally_mutable_inline(_Internally_mutable_inline_(value) int& value) noexcept {
//value += 1;
(void)value;
}
How I figured this out:
sal.h defines an _Unchanged_ annotation (despite doing web dev for several years now and little C++, I remembered this when I saw your question in a google alert for SAL!):
// annotation to express that a value (usually a field of a mutable class)
// is not changed by a function call
#define _Unchanged_(e) _SAL2_Source_(_Unchanged_, (e), _At_(e, _Post_equal_to_(_Old_(e)) _Const_))
...if you look at this macro closely, you'll see that it just substitutes as:
_At_(e, _Post_equal_to_(_Old_(e)) _Const_)
...and further unrolling it, you'll see _Post_equal_to_ is:
#define _Post_equal_to_(expr) _SAL2_Source_(_Post_equal_to_, (expr), _Out_range_(==, expr))
Do you see it? All it's doing is saying the _Out_range_ is equal to the expression you specify. _Out_range_ (and all the other range SAL macros) appear to accept all of the standard C operators. That behavior is not documented, but years of reading through the Windows SDK headers shows me it's intentional! Here, all we need to do is use the not equals operator with the _Old_ intrinsic, and the analyzer's solver should be able to figure it out!
_Unchanged_ itself is broken?
To my great confusion, _Unchanged_ itself seems broken:
_Unchanged_(value)
void change_value_not_supposed_to(_Inout_ int& value) noexcept {
value += 1;
}
...that produces NO warning. Without the _Inout_, code analysis is convinced that value is uninitialized on function entry. This makes no sense of course, and I'm calling this directly from main in the same file. Twiddling with inlining or link time code generation doesn't seem to help
I've played a lot with it, and various combinations of _Inout_, even _Post_satisfies_. I should file a bug, but I'm already distracted here, I'm supposed to be doing something else right now :)
Link back here if anybody does file a bug. I don't even know what the MSVC/Compiler teams use for bug reporting these days.
Fun facts
5-6 years ago I tried to convince Microsoft to open source the SAL patents! It would have been great, I would have implemented them in Clang, so we'd all be able to use it across platforms! I might have even kicked off a career in static-analysis with it. But alas, they didn't want to do it in the end. Open sourcing them would have meant they might have to support it and/or any extensions the community might have introduced, and I kinda understand why they didn't want that. It's a shame, I love SAL, and so do many others!

How do I use STL std::list with objects?

I want to create linked lists of objects sorted by an object attribute (physical size); but so far it seems I will have to code them myself...
These lists will be short, typically a dozen nodes each; but I may have up to a thousand lists; so I can't afford the extra weight of using std::map's. In fact, I'd be quite happy with single linked list.
But I need the node to be more than just a value.
The key value in my objects is rarely going to change; however elements will have to come out of one list and move to another quite often.
((Actual use: One list per quad, in a quad-tree (as for collision detection, etc); objects sorted by size, as the larger objects are less numerous but need to be picked quickly from larger ranges, so they should come up first in the lists.))
But every example I find for using std::list to maintain sorted lists uses a list of integers as the example; but that's not very useful; what I have is objects that have one value member to be sorted by.
I was thinking of using lower_bound to find the insertion point, then insert the object; but the lower_bound iterator takes a begin, and end, and a plain value as the third argument; I see no mechanism by which I can specify to use a particular member of my objects to sort by.
I could, of course, define a conversion operator,
my_object_type::int(){ return sortby; }
Would that work? Is there a better way?
I seem to have found my answer in this reference:
https://www.geeksforgeeks.org/lower_bound-in-cpp/
under "Syntax 2"; there is provision for a fourth
argument being a comparison functor. So, something
like this should work (not tested yet):
class pnt_proxy
{
int x; //position
int y;
point* real_pnt; //the real point this represents
public:
float sz; //rough largest diagonal across object
}
class pproxy_cmp : public std::binary_function< pnt_proxy, pnt_proxy, bool >
{
public:
bool operator()( pnt_proxy const & a, pnt_proxy const & b ) const
{
return a.sz < b.sz;
}
};
std::list< pnt_proxy > ll;
void insert_sorted( pnt_proxy const & pp )
{
if( ll.size() )
{
std::list<pnt_proxy>::iterator insert_at;
insert_at =
std::lower_bound( ll.begin(), ll.end(), pp, pproxy_cmp() );
ll.insert( insert_at, pp );
}
else ll.push_back( pp );
}

How to implement non chronological backtracking

I'm working on a CDCL SAT-Solver. I don't know how to implement non-chronological backtracking. Is this even possible with recursion or is it only possible in a iterative approach.
Actually what i have done jet is implemented a DPLL Solver which works with recursion. The great differnece from DPLL and CDCL ist that the backracking in the tree is not chronological. Is it even possible to implement something like this with recursion. In my opionion i have two choices in the node of the binary-decision-tree if one of to path leads i a conlict:
I try the other path -> but then it would be the same like the DPLL, means a chronological backtracking
I return: But then i will never come back to this node.
So am i missing here something. Could it be that the only option is to implement it iterativly?
Non-chronological backtracking (or backjumping as it is usually called) can be implemented in solvers that use recursion for the variable assignments. In languages that support non-local gotos, you would typically use that method. For example in the C language you would use setjmp() to record a point in the stack and longjmp() to backjump to that point. C# has try-catch blocks, Lispy languages might have catch-throw, and so on.
If the language doesn't support non-local goto, then you can implement a substitute in your code. Instead of dpll() returning FALSE, have it return a tuple containing FALSE and the number of levels that need to be backtracked. Upstream callers decrement the counter in the tuple and return it until zero is returned.
You can modify this to get backjumping.
private Assignment recursiveBackJumpingSearch(CSP csp, Assignment assignment) {
Assignment result = null;
if (assignment.isComplete(csp.getVariables())) {
result = assignment;
}
else {
Variable var= selectUnassignedVariable(assignment, csp);
for (Object value : orderDomainValues(var, assignment, csp)) {
assignment.setAssignment(var, value);
fireStateChanged(assignment, csp);
if (assignment.isConsistent(csp.getConstraints(var))) {
result=recursiveBackJumpingSearch(csp, assignment);
if (result != null) {
break;
}
if (result == null)
numberOfBacktrack++;
}
assignment.removeAssignment(var);
}
}
return result;
}

C# why can't a UInt32 be unboxed as UInt64?

Consider:
object o = 123456U;
ulong l = (ulong) o; // fails
But this:
object o = 123456U;
ulong l = (ulong) (uint) o; // succeeds
The real issue I have is that I'd like to have a function that depending on a parameter type, processes them differently. Such as:
void foo(object o)
{
switch (Type.GetTypeCode(o.GetType()))
{
case TypeCode.UInt32:
case TypeCode.UInt64:
ulong l = (ulong) o;
RunUnsignedIntVersion(l);
break;
case TypeCode.Int32:
case TypeCode.Int64:
long n = (long) o;
RunSignedVersion(n);
break;
}
}
and you can't make the following calls:
foo(123456U);
foo(123456);
I know there are ways to do this using generics. But I'm using .net micro framework and generics are not supported. But any C# 3.0 compiler specific features are supported including anonymous functions.
Edit I'd like to avoid having to handle each type separately. Is there a way this can be done and still have a parameter of object type?
The unbox operations support only the unbox, not any coercion that you might expect.
Whilst this can be frustrating it is worth noting that fixing this would
make unboxing considerably more expensive
possibly complicate the language due to nasty edge cases on method overload selection
Amongst others, for some in depth explanation, Eric Lippert is, as ever, most instructive
If you care about performance the only effective way to do this is (as Jimmy points out)
case TypeCode.Int32:
RunSignedVersion((int) o);
break;
case TypeCode.Int64:
long n = (long) o;
RunSignedVersion(n);
break;
This seems not too onerous.
If this is too painful then you may make use of Convert.ToInt64 or Convert.ToUInt64() with the associated cost.
void foo(object o)
{
switch (Type.GetTypeCode(o.GetType()))
{
case TypeCode.UInt32:
case TypeCode.UInt64:
ulong l = Convert.ToUInt64(o);
RunUnsignedIntVersion(l);
break;
case TypeCode.Int32:
case TypeCode.Int64:
long n = Convert.ToInt64(o);
RunSignedVersion(n);
break;
}
}
If Convert is not available here is the rotor source for the relevant methods:
[CLSCompliant(false)]
public static ulong ToUInt64(object value) {
return value == null? 0: ((IConvertible)value).ToUInt64(null);
}
[CLSCompliant(false)]
public static long ToInt64(object value) {
return value == null? 0: ((IConvertible)value).ToInt64(null);
}
IConvertible is supported as an interface in the compact framework, I would assume this would therefore work but have not tried it.
If you want the MicroFramework then I suggest simply implementing the conversion options on a per type basis is the best you can do. The API is so sparse that there really isn't much else possible. I would also suggest that anything based on boxing is risky since this is a significant allocation overhead in a very memory constrained environment.
If you are trying to implement a string.Format() alike have you considered System.Ext.Text.StringBuilder.AppendFormat followed by a ToString?
case TypeCode.Int32:
RunSignedVersion((int) o);
break;
case TypeCode.Int64:
long n = (long) o;
RunSignedVersion(n);
break;
the reason you can't unbox as int is because unboxing and casting are two different operations that happen to share the same operator.
This is because you can only unbox to the same type that was originally boxed (or to the nullable version of that type).
For example, a boxed byte can only be unboxed to byte or byte?, a boxed int can only be unboxed to int or int?, a boxed long can only be unboxed to long or long? etc etc.
You could create an extension method like that :
public static class ConversionExtensions
{
public static ulong ToUInt64(this object value)
{
return ((IConvertible)value).ToUInt64();
}
}
You could then use it as follows :
object o = 123456U;
ulong l = o.ToUInt64();

How big is the memory cost when passing an type as parameter to an method?

For example, I try to do something like this:
- (BOOL)compare:(NSDecimal)leftOperand greaterThan:(NSDecimal)rightOperand {
BOOL returnValue = NO;
NSComparisonResult result = NSDecimalCompare(&leftOperand, &rightOperand);
if (result == NSOrderedDescending) { // if the left operand is greater than the right operand
returnValue = YES;
}
return returnValue;
}
But I wonder how big is the cost for memory when using this wrapper. The NSDecimalCompare function takes parameters by reference (is that the word?). But my method does not. I find that by-reference stuff hard to use. Does my method create copies of these values? Is it a waste of memory?
You'll be making copies of your NSDecimals, but they're only 36-byte (if my math is correct) structs, so it might not be a significant overhead.
But is this really an issue? For example, are you calling this method many times per second? Sample your code first to see where the bottlenecks are before trying to optimize things like this. As Knuth says, "premature optimization is the root of all evil".