Tuesday, 20 June 2017

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than the constructor would not already implicitly do.


In the second case, without them, the compiler will expect an implementation elsewhere - such as a .cpp file.

Why is Gwen Stacy (Emma Stone) in the Amazing Spider-man? - Movies & TV



This is a re-telling of the origin story of Spider-man. There wasn't any girlfriend in the comic origin. I understand that a love interest is needed in a movie but why was Gwen Stacy chosen instead of the better known Mary Jane Watson? Gwen Stacy has a tragic death in the comic, which means they could potential kill her in the re-boot. Why bring her into a retelling?


Answer




From Marvel.com:





Gwen Stacy was Peter Parker's first true love.



Next to the death of his Uncle Ben, no death has weighed as heavily upon Spider-Man's shoulders as her passing.



Death of Gwen Stacy





From producer Matt Tolmach:





The relationship between Peter and Gwen is very significant – the previous movies haven’t explored this until now.



Gwen is a very self-assured character; she’s his rival intellectually. And her father happens to be Captain Stacy and let’s be honest, it’s hard enough to meet your girlfriend’s parents for the first time, but when he happens to be the head of the police force that’s chasing you, it makes things that much more complicated.



But there’s an emotional honesty and partnership that’s unique to their relationship. Gwen is really the only person who truly knows Peter – and because of that, there’s a closeness that develops between the two of them that neither of them have with anyone else in their lives.





From director Marc Webb:





One of the reasons why I wanted to use Gwen — first and foremost, she’s his first love in the comics. Let’s just set the record straight, it’s not Mary Jane.



But I like the idea of following somebody who is as smart, if not smarter, than Peter Parker. And Emma Stone is the perfect woman to play somebody who is much more proactive, much more intelligent and feisty.



I just like that dynamic in relationships in movies where they’re kind of lovers as rivals, you know? There’s this back and forth that I love, in the laboratory, and there’s just this great bond that you feel between them. She’s not just a prize, she’s not just a damsel in distress. She’s a confidante, and that was a really important thing.





I'd say using Gwen Stacy instead of Mary Jane also helps distinguish The Amazing Spider-Man from Raimi's Spider-Man.


debugging - Why do I get: double free or corruption (top) after running this c++ script?

I've got the following piece of code:



Class Chain


with



char* c; 



as its only public atrib



istream& operator>>(istream& i, Chain& s) {
delete [] s.c;
const int L = 256;
char *t = new char[L];
i.getline(t,L);
s.c = t;

return i;
}

ostream& operator<<(ostream& o, Chain s) {
o << s.c;
return o;
}

#include
#include "Chain.h"

using namespace std;

int main(){

Chain id;

cin >> id;

cout << id;
cout << id;



After running the code under the Eclipse IDE on Xubuntu (last version) I get the following error:




Error in [...] double free or corruption (top): 0x00000000008fd290 ***




What could be wrong?

Monday, 19 June 2017

c# - How to add double quotes to a string that is inside a variable?

If you have to do this often and you would like this to be cleaner in code you might like to have an extension method for this.



This is really obvious code, but still I think it can be useful to grab and make you save time.



  /// 
/// Put a string between double quotes.

///

/// Value to be put between double quotes ex: foo
/// double quoted string ex: "foo"
public static string AddDoubleQuotes(this string value)
{
return "\"" + value + "\"";
}


Then you may call foo.AddDoubleQuotes() or "foo".AddDoubleQuotes(), on every string you like.




Hope this help.

Open and define two excel files in VBA

Answer


As a part of a bigger macro, and need to open and define two workbooks and sheets. (I am aware of that I define my worksheets as Variant, I need this for futhure operations). I get a mistake when I try to set value to SheetRI. Does anyone see what it can be? Thanks in advance!



Sub compareQRTsAll()

Dim ActiveWb As Workbook
Dim ActiveSh As Worksheet
Dim SheetFasit As Variant
Dim SheetRI As Variant
Dim FolderFasit As String
Dim FileFasit As String

Dim FolderRI As String
Dim FileRI As String
Dim WbFasit As Workbook
Dim WbRI As Workbook
Dim WbFasitPath As String
Dim strRangeToCheck As String
Dim nShFasit As Integer
Dim nShRI As Integer
Dim iRow As Long
Dim iCol As Long

Dim i As Integer
Dim j As Integer
i = 2
j = 6

Set ActiveWb = ActiveWorkbook
Set ActiveSh = ActiveWb.Worksheets(1)
strRangeToCheck = "A1:AAA1000"
ActiveSh.Range("A2:D10000").Clear


FolderFasit = ActiveSh.Range("J6")
FolderRI = ActiveSh.Range("J7")

Do While ActiveSh.Cells(j, 8) <> ""

FileFasit = Dir(FolderFasit & "\*" & ActiveSh.Cells(j, 8) & "*.xls*")
Set WbFasit = Workbooks.Open(Filename:=FolderFasit & "\" & FileFasit)
SheetFasit = WbFasit.Worksheets(1).Range(strRangeToCheck)
nShFasit = WbFasit.Sheets.Count


FileRI = Dir(FolderRI & "\*" & ActiveSh.Cells(j, 8) & "*.xls*")
Set WbRI = Workbooks.Open(Filename:=FolderRI & "\" & FileRI)
SheetRI = WbRI.Worksheets(1).Range(strRangeToCheck) '<-------------THIS DOESN'T WORK
nShRI = WbRI.Sheets.Count


If nShFasit <> nShRI Then
MsgBox "QRT " & ActiveSh.Cells(j, 8) & " has different number of sheets in fasit and in RI. Further check will not be performed"

ElseIf nShFasit = nShRI And nShFasit = 1 Then


For iRow = LBound(SheetFasit, 1) To UBound(SheetFasit, 1)
For iCol = LBound(SheetFasit, 2) To UBound(SheetFasit, 2)
If SheetFasit(iRow, iCol) = SheetRI(iRow, iCol) Then

' Do nothing.
Else
ActiveSh.Cells(i, 1) = "Check row " & iRow & ", column " & iCol & " in " & ActiveSh.Cells(j, 8)
ActiveSh.Cells(i, 2) = SheetFasit(iRow, iCol)
ActiveSh.Cells(i, 3) = SheetRI(iRow, iCol)

i = i + 1
End If
Next iCol
Next iRow

End If


'close workbooks



Dim wb As Workbook
For Each wb In Workbooks
If Not wb Is ActiveWb Then
wb.Close SaveChanges:=False
End If
Next wb



j = j + 1
Loop

End Sub

What are the Android SDK build-tools, platform-tools and tools? And which version should be used?

I'll leave the discussion of the difference between Build Tools, Platform Tools, and Tools to others. From a practical standpoint, you only need to know the answer to your second question:


Which version should be used?


Answer: Use the most recent version.


For those using Android Studio with Gradle, the buildToolsVersion has to be set in the build.gradle (Module: app) file.


android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
...
}

Where do I get the most recent version number of Build Tools?


Open the Android SDK Manager.



  • In Android Studio go to Tools > Android > SDK Manager > Appearance & Behavior > System Settings > Android SDK

  • Choose the SDK Tools tab.

  • Select Android SDK Build Tools from the list

  • Check Show Package Details.


The last item will show the most recent version.


enter image description here


Make sure it is installed and then write that number as the buildToolsVersion in build.gradle (Module: app).

javascript - What does "this" mean in jQuery?




In jquery, what does this means and when it is used?


Answer



this in JavaScript is very special and powerful. It can mean just about anything. I cover some of it here and here, but it's really worth finding a good tutorial on JavaScript and spending some time with it.



Let's look at jQuery's use of it first, then talk about it more generally in JavaScript (a bit).



In jQuery, specifically




In code written with jQuery, this usually refers to the DOM element that's the subject of the function being called (for instance, in an event callback).



Example jQuery event callback (what this is is covered in the .bind docs):



$("div").click(function() {
// Here, `this` will be the DOM element for the div that was clicked,
// so you could (for instance) set its foreground color:
this.style.color = "red";

// You'll frequently see $(this) used to wrap a jQuery object around the

// element, because jQuery makes lots of things a lot simpler. You might
// hide the element, for example:
$(this).hide();
});


Similarly, various jQuery functions that act on all of the elements matched by the current jQuery selector can optionally accept a function, and when that function gets called, this is again the DOM element in question — for instance, the html function allows this:



// Find all divs inside the `foo` element, and set
// their content to their CSS class name(s)

// (Okay, so it's a hokey example)
$("#foo div").html(function() {
return this.className;
});


Another place jQuery uses this is in the callback on jQuery.each:



var a = ["one", "two", "three"];
jQuery.each(a, function() {

alert(this);
});


...which will alert "one", then "two", then "three". As you can see, this is a totally different usage of this.



(Confusingly, jQuery has two functions called each, the one above which is on the jQuery/$ function itself and always called that way [jQuery.each(...) or $.each(...)], and a different one on jQuery instances [objects] rather than the jQuery/$ function iself. Here are the docs for the other one, I don't discuss the other one in this answer because it uses this the same way html and event callback do, and I wanted to show a different use of this by jQuery.)



Generically in JavaScript




this refers to an object. Update: As of ES5's strict mode, that's no longer true, this can have any value. The value of this within any given function call is determined by how the function is called (not where the function is defined, as in languages like C# or Java). The most common way to set up this when calling a function is by calling the function via a property on the object:



var obj = {};
obj.foo = function() {
alert(this.firstName);
};
obj.firstName = "Fred";
obj.foo(); // alerts "Fred"



There, because we called foo via a property on obj, this was set to obj for the duration of the call. But don't get the impression that foo is in any way married to obj, this works just fine:



var obj = {};
obj.foo = function() {
alert(this.firstName);
};
obj.firstName = "Fred";
obj.foo(); // alerts "Fred"

var differentObj = {};

differentObj.firstName = "Barney";
differentObj.bar = obj.foo; // Not *calling* it, just getting a reference to it
differentObj.bar(); // alerts "Barney"


In fact, foo isn't intrinsically tied to any object at all:



var f = obj.foo; // Not *calling* it, just getting a reference to it
f(); // Probably alerts "undefined"



There, since we didn't call f via an object property, this wasn't explicitly set. When this isn't explicitly set, it defaults to the global object (which is window in browsers). window probably doesn't have a property firstName, and so we got "undefined" in our alert.



There are other ways to call functions and set what this is: By using the function's .call and .apply functions:



function foo(arg1, arg2) {
alert(this.firstName);
alert(arg1);
alert(arg2);
}


var obj = {firstName: "Wilma"};
foo.call(obj, 42, 27); // alerts "Wilma", "42", and "27"


call sets this to the first argument you give it, and then passes along any other arguments you give it to the function it's calling.



apply does exactly the same thing, but you give it the arguments for the function as an array instead of individually:



var obj = {firstName: "Wilma"};

var a = [42, 27];
foo.apply(obj, a); // alerts "Wilma", "42", and "27"
// ^-- Note this is one argument, an array of arguments for `foo`


Again, though, there's a lot more to explore about this in JavaScript. The concept is powerful, a bit deceptive if you're used to how some other languages do it (and not if you're used to some others), and worth knowing.



Here are some examples of this not referring to an object in ES5's strict mode:



(function() {

"use strict"; // Strict mode

test("direct");
test.call(5, "with 5");
test.call(true, "with true");
test.call("hi", "with 'hi'");

function test(msg) {
console.log("[Strict] " + msg + "; typeof this = " + typeof this);
}

})();


Output:



[Strict] direct; typeof this = undefined
[Strict] with 5; typeof this = number
[Strict] with true; typeof this = boolean
[Strict] with 'hi'; typeof this = string



Whereas in loose mode, all of those would have said typeof this = object; live copy.


java - Is there any performance difference between using > and >= in a loop





In loops we keep terminating conditions and we check those conditions in every pass.



I have seen 2 methods to check



1 . i > x or i < x



and the second approch is



2 . i >= x or i <= x




Is there any performance difference in these to 2 approaches while the logical comparison.



Is there any difference in execution time required for both operations. i.e. > and >= ?


Answer



There's very little if any performance difference between these two statements, but there is a significant difference between the two statements and their logic flow.



For instance:




  • If you have a loop that runs until i <= 20, you'll loop until i == 20.


  • If you have a loop that runs until i < 20, you'll loop until i == 19.



If you have a condition that requires your iteration value to stop before a certain value, be sure that you pick the inequality that most suits it.



By and large, though, if there were any difference in run time or performance on this boolean statement, it would be barely noticeable. One should not base any optimization work around switching out those statements.*



*: Also, don't break Rule #1 of Optimization.


Sunday, 18 June 2017

c++ - Sequence Points vs Operator Precedence











I'm still trying to wrap my head around how the following expression results in undefined behavior:



a = a++;


Upon searching SO about this, I found the following question:




Difference between sequence points and operator precedence? 0_o



I read through all the answers but I still am having difficulty with the details. One of the answers describes the behavior of my above code example as ambiguous, in terms of how a is modified. For example, it could come down to either of these:



a=(a+1);a++;
a++;a=a;


What exactly makes a's modification ambiguous? Does this have to do with CPU instructions on different platforms, and how the optimizer can take advantage of the undefined behavior? In other words, it seems undefined because of the generated assembler?




I don't see a reason for the compiler to use a=(a+1);a++;, it just looks quirky and doesn't make much sense. What would possess the compiler to make it behave this way?



EDIT:



Just to be clear, I do understand what is happening, I just don't understand how it can be undefined when there are rules on operator precedence (which essentially defines the order of evaluation of the expression). Assignment happens last in this case, so a++ needs to be evaluated first, to determine the value to assign to a. So what I expect is that a is modified first, during the post-fix increment, but then yields a value to assign back to a (second modification). But the rules for operator precedence seem to make the behavior very clear to me, I fail to find where there is any "wiggle-room" for it to have undefined behavior.


Answer



The first answer in the question you linked to explains exactly what's going on. I'll try to rephrase it to make it more clear.



Operator precedence defines the order of the computation of values via expressions. The result of the expression (a++) is well understood.




However, the modification of the variable a is not part of the expression. Yes, really. This is the part you're having trouble understanding, but that's simply how C and C++ define it.



Expressions result in values, but some expressions can have side effects. The expression a = 1 has a value of 1, but it also has the side effect of setting the variable a to 1. As far as how C and C++ define things, these are two different steps. Similarly, a++ has a value and a side-effect.



Sequence points define when side effects are visible to expressions that are evaluated after those sequence points. Operator precedence has nothing to do with sequence points. That's just how C/C++ defines things.


php - Parse error: syntax error, unexpected '{', expecting '(' help?



I'm trying to make a photo gallery plugin for express engine, yet it's giving me this error and everything seems to close. Any help?



$whats_gonna_happen=$_GET['pictures'];

class upload_pictures
{

public function upload_pictures
{
if (!isset($whats_gonna_happen))
{
$uploads='';
$cout=1;
$stuff=$this->EE->db->query('SELECT id, name FROM albums');
$albums_list='';
while ($row=$stuff->result_array())
{

$albums_list .= "\n";
}
mysql_free_result($stuff);
echo '

Pictures upload









Choose album :



photo :



Caption :








';
}

Answer



The problem is here:



public function upload_pictures


should be:




public function upload_pictures()

javascript - What is the difference between Bower and npm?



What is the fundamental difference between bower and npm? Just want something plain and simple. I've seen some of my colleagues use bower and npm interchangeably in their projects.


Answer



All package managers have many downsides. You just have to pick which you can live with.



History




npm started out managing node.js modules (that's why packages go into node_modules by default), but it works for the front-end too when combined with Browserify or webpack.



Bower is created solely for the front-end and is optimized with that in mind.



Size of repo



npm is much, much larger than bower, including general purpose JavaScript (like country-data for country information or sorts for sorting functions that is usable on the front end or the back end).



Bower has a much smaller amount of packages.




Handling of styles etc



Bower includes styles etc.



npm is focused on JavaScript. Styles are either downloaded separately or required by something like npm-sass or sass-npm.



Dependency handling



The biggest difference is that npm does nested dependencies (but is flat by default) while Bower requires a flat dependency tree (puts the burden of dependency resolution on the user).




A nested dependency tree means that your dependencies can have their own dependencies which can have their own, and so on. This allows for two modules to require different versions of the same dependency and still work. Note since npm v3, the dependency tree will by flat by default (saving space) and only nest where needed, e.g., if two dependencies need their own version of Underscore.



Some projects use both is that they use Bower for front-end packages and npm for developer tools like Yeoman, Grunt, Gulp, JSHint, CoffeeScript, etc.






Resources





character - Was Bane part of the League of Shadows during the time Bruce was being trained? - Movies & TV

Both Bruce Wayne and Bane were obviously trained by the League of Shadows so it seems quite possible that their time with the League of Shadows could've coincided.



Bane was in the pit for a long time but had he been rescued from the pit and joined the League of Shadows when Bruce was there?

Why is it bad style to `rescue Exception => e` in Ruby?




Ryan Davis’s Ruby QuickRef says (without explanation):




Don’t rescue Exception. EVER. or I will stab you.




Why not? What’s the right thing to do?


Answer



TL;DR: Use StandardError instead for general exception catching. When the original exception is re-raised (e.g. when rescuing to log the exception only), rescuing Exception is probably okay.







Exception is the root of Ruby's exception hierarchy, so when you rescue Exception you rescue from everything, including subclasses such as SyntaxError, LoadError, and Interrupt.



Rescuing Interrupt prevents the user from using CTRLC to exit the program.



Rescuing SignalException prevents the program from responding correctly to signals. It will be unkillable except by kill -9.



Rescuing SyntaxError means that evals that fail will do so silently.




All of these can be shown by running this program, and trying to CTRLC or kill it:



loop do
begin
sleep 1
eval "djsakru3924r9eiuorwju3498 += 5u84fior8u8t4ruyf8ihiure"
rescue Exception
puts "I refuse to fail or be stopped!"
end

end


Rescuing from Exception isn't even the default. Doing



begin
# iceberg!
rescue
# lifeboats
end



does not rescue from Exception, it rescues from StandardError. You should generally specify something more specific than the default StandardError, but rescuing from Exception broadens the scope rather than narrowing it, and can have catastrophic results and make bug-hunting extremely difficult.






If you have a situation where you do want to rescue from StandardError and you need a variable with the exception, you can use this form:



begin
# iceberg!

rescue => e
# lifeboats
end


which is equivalent to:



begin
# iceberg!
rescue StandardError => e

# lifeboats
end





One of the few common cases where it’s sane to rescue from Exception is for logging/reporting purposes, in which case you should immediately re-raise the exception:



begin
# iceberg?

rescue Exception => e
# do some logging
raise # not enough lifeboats ;)
end

reactjs - Should flux stores, or actions (or both) touch external services?



Should the stores maintain their own state and have the ability to call network and data storage services in doing so ...in which case the actions are just dumb message passers,




-OR-



...should the stores be dumb recipients of immutable data from the actions (and the actions be the ones that fetch/send data between external sources? Store in this instance would act as view-models and would be able to aggregate / filter their data prior to setting their own state base on the immutable data they were fed by the action.



It seems to me that it should be one or the other (rather than a mix of both). If so, why is one preferred / recommended over the other?


Answer





I've seen the flux pattern implemented both ways, and after having done both myself (initially going with the former approach), I believe that stores should be dumb recipients of data from the actions, and that asynchronous processing of writes should live in the action creators. (Async reads can be handled differently.) In my experience, this has a few benefits, in order of importance:





  1. Your stores become completely synchronous. This makes your store logic much easier to follow and very easy to test—just instantiate a store with some given state, send it an action, and check to see if the state changed as expected. Furthermore, one of the core concepts in flux is to prevent cascading dispatches and to prevent multiple dispatches at once; this is very difficult to do when your stores do asynchronous processing.


  2. All action dispatches happen from the action creators. If you handle asynchronous operations in your stores and you want to keep your stores' action handlers synchronous (and you should in order to get the flux single-dispatch guarantees), your stores will need to fire additional SUCCESS and FAIL actions in response to asynchronous processing. Putting these dispatches in the action creators instead helps separate the jobs of the action creators and the stores; furthermore, you don't have to go digging through your store logic to figure out where actions are being dispatched from. A typical asynchronous action in this case might look something like this (change the syntax of the dispatch calls based on the flavor of flux you're using):



    someActionCreator: function(userId) {
    // Dispatch an action now so that stores that want
    // to optimistically update their state can do so.
    dispatch("SOME_ACTION", {userId: userId});


    // This example uses promises, but you can use Node-style
    // callbacks or whatever you want for error handling.
    SomeDataAccessLayer.doSomething(userId)
    .then(function(newData) {
    // Stores that optimistically updated may not do anything
    // with a "SUCCESS" action, but you might e.g. stop showing
    // a loading indicator, etc.
    dispatch("SOME_ACTION_SUCCESS", {userId: userId, newData: newData});
    }, function(error) {
    // Stores can roll back by watching for the error case.

    dispatch("SOME_ACTION_FAIL", {userId: userId, error: error});
    });
    }


    Logic that may otherwise be duplicated across various actions should be extracted into a separate module; in this example, that module would be SomeDataAccessLayer, which handles doing the actual Ajax request.


  3. You need less action creators. This is less of a big deal, but nice to have. As mentioned in #2, if your stores have synchronous action dispatch handling (and they should), you'll need to fire extra actions to handle the results of asynchronous operations. Doing the dispatches in the action creators means that a single action creator can dispatch all three action types by handling the result of the asynchronous data access itself.



mysql - How to prevent XSS attacks in PHP?

I have found on the web many function/class to prevent XSS attacks. Now, what's best PHP function/class for 100% prevent XSS attacks for input POST/GET form value?

javascript - Changing URL without reloading (angularjs // follow up Q.)

Im trying to change the url with angular without reloading the page.




I came across this question and specifically this answer:




If you need to change the path, add this after your .config in your
app file. Then you can do $location.path('/sampleurl', false); to
prevent reloading



app.run(['$route', '$rootScope', '$location', function ($route, $rootScope, $location) {
var original = $location.path;

$location.path = function (path, reload) {
if (reload === false) {
var lastRoute = $route.current;
var un = $rootScope.$on('$locationChangeSuccess', function () {
$route.current = lastRoute;
un();
});
}
return original.apply($location, [path]);
};

}])



However when i run that code i get an error straight away and console reads.




Uncaught Error: [$injector:unpr] http://errors.angularjs.org/1.6.2/$injector/unpr?p0=%24routeProvider%20%3C-%20%24route





When i click the link i read that maybe the error is because you cant pass $rootScope to anything but a directive or controller? Can someone help me explain whats going on?

plot explanation - Fox's analysis of the Bat - Movies & TV



Near the end of The Dark Knight Rises, Fox examines the Bat and finds out that an auto-pilot had been installed. This seems to me a bit strange, because we have seen the Bat carrying the bomb far from Gotham. I have seen that there are some questions here about the way Batman managed to escape the explosion, but shouldn't the vehicle have been completely destroyed? From what I understood, the bomb was bigger than an atomic bomb.



How did they manage to retrieve the Bat and analyze its software(this is even more unbelievable)? Have I missed something?


Answer



If I'm remembering right, the techs say that the change was 'checked in'. I took this to mean 'checked into source control'.
That is:





  • There are two bats

  • They both use the same software, which includes (non-functional) autopiloting.

  • Bruce Wayne was planning on using one of them, but needed the autopilot fixed. So he modified the autopilot. Being the nice guy he is (or perhaps just not wanting to lose his work), he checks his changes into source control.

  • During a regular maintenance cycle (perhaps when the techs booted up the second machine), the software is pulled and deployed to the other bat.

  • The existence of a working autopilot is communicated to Lucius Fox.


javascript - How to measure time taken by a function to execute



I need to get execution time in milliseconds.




I originally asked this question back in 2008. The accepted answer

then was to use new Date().getTime() However, we can all agree now
that using the standard performance.now() API is more
appropriate. I am therefore changing the accepted answer to this one.



Answer





var t0 = performance.now();

doSomething(); // <---- The function you're measuring time for


var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");



NodeJs: it is required to import the performance class










console.time('someFunction');

someFunction(); // Whatever is timed goes between the two "console.time"

console.timeEnd('someFunction');



Note:
The string being pass to the time() and timeEnd() methods must match
(for the timer to finish as expected).




console.time() documentations:




  1. NodeJS documentation regarding

  2. MDN (client-side) documentation




Saturday, 17 June 2017

optimization - What Every Programmer Should Know About Memory?




I am wondering how much of Ulrich Drepper's What Every Programmer Should Know About Memory from 2007 is still valid. Also I could not find a newer version than 1.0 or an errata.


Answer



As far as I remember Drepper's content describes fundamental concepts about memory: how CPU cache works, what are physical and virtual memory and how Linux kernel deals that zoo. Probably there are outdated API references in some examples, but it doesn't matter; that won't affect the relevance of the fundamental concepts.



So, any book or article that describes something fundamental cannot be called outdated. "What every programmer should know about memory" is definitely worth to read, but, well, I don't think it's for "every programmer". It's more suitable for system/embedded/kernel guys.


java - How to get the right input for the String variable using scanner class?




I am learning Java and I was trying an input program. When I tried to input an integer and string using instance to Scanner class , there is an error by which I can't input string. When I input string first and int after, it works fine. When I use a different object to Scanner class it also works fine. But what's the problem in this method when I try to input int first and string next using same instance to Scanner class?




import java.util.Scanner;

public class Program {
public static void main(String[] args) {

Scanner input=new Scanner(System.in);

//Scanner input2=new Scanner(System.in);

System.out.println("Enter your number :" );


int ip = input.nextInt();

System.out.println("Your Number is : " + ip);

System.out.println("Enter Your Name : ");

String name= input.nextLine();

System.out.println("Your Next is : " + name);

}
}

Answer



nextInt() doesn't wait for the end of the line - it waits for the end of the token, which is any whitespace, by default. So for example, if you type in "27 Jon" on the first line with your current code, you'll get a value of 27 for ip and Jon for name.



If you actually want to consumer a complete line, you might be best off calling input.nextLine() for the number input as well, and then use Integer.parseInt to parse the line. Aside from anything else, that represents what you actually want to do - enter two lines of text, and parse the first as a number.



Personally I'm not a big fan of Scanner - it has a lot of gotchas like this. I'm sure it's fine when it's being used in exactly the way the designers intended, but it's not always easy to tell what that is.


regex - Greedy vs. Reluctant vs. Possessive Quantifiers



I found this excellent tutorial on regular expressions and while I intuitively understand what "greedy", "reluctant" and "possessive" quantifiers do, there seems to be a serious hole in my understanding.



Specifically, in the following example:




Enter your regex: .*foo  // greedy quantifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.

Enter your regex: .*?foo // reluctant quantifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfoo" starting at index 0 and ending at index 4.
I found the text "xxxxxxfoo" starting at index 4 and ending at index 13.

Enter your regex: .*+foo // possessive quantifier

Enter input string to search: xfooxxxxxxfoo
No match found.


The explanation mentions eating the entire input string, letters been consumed, matcher backing off, rightmost occurrence of "foo" has been regurgitated, etc.



Unfortunately, despite the nice metaphors, I still don't understand what is eaten by whom... Do you know of another tutorial that explains (concisely) how regular expressions engines work?



Alternatively, if someone can explain in somewhat different phrasing the following paragraph, that would be much appreciated:





The first example uses the greedy
quantifier .* to find "anything", zero
or more times, followed by the letters
"f" "o" "o". Because the quantifier is
greedy, the .* portion of the
expression first eats the entire input
string. At this point, the overall
expression cannot succeed, because the
last three letters ("f" "o" "o") have

already been consumed (by whom?). So the matcher
slowly backs off (from right-to-left?) one letter at a time
until the rightmost occurrence of
"foo" has been regurgitated (what does this mean?), at which
point the match succeeds and the
search ends.



The second example, however, is
reluctant, so it starts by first
consuming (by whom?) "nothing". Because "foo"

doesn't appear at the beginning of the
string, it's forced to swallow (who swallows?) the
first letter (an "x"), which triggers
the first match at 0 and 4. Our test
harness continues the process until
the input string is exhausted. It
finds another match at 4 and 13.



The third example fails to find a
match because the quantifier is

possessive. In this case, the entire
input string is consumed by .*+, (how?)
leaving nothing left over to satisfy
the "foo" at the end of the
expression. Use a possessive
quantifier for situations where you
want to seize all of something without
ever backing off (what does back off mean?); it will outperform
the equivalent greedy quantifier in
cases where the match is not

immediately found.



Answer



I'll give it a shot.



A greedy quantifier first matches as much as possible. So the .* matches the entire string. Then the matcher tries to match the f following, but there are no characters left. So it "backtracks", making the greedy quantifier match one less thing (leaving the "o" at the end of the string unmatched). That still doesn't match the f in the regex, so it "backtracks" one more step, making the greedy quantifier match one less thing again (leaving the "oo" at the end of the string unmatched). That still doesn't match the f in the regex, so it backtracks one more step (leaving the "foo" at the end of the string unmatched). Now, the matcher finally matches the f in the regex, and the o and the next o are matched too. Success!



A reluctant or "non-greedy" quantifier first matches as little as possible. So the .* matches nothing at first, leaving the entire string unmatched. Then the matcher tries to match the f following, but the unmatched portion of the string starts with "x" so that doesn't work. So the matcher backtracks, making the non-greedy quantifier match one more thing (now it matches the "x", leaving "fooxxxxxxfoo" unmatched). Then it tries to match the f, which succeeds, and the o and the next o in the regex match too. Success!



In your example, it then starts the process over with the remaining unmatched portion of the string, following the same process.




A possessive quantifier is just like the greedy quantifier, but it doesn't backtrack. So it starts out with .* matching the entire string, leaving nothing unmatched. Then there is nothing left for it to match with the f in the regex. Since the possessive quantifier doesn't backtrack, the match fails there.


Is there a CSS parent selector?



How do I select the

  • element that is a direct parent of the anchor element?



    As an example, my CSS would be something like this:



    li < a.active {
    property: value;
    }



    Obviously there are ways of doing this with JavaScript, but I'm hoping that there is some sort of workaround that exists native to CSS Level 2.



    The menu that I am trying to style is being spewed out by a CMS, so I can't move the active element to the

  • element... (unless I theme the menu creation module which I'd rather not do).



    Any ideas?


  • Answer



    There is currently no way to select the parent of an element in CSS.




    If there was a way to do it, it would be in either of the current CSS selectors specs:





    In the meantime, you'll have to resort to JavaScript if you need to select a parent element.






    The Selectors Level 4 Working Draft includes a :has() pseudo-class that works the same as the jQuery implementation. As of 2019, this is still not supported by any browser.




    Using :has() the original question could be solved with this:



    li:has(> a.active) { /* styles to apply to the li tag */ }

    php - how to save data dynamically to database

    JQUERY CODE




     $(document).ready(function () {
    $("#add").click(function () {
    $(".left .inputs").append("
  • ");
    $(".right .inputs").append("
  • ");
    });
    });


    MY PHP CODE




        $con = mysql_connect ("localhost","root","") or die('cannot connect to database error: '.mysql_error());
    if (isset($_POST['name']) && isset($_POST['grade']))
    {

    $desk_report = $_POST['name'];//contains array value
    $desk_action = $_POST['grade'];//contains array value
    foreach($desk_report as $key=>$user) { //Loop through arrays
    if (!empty($desk_report[$key]) && !empty($desk_action[$key])) {

    mysql_select_db("quickbook", $con);
    $sql = "INSERT INTO student_reg(relative_name,relative_grade) VALUES ('$desk_report[$key]','$desk_action[$key]')";
    if ($sql_run = mysql_query($sql)) {
    echo 'ok.';
    } else {
    echo '*Sorry, we couldn\'t register you at this time. Try again later.';
    }
    }
    }
    }

    ?>


    I want to add inputs data to database.i created a code.but this is not working .can you help me?

    http - Why use output buffering in PHP?

    I have read quite a bit of material on Internet where different authors suggest using output buffering. The funny thing is that most authors argument for its use only because it allows for mixing response headers with actual content. Frankly, I think that responsible web applications should not mix outputting headers and content, and web developers should look for possible logical flaws in their scripts which result in headers being sent after output has been generated. This is my first argument against the ob_* output buffering API. Even for that little convenience you get - mixing headers with output - it is not a good enough reason to use it, unless one needs to hack up scripts fast, which is usually not the goal nor the way in a serious web application.



    Also, I think most people dealing with the output buffering API do not think about the fact that even without the explicit output buffering enabled, PHP in combination with the web-server it is plugged into, still does some internal buffering anyway. It is easy to check - do an echo of some short string, sleep for say 10 seconds, and do another echo. Request your script with a browser and watch the blank page pause for 10 seconds, with both lines appearing thereafter. Before some say that it is a rendering artefact, not traffic, tracing the actual traffic between the client and the server shows that the server has generated the Content-Length header with an appropriate value for the entire output - suggesting that the output was not sent progressively with each echo call, but accumulated in some buffer and then sent on script termination. This is one of my gripes with explicit output buffering - why do we need two different output buffer implementations on top of one another? May it be because the internal (inaccessible) PHP/Web-server output buffering is subject to conditions a PHP developer cannot control, and is thus not really usable?



    In any case, I for one, start to think one should avoid explicit output buffering (the series of ob_* functions) and rely on the implicit one, assisting it with the good flush function, when necessary. Maybe if there was some guarantee from the web server to actually send output to the client with each echo/print call, then it would be useful to set up explicit buffering - after all one does not want to send response to the client with some 100 byte chunks. But the alternative with two buffers seems like a somewhat useless layer of abstraction.



    So, ultimately, do serious web applications need output buffering?

    enumeration - Iterate Between Enum Values in C#






    Possible Duplicate:
    How to enumerate an enum?






    Suppose that i have an enumeration like that:




    public enum Cars
    {
    Audi = 0,
    BMW,
    Opel,
    Renault,
    Fiat,
    Citroen,
    AlfaRomeo,

    }


    Do i have a chance to iterate between Opel and Citroen? I want to give these values as parameters of a method.


    Answer



    This will work:



    for(Cars car=Cars.Opel; car<=Cars.Citroen; car++)
    {
    Console.WriteLine(car);

    }


    but you have to make sure that the start value is less than the end value.



    EDIT
    If you don't hardcode the start and end, but supply them as parameters, you need to use them in the correct order. If you just switch "Opel" and "Citroen", you will get no output.



    Also (as remarked in the comments) the underlying integer values must not contain gaps or overlaps. Luckily if you do not specify values yourself (even the '=0' is not needed), this will be the default behaviour. See MSDN:





    When you do not specify values for the elements in the enumerator list, the values are automatically incremented by 1.



    php - I can't find what *exactly* the syntax error is in mysql





    I am creating a simple database and it's table for learning purpose:



    This is my php code(script.php)




    $sql = file_get_contents("init.sql");

    $servername = "localhost";
    $username = "root";

    $password = "";
    // connect to database
    $conn = new mysqli($servername, $username, $password);

    if ($conn->connect_error) {
    die("Connection error: " . $conn->connect_error);
    }


    if($conn->query($sql) == True){

    echo "Database and Table has been created succesfully!";
    }

    else {
    echo "\nError creating database and table: . $conn->error";
    }

    ?>



    And this is mysql file(init.mysql)



    CREATE DATABASE test;
    USE test;

    CREATE TABLE Users (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),

    date_of_registration TIMESTAMP)


    The exact error I am seeing is:-




    Error creating database and table: . You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USE test; CREATE TABLE Users ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY ' at line 2




    In my opinion, the code is syntactically correct but as you see I am getting an error

    So I am struggling to find where is the error but no luck :(
    Or I am blind enough to see it.


    Answer



    To process multiple queries in one call (you have three in your file), you need to use multi_query. Change



    if($conn->query($sql) == True){


    to




    if($conn->multi_query($sql) == True){

    android - How to HTTP request by POST method with Kotlin




    I'm new a kotlin dev. please teach me to use httpRequest in Kotlin.



    Question
    - If I want to request to API service and I must to send request body with json object by use post method, How I write??



    Thank for help.


    Answer



    You can write your data using outputStream.write(postData).




     fun pushToChat(message: String) {

    val serverURL: String = "your URL"
    val url = URL(serverURL)
    val connection = url.openConnection() as HttpURLConnection
    connection.requestMethod = "POST"
    connection.connectTimeout = 300000
    connection.connectTimeout = 300000
    connection.doOutput = true


    val postData: ByteArray = message.toByteArray(StandardCharsets.UTF_8)

    connection.setRequestProperty("charset", "utf-8")
    connection.setRequestProperty("Content-lenght", postData.size.toString())
    connection.setRequestProperty("Content-Type", "application/json")

    try {
    val outputStream: DataOutputStream = DataOutputStream(connection.outputStream)
    outputStream.write(postData)

    outputStream.flush()
    } catch (exception: Exception) {

    }

    if (connection.responseCode != HttpURLConnection.HTTP_OK && connection.responseCode != HttpURLConnection.HTTP_CREATED) {
    try {


    val reader: BufferedReader = BufferedReader(InputStreamReader(inputStream))

    val output: String = reader.readLine()

    println("There was error while connecting the chat $output")
    System.exit(0)

    } catch (exception: Exception) {
    throw Exception("Exception while push the notification $exception.message")
    }
    }


    }

    jquery - Event handler not working on dynamic content




    I have a tag A in which when clicked on, it appends another tag B to perform an action B on click. So when I click on tag B, action B is performed. However, the .on method does not seems to be working on the dynamically created tag B.



    My html and jquery for tag A is as below:



    Add Shipping address

    $('.add_address').click(function(){

    //Add another
    $(document).append('
    Update');
    })


    When tag B is clicked, some action B is performed. My jquery is as below:



    $('.update').on('click',function(){
    //action B
    });



    I have some non dynamic content which has class ".update" as well. In the .on() method above works fine for the non dynamic content, but not for the dynamic content.



    How can I make it work for dynamic content?


    Answer



    You have to add the selector parameter, otherwise the event is directly bound instead of delegated, which only works if the element already exists (so it doesn't work for dynamically loaded content).



    See http://api.jquery.com/on/#direct-and-delegated-events




    Change your code to



    $(document.body).on('click', '.update' ,function(){


    The jQuery set receives the event then delegates it to elements matching the selector given as argument. This means that contrary to when using live, the jQuery set elements must exist when you execute the code.



    As this answers receives a lot of attention, here are two supplementary advises :



    1) When it's possible, try to bind the event listener to the most precise element, to avoid useless event handling.




    That is, if you're adding an element of class b to an existing element of id a, then don't use



    $(document.body).on('click', '#a .b', function(){


    but use



    $('#a').on('click', '.b', function(){



    2) Be careful, when you add an element with an id, to ensure you're not adding it twice. Not only is it "illegal" in HTML to have two elements with the same id but it breaks a lot of things. For example a selector "#c" would retrieve only one element with this id.


    ruby - Match all occurrences of a regex



    Is there a quick way to find every match of a regular expression in Ruby? I've looked through the Regex object in the Ruby STL and searched on Google to no avail.


    Answer



    Using scan should do the trick:




    string.scan(/regex/)

    angular - How to call function after dom renders in Angular2?

    I am new to Angular2 and Angular in general and am trying to get some jQuery to fire after the dom is updated when the data of a component is changed. The jQuery needs to calculate heights of elements so I can't exactly just use the data. Unfortunately it looks like onAllChangesDone only fires after data changes, not the dom.

    javascript - Get a list of Available Functions on Tampermonkey

    I currently have a Tampermonkey script with a series of contextualized functions that I provide as options to run when on a certain page.



    A simplified version of my code would be:



    window.func1 = function func1() {...}
    window.func2 = function func2(a) {...}

    window.func3 = function func3(a, b) {...}


    I would like to have some function that could take the current script I have and provide me with a list of the available functions, something along the lines of:



    >> getAvailableFunctions()
    << func1, func2, func3


    I have been investigating with the Object.getOwnPropertyNames method, but I can't seem to pull that one out with Tampermonkey.




    Can I have some input?



    Thank you!

    Friday, 16 June 2017

    How to extract multi-part zip files with PHP?

    How to extract a multi-part zip file with PHP? Is it possible?

    R Shiny App Error - Couldn't Find Any UI

    I wrote this code and initially Shiny worked just fine and the app started:



    library(shiny)    
    ui <- fluidPage(
    titlePanel("Title"),
    sidebarPanel(

    sliderInput("priceInput", "Price", min = 0, max = 100,
    value = c(25,40), pre = "$")
    ),
    mainPanel("Results")
    )


    But when I add any UI element to it, for example radioButton:



    ui <- fluidPage(

    titlePanel("Title"),
    sidebarPanel(
    sliderInput("priceInput", "Price", min = 0, max = 100,
    value = c(25,40), pre = "$"),
    radioButtons("typeInput", "Product type",
    choices = c("p1", "p2", "p3"),
    selected = "p1")
    ),
    mainPanel("Results")
    )



    I get an error:



    Shiny couldn't find any UI for this application. We looked in:    
    www/index.html
    ui.R
    app.R



    Any idea what might cause this? I tried other options instead of a radio button and received the same error. My server file is just an empty file with the server function.

    css3 - CSS select first element with a certain class

    What is the syntax for selecting the first element with a certain class? Please specify whether that method of selection is part of CSS3 or CSS2.1.

    security - Is there an exhaustive list of functions that can be disabled using disable_functions in PHP?

    I have always wondered if there is an exhaustive list of functions that could be disabled using disable_functions in php.ini?



    I question if there is a specific list that can vs can't be because PHP.net says:




    This directive allows you to disable certain functions for security reasons.




    It reads like there are certain functions that can be. And certain functions that can't be.

    PHP header() will not redirect issue



    I'm having an issue with the header("Location: index.php?action=messagesent") it will not redirect after The user presses submit and the php is ran. Normally it will redirect but for some reason it's not working, It just reloads the page after they hit submit. But it does echo "Message Sent" right under the header code. Any ideas on why it is not redirecting? Thank You in advance.




    Heres my Code:



            












    $servername = "localhost";
    $username = "myusername";
    $password = 'password';
    $dbname = "database";

    try {


    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO messaging
    (fromid, toid, message, tousername,
    fromusername,userprofile, sentdate, messagebefore)
    VALUES (:fromid, :toid, :message, :tousername,

    :fromusername, :userprofile, :sentdate, :messagebefore)");

    // insert a row
    $toid = $fromid;
    $fromid = $_SESSION['user']['id'];
    $messagebefore = $message;
    $message = $_POST['message'];
    $tousername = $row['fromusername'];
    $fromusername = $_SESSION['user']['username'];
    $userprofile = $row['userprofile'];

    $sentdate = $date = date('H:i, jS F Y');

    //Bind Inputs
    $stmt->bindParam(':fromid', $fromid);
    $stmt->bindParam(':toid', $toid);
    $stmt->bindParam(':message', $message);
    $stmt->bindParam(':tousername', $tousername);
    $stmt->bindParam(':fromusername', $fromusername);
    $stmt->bindParam(':userprofile', $userprofile);
    $stmt->bindParam(':sentdate', $sentdate);

    $stmt->bindParam(':messagebefore', $messagebefore);

    $stmt->execute();

    echo "Message Sent. ";
    header("Location: inbox.php?action=messagesent");
    }
    catch(PDOException $e){
    }
    $conn = null;

    ?>

    Answer



    A header(Location: ...) will only work if you have not already sent output to the browser.



    Earlier in your script you output the form, hence header() fails with an error message.



    If you look in your error log, you will see the error message.



    Add error reporting to the

    top of your file(s) while testing right after your opening PHP tag for example



    error_reporting(E_ALL); 
    ini_set('display_errors', 1);


    Now you will see the errors on the browser page.


    How can i validate integer form in php?



    I want to validate integer form using PHP. Do anyone of you know how to validate it?
    my code like this
    `
    Thông tin thành viên
    ">



            First name : 

    Last name :
    User Name:
    Password:
    Email:
    Phone:
    Cấp độ :






    `


    When people input the phone i want to check whether it is integer or not? can you help?



    Answer



    Try using this is you want to check it on runtime.



    Phone: 


    Or use is_int or is_numeric if you want to check the phone number after POST.


    plot explanation - What did Griffin show the Colonel to convince him in Men in Black 3?

    I'm wondering what exactly is shown to the Colonel by Griffin near the end of Men in Black 3 (MIB3)?


    I don't remember exactly what the Colonel answered when J asked him what he saw, but it was pretty vague.


    Any ideas?


    Answer


    He answers with



    He showed me how important you are... you and your partner.



    So my interpretation is he showed him the truth about J, J's future and his mission:



    Young Agent K: You ever wanna see more, I know a top secret organization that can use a man like you.


    Colonel: I wish I could.



    Here I have the impression that Colonel has also seen his own death.


    php - Help with this error message > unexpected T_OBJECT_OPERATOR




    The code below is part of an rss feed parser using WordPress's simplepie fetch_feed()...



    Code is:



        if ($enclosure = $item->get_enclosure(0))
    {
    $image_thumb = $item->get_enclosure()->get_link().'screenshot.jpg';
    $image = $item->get_enclosure()->get_link().'screenshot-full.jpg';
    }
    $link = esc_url( strip_tags( $item->get_link() ) );

    $content = $item->get_content();


    Upon trying to activate the theme in which this code appears, I'm getting the following error:



    Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/.../functions.php on line 1341



    Line 1341 is the line that starts with $image_thumb


    Answer



    My wild guess is that this is PHP 4, which doesn't support method chaining.



    How to make multiple MySQL connections from PHP

    Answer


    Answer





    The code below is working for one if statement but not giving results for another else if and it is showing the 'flights' table in first query but after another condition not display the other table named 'isb to muree'




    $from = isset($_POST['from'])?$_POST['from']:'';
    $To = isset($_POST['To'])?$_POST['To']:'';

    if( $from =='Islamabad'){
    if($To == 'Lahore'){



    $db_host = 'localhost';
    $db_user = 'root';

    $database = 'homedb';
    //$table = 'flights';

    if (!mysql_connect($db_host, $db_user))
    die("Can't connect to database");


    if (!mysql_select_db($database))
    die("Can't select database");
    $result = mysql_query("SELECT * FROM flights");
    if (!$result) {
    die("Query to show fields from table failed");
    }




    $fields_num = mysql_num_fields($result);

    echo "

    Table: 'flights'

    ";
    echo "";


    while($row = mysql_fetch_row($result))
    {
    echo "";


    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
    echo "";

    echo "\n";
    }
    }

    else if( $from =='Islamabad'){

    if($To == 'murree'){
    if (!mysql_connect($db_host, $db_user))
    die("Can't connect to database");

    if (!mysql_select_db($database))
    die("Can't select database");

    $result = mysql_query("SELECT * FROM 'isb to murree'");
    if (!$result) {
    die("Query to show fields from table failed");

    }
    $fields_num = mysql_num_fields($result);

    echo "

    Table: 'isb to murree'

    ";
    echo "
    $cell
    ";


    while($row = mysql_fetch_row($result))
    {
    echo "";


    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
    echo "";

    echo "\n";

    }
    }

    }
    }


    mysqli_close($con);
    ?>

    Answer



    You should move the database connection variables to the top of your code (so they are outside of the if statement)




    $db_host = 'localhost';
    $db_user = 'root';
    $database = 'homedb';

    c++ - What is the efficient way to count set bits at a position or lower?




    Given std::bitset<64> bits with any number of bits set and a bit position X (0-63)



    What is the most efficient way to count bits at position X or lower or return 0 if the bit at X is not set



    Note: If the bit is set the return will always be at least 1



    The brute force way is very slow:



    int countupto(std::bitset<64> bits, int X)

    {
    if (!bits[X]) return 0;
    int total=1;
    for (int i=0; i < X; ++i)
    {
    total+=bits[i];
    }
    return total;
    }



    The count() methof of bitset will give you the popcount of all the bits, but bitset does not support ranges



    Note: This is not a dup of How to count the number of set bits in a 32-bit integer? as that asks about all bits not the range 0 through X


    Answer



    This C++ gets g++ to emit very good x86 ASM (godbolt compiler explorer). I expect it will compile efficiently on other 64bit architectures, too (if there's a HW popcount for std::bitset::count to use, otherwise that'll always be the slow part; e.g. sure to use g++ -march=nehalem or higher, or -mpopcnt if you don't want to enable anything else, if you can limit your code to only running on CPUs that support that x86 instruction):



    #include 

    int popcount_subset(std::bitset<64> A, int pos) {

    int high_bits_to_eliminate = 63 - pos;
    A <<= (high_bits_to_eliminate & 63); // puts A[pos] at A[63].

    return (A[63]? ~0ULL : 0) & A.count(); // most efficient way: great code with gcc and clang
    // see the godbolt link for some #ifdefs with other ways to do the check, like
    // return A[BSET_SIZE-1] ? A.count() : 0;
    }


    This probably isn't optimal on 32bit architectures, so compare other alternatives if you need to make a 32bit build.




    This will work for other sizes of bitset, as long as you do something about the hard-coded 63s, and change the & 63 mask for the shift count into a more general range-check. For optimal performance with strange size bitsets, make a template function with a specialization for size <= register width of the target machine. In that case, extract the bitset to an unsigned type of the appropriate width, and shift to the top of the register instead of the top of the bitset.



    You'd expect this to also generate ideal code for bitset<32>, but it doesn't quite. gcc/clang still use 64bit registers on x86-64.



    For large bitsets, shifting the whole thing will be slower than just popcounting the words below the one containing pos, and using this on that word. (This is where a vectorized popcount really shines on x86 if you can assume SSSE3 but not the popcnt insn hardware support, or for 32bit targets. AVX2 256bit pshufb is the fastest way to do bulk popcounts, but without AVX2 I think 64bit popcnt is pretty close to a 128-bit pshufb implementation. See the comments for more discussion.)



    If you have an array of 64-bit elements, and want to count bits below a certain position in each one separately, then you definitely should use SIMD. The shift parts of this algorithm vectorize, not just the popcnt part. Use psadbw against an all-zero register to horizontal-sum bytes in 64-bit chunks after a pshufb-based popcnt that produces counts for the bits in each byte separately. SSE/AVX doesn't have 64-bit arithmetic right shift, but you can use a different technique to blend on the high bit of each element.







    How I came up with this:



    The asm instructions you want to get the compiler to output will:




    1. remove the unwanted bits from the 64bit value

    2. test the highest of the wanted bits.

    3. popcount it.

    4. return 0 or popcount, depending on the result of the test. (Branchless or branching implementations both have advantages. If the branch is predictable, a branchless implementation tends to be slower.)







    The obvious way to do 1 is to generate a mask ((1<<(pos+1)) -1) and & it. A more efficient way is to left-shift by 63-pos, leaving the bits you want packed at the top of a register.



    This also has the interesting side-effect of putting the bit you want to test as the top bit in the register. Testing the sign bit, rather than any other arbitrary bit, takes slightly fewer instructions. An arithmetic right shift can broadcast the sign bit to the rest of the register, allowing for more-efficient-than-usual branchless code.







    Doing the popcount is a much-discussed problem, but is actually the trickier part of the puzzle. On x86, there is extremely efficient hardware support for it, but only on recent-enough hardware. On Intel CPUs, the popcnt instruction is only available on Nehalem and newer. I forget when AMD added support.



    So to use it safely, you need to either do CPU dispatching with a fallback that doesn't use popcnt. Or, make separate binaries that do/don't depend on some CPU features.



    popcount without the popcnt instruction can be done a few ways. One uses SSSE3 pshufb to implement a 4-bit LUT. This is most effective when used on a whole array, rather than a single 64b at a time, though. Scalar bithacks might be best here, and wouldn't require SSSE3 (and so would be compatible with ancient AMD CPUs that have 64bit but not pshufb.)






    The Bitbroadcast:




    (A[63]? ~0ULL : 0) asks the compiler to broadcast the high bit to all other bit positions, allowing it to be used as an AND-mask to zero (or not) the popcount result. Note that even for large bitset sizes, it's still only masking the output of popcnt, not the bitset itself, so ~0ULL is fine I used ULL to make sure wasn't ever asking the compiler to broadcast the bit only to the low 32b of a register (with UL on Windows, for example).



    This broadcast can be done with an arithmetic right shift by 63, which shifts in copies of the high bit.



    clang generated this code from the original version. After some prodding from Glenn about different implementations for 4, I realized that I could lead gcc towards clang's optimal solution by writing the source more like the ASM I want. The obvious ((int64_t)something) >> 63 to more directly request an arithmetic right shift would not be strictly portable, because signed right-shifts are implementation-defined as either arithmetic or logical. The standard doesn't provide any portable arithmetic right-shift operator. (It's not undefined behaviour, though.) Anyway, fortunately compilers are smart enough: gcc sees the best way once you give it enough of a hint.



    This source makes great code on x86-64 and ARM64 with gcc and clang. Both simply use an arithmetic right shift on the input to popcnt (so the shift can run in parallel with the popcnt). It also compiles great on 32bit x86 with gcc, because the masking only happens to a 32bit variable (after multiple popcnt results are added). It's the rest of the function that's nasty on 32bit (when the bitset is larger than a register).







    Original ternary-operator version with gcc



    Compiled with gcc 5.3.0 -O3 -march=nehalem -mtune=haswell (older gcc, like 4.9.2, also still emit this):



    ; the original ternary-operator version.  See below for the optimal version we can coax gcc into emitting.
    popcount_subset(std::bitset<64ul>, int):
    ; input bitset in rdi, input count in esi (SysV ABI)
    mov ecx, esi ; x86 variable-count shift requires the count in cl
    xor edx, edx ; edx=0
    xor eax, eax ; gcc's workaround for popcnt's false dependency on the old value of dest, on Intel

    not ecx ; two's complement bithack for 63-pos (in the low bits of the register)
    sal rdi, cl ; rdi << ((63-pos) & 63); same insn as shl (arithmetic == logical left shift)
    popcnt rdx, rdi
    test rdi, rdi ; sets SF if the high bit is set.
    cmovs rax, rdx ; conditional-move on the sign flag
    ret


    See How to prove that the C statement -x, ~x+1, and ~(x-1) yield the same results? for background on gcc's use of the -x == ~x + 1 two's complement identity. (And Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted? which tangentially mentions that shl masks the shift count, so we only need the low 6 bits of ecx to hold 63 - pos. Mostly linking that because I wrote it recently and anyone still reading this paragraph might find it interesting.)




    Some of those instructions will go away when inlining. (e.g. gcc would generate the count in ecx in the first place.)



    With Glenn's multiply instead of ternary operator idea (enabled by USE_mul), gcc does



        shr     rdi, 63
    imul eax, edi


    at the end instead of xor / test / cmovs.







    Haswell perf analysis, using microarch data from Agner Fog (Multiply version):




    • mov r,r: 1 fused-domain uop, 0 latency, no execution unit

    • xor-zeroing: 1 fused-domain uop, no execution unit

    • not: 1 uop for p0/p1/p5/p6, 1c latency, 1 per 0.25c throughput

    • shl (aka sal) with count in cl: 3 uops for p0/p6: 2c latency, 1 per 2c throughput. (Agner Fog's data indicates that IvyBridge only takes 2 uops for this, strangely.)

    • popcnt: 1 uop for p1, 3c latency, 1 per 1c throughput


    • shr r,imm: 1 uop for p0/p6, 1c latency. 1 per 0.5c throughput.

    • imul r,r: 1uop for p1, 3c latency.

    • not counting the ret



    Totals:




    • 9 fused-domain uops, can issue in 2.25 cycles (in theory; uop cache-line effects usually bottleneck the frontend slightly).

    • 4 uops (shifts) for p0/p6. 2 uops for p1. 1 any-ALU-port uop. Can execute at one per 2c (saturating the shift ports), so the frontend is the worst bottleneck.




    Latency: Critical path from when the bitset is ready to when the result is: shl(2) -> popcnt(3) -> imul(3). Total 8 cycles. Or 9c from when pos is ready, because the not is an extra 1c latency for it.



    The optimal bitbroadcast version replaces shr with sar (same perf), and imul with and (1c latency instead of 3c, runs on any port). So the only perf change is reducing the critical path latency to 6 cycles. Throughput is still bottlenecked on the frontend. and being able to run on any port doesn't make a difference, unless you're mixing this with code that bottlenecks on port1 (instead of looking at the throughput for running just this code in a tight loop).



    cmov (ternary operator) version: 11 fused-domain uops (frontend: one per 2.75c). execution units: still bottlenecked on the shift ports (p0/p6) at one per 2c. Latency: 7c from bitset to result, 8c from pos to result. (cmov is 2c latency, 2 uops for any of p0/p1/p5/p6.)







    Clang has some different tricks up its sleeve: Instead of test/cmovs, it generates a mask of either all-ones or all-zeros by using an arithmetic right-shift to broadcast the sign bit to all positions of a register. I love it: Using and instead of cmov is more efficient on Intel. It still has the data-dependency and does the work for both sides of the branch (which is the main downside to cmov in general), though. Update: with the right source code, gcc will use this method, too.



    clang 3.7 -O3 -Wall -march=nehalem -mtune=haswell



    popcount_subset(std::bitset<64ul>, int):
    mov ecx, 63
    sub ecx, esi ; larger code size, but faster on CPUs without mov-elimination
    shl rdi, cl ; rdi << ((63-pos) & 63)
    popcnt rax, rdi ; doesn't start a fresh dep chain before this, like gcc does
    sar rdi, 63 ; broadcast the sign bit

    and eax, edi ; eax = 0 or its previous value
    ret


    sar / and replaces xor / test / cmov, and cmov is a 2-uop instruction on Intel CPUs, so that's really nice. (For the ternary-operator version).



    Clang still does the sar / and trick instead of an actual imul when using the multiply source version, or the "bitbroadcast" source version. So those help gcc without hurting clang. (sar/and is definitely better than shr/imul: 2c less latency on the critical path.) The pow_of_two_sub version does hurt clang (see the first godbolt link: omitted from this answer to avoid clutter with ideas that didn't pan out).



    The mov ecx, 63 / sub ecx, esi is actually faster on CPUs without mov-elimination for reg,reg moves (zero latency and no execution port, handled by register renaming). This includes Intel pre-IvyBridge, but not more recent Intel and AMD CPUs.




    Clang's mov imm / sub method puts only one cycle of latency for pos onto the critical path (beyond the bitset->result latency), instead of two for a mov ecx, esi / not ecx on CPUs where mov r,r has 1c latency.






    With BMI2 (Haswell and later), an optimal ASM version can save a mov to ecx. Everything else works the same, because shlx masks its shift-count input register down to the operand-size, just like shl.



    x86 shift instructions have crazy CISC semantics where if the shift count is zero, the flags aren't affected. So variable-count shift instructions have a (potential) dependency on the old value of the flags. "Normal" x86 shl r, cl decodes to 3 uops on Haswell, but BMI2 shlx r, r, r is only 1. So it's too bad that gcc still emits sal with -march=haswell, instead of using shlx (which it does use in some other cases).



    // hand-tuned BMI2 version using the NOT trick and the bitbroadcast
    popcount_subset(std::bitset<64ul>, int):

    not esi ; The low 6 bits hold 63-pos. gcc's two-s complement trick
    xor eax, eax ; break false dependency on Intel. maybe not needed when inlined.
    shlx rdi, rdi, rsi ; rdi << ((63-pos) & 63)
    popcnt rax, rdi
    sar rdi, 63 ; broadcast the sign bit: rdi=0 or -1
    and eax, edi ; eax = 0 or its previous value
    ret


    Perf analysis for Intel Haswell: 6 fused-domain uops (frontend: one per 1.5c). Execution units: 2 p0/p6 shift uops. 1 p1 uop. 2 any-port uops: (one per 1.25c from total execution port limits). Critical path latency: shlx(1) -> popcnt(3) -> and(1) = 5c bitset->result. (or 6c from pos->result).




    Note that when inlining, a human (or smart compiler) could avoid the need for the xor eax, eax. It's only there because of popcnt's false dependency on the output register (on Intel), and we need the output in eax (which the caller may have used recently for a long dep chain). With -mtune=bdver2 or something, gcc won't zero the register it's going to use for popcnt output.



    When inlining, we could use an output register that already has to be ready at least as early as popcnt's source reg to avoid the problem. Compilers will do an in-place popcnt rdi,rdi when the source isn't needed later, but that's not the case here. Instead, we can pick another register that already has to be ready before the source. popcnt's input depends on 63-pos, and we can clobber it, so popcnt rsi,rdi's dependency on rsi can't delay it. Or if we had 63 in a register, we could popcnt rsi,rdi / sarx rax, rsi, reg_63 / and eax, esi. Or BMI2 3-operand shift instructions would also let us not clobber inputs in case they're needed afterwards.






    This is so light-weight that loop overhead and setting up the input operands / storing the results are going to be major factors. (And the 63-pos can optimize away with a compile-time constant, or into wherever a variable count comes from.)







    The Intel compiler amusingly shoots itself in the foot and doesn't take advantage of the fact that A[63] is the sign bit. shl / bt rdi, 63 / jc. It even sets up the branches in a really dumb way. It could zero eax, and then jump over popcnt or not based on the sign flag set by shl.



    An optimal branching implementation, starting from ICC13 output from -O3 -march=corei7 on godbolt:



       // hand-tuned, not compiler output
    mov ecx, esi ; ICC uses neg/add/mov :/
    not ecx
    xor eax, eax ; breaks the false dep, or is the return value in the taken-branch case
    shl rdi, cl

    jns .bit_not_set
    popcnt rax, rdi
    .bit_not_set:
    ret


    That's pretty much optimal: The A[pos] == true case has one not-taken branch. It doesn't save very much over the branchless method, though.



    If the A[pos] == false case is more common: jump over a ret instruction, to a popcnt / ret. (Or after inlining: jump to a block at the end that does the popcnt and jumps back).


    c++ - Does curly brackets matter for empty constructor?

    Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...

    $cell

    Blog Archive