问:
Templated iterator for a std::vector
I am creating a utility function to combine a byte array into a value based on the template type.

I currently have two overloaded functions which accept a pair of forward iterators and a pair of reverse iterators, in order to do take care of big and little endian.

Is it possible to have a templated vector iterator and reduce my two functions into one?

For example, this is what I want to do:

```<code>template <typename T, typename Iter>
inline T combine_bytes(std::vector<unsigned char>::Iter const &start,
                       std::vector<unsigned char>::Iter const &end,
                       bool bigendian)
{
    T ret;
    std::copy(start, end, reinterpret_cast<unsigned char *>(&ret));

    return ret;
}
</code>```

The compilation error I get is:

```<code>Failed to specialize function template 'T Utils::combine_bytes(const Iter &, const Iter &)'
</code>```

答:
I suppose that, using SFINAE, you can write something as follows

```<code>template <typename T, typename Iter>
inline typename std::enable_if<
      std::is_same<Iter, std::vector<unsigned char>::iterator>{}
   || std::is_same<Iter, std::vector<unsigned char>::reverse_iterator>{},
   T >::type combine_bytes (Iter const & start,
                            Iter const & end,
                            bool bigendian)
{
    T ret;
    std::copy(start, end, reinterpret_cast<unsigned char *>(&ret));

    return ret;
}
</code>```

that is: define the function for a generic template type Iter and enable it only if Iter is std::vector<unsigned char>::iterator or std::vector<unsigned char>::reverse_iterator

Title: Templated iterator for a std::vector

Body:
I am creating a utility function to combine a byte array into a value based on the template type.

I currently have two overloaded functions which accept a pair of forward iterators and a pair of reverse iterators, in order to do take care of big and little endian.

Is it possible to have a templated vector iterator and reduce my two functions into one?

For example, this is what I want to do:

template <typename T, typename Iter>
inline T combine_bytes(std::vector<unsigned char>::Iter const &start,
                       std::vector<unsigned char>::Iter const &end,
                       bool bigendian)
{
    T ret;
    std::copy(start, end, reinterpret_cast<unsigned char *>(&ret));

    return ret;
}

The compilation error I get is:

Failed to specialize function template 'T Utils::combine_bytes(const Iter &, const Iter &)'

Body:
I suppose that, using SFINAE, you can write something as follows

template <typename T, typename Iter>
inline typename std::enable_if<
      std::is_same<Iter, std::vector<unsigned char>::iterator>{}
   || std::is_same<Iter, std::vector<unsigned char>::reverse_iterator>{},
   T >::type combine_bytes (Iter const & start,
                            Iter const & end,
                            bool bigendian)
{
    T ret;
    std::copy(start, end, reinterpret_cast<unsigned char *>(&ret));

    return ret;
}

that is: define the function for a generic template type Iter and enable it only if Iter is std::vector<unsigned char>::iterator or std::vector<unsigned char>::reverse_iterator


来源: stackexchange

问:
MySQL PHP Pagination - Getting the same results on every page or no results at all
I'm trying to query data from MySQL. I need the data table to be paginated. See  the code below; you can see that I use "OFFSET" after the limit. I can also use ","

However, I tried both but OFFSET does not bring up any results and "," brings up results but the same 10 results no matter which page I'm on.

Here is the code:

// define how many results you want per page
$results_per_page = 10;

// find out the number of results stored in database
$sql='SELECT * FROM ETF';
$result = mysqli_query($con,$sql);

$number_of_results = mysqli_num_rows($result);

// determine number of total pages available
$number_of_pages = ceil($number_of_results/$results_per_page);

// determine which page number visitor is currently on
if (!isset($_GET["page"])) {
  $page = 1;
} else {
  $page = $_GET["page"];
}

// determine the sql LIMIT starting number for the results on the displaying page
$this_page_first_result = ($page-1)*$results_per_page;

// retrieve selected results from database and display them on page
$sql='SELECT * FROM ETF LIMIT '. $this_page_first_result . " OFFSET " . $results_per_page;

$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_assoc($result)) {
  echo $row['ETF'] . ' ' . $row['ETF NAME']. '
';
}

// display the links to the pages
for ($page=1;$page<=$number_of_pages;$page++) {
  echo '' . $page. '';
}
```

答:
I'm not sure what you mean by using "," but:

The LIMIT should be 10 - i.e. $results_per_page (your constant)

The OFFSET should be dynamic depending on what page you're on. On page 1, OFFSET=0 (or you can omit OFFSET on page 1 if you want). On page 2, the OFFSET in your example should be 10.

Where is your ORDER BY clause? It's important to do this for LIMIT/OFFSET to work consistently.

I'm not sure if this is your final production code, but just a side note: in production, never use $_GET directly in your query. You aren't at the moment, but depending on what you do for the above point, make sure to verify that your $_GET parameter is an integer before you do anything with it, to guard against SQL injection.

ContentLicense: CC BY-SA 3.0


问:
How to access Vue.js component data from a method?
Based on vuejs docs I would expect the following code to work, but this is null when I click the button:

```<code><template>
    <div class="hello">
        <button v-on:click="test">Test </button>
    </div>
</template>

<script>
    export default {
  name: 'hello',
    methods: {
        test: () => {
            console.log('log data.message', this.msg);
        }
    },
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  }
}
</script>
</code>```

Here is the full demo: https://codesandbox.io/s/vq3ovx9n70

答:
Change your test() method to:

```<code>test: function() {
        console.log('log data.message', this.msg);
    }
</code>```

Arrow functions don't bind this the way you want here.

ContentLicense: CC BY-SA 4.0

Body:
Based on vuejs docs I would expect the following code to work, but this is null when I click the button:

<template>

        <button v-on:click="test">Test </button>

</template>

Here is the full demo: https://codesandbox.io/s/vq3ovx9n70

Body:
Change your test() method to:

test: function() {
        console.log('log data.message', this.msg);
    }

Arrow functions don't bind this the way you want here.


问:
How to transfer time from one view controller to another?
in my current project I have implemented a timer on one view.
When I click the button that leads to the next view, I want it to show the time the previous view had at the end.

This is my current code:

```<code>import UIKit

class ViewController2: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var myTableView: UITableView!

@IBOutlet weak var labelMinute: UILabel!
@IBOutlet weak var labelHour: UILabel!
@IBOutlet weak var labelSecond: UILabel!

weak var timer: Timer?
var startTime: Double = 0
var time: Double = 0
var elapsed: Double = 0
var status: Bool = false

var psgTextField: UITextField?
var emTextField: UITextField?

public var passengers2 = [""]

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return passengers2.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell2 = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell2")
    cell2.textLabel?.text = passengers2[indexPath.row]

    return (cell2)
}

@objc func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){

    if editingStyle == UITableViewCellEditingStyle.delete {

        let alertController = UIAlertController(title: "Do you really want to leave?",
                                                message: nil,
                                                preferredStyle: .alert)

        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

        alertController.addAction(UIAlertAction(title: "Leave", style: .default, handler: { action in self.passengers2.remove(at: indexPath.row)
            self.myTableView.deleteRows(at: [indexPath], with: .automatic)
        } ))

        self.present(alertController, animated: true)

    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    startTime = Date().timeIntervalSinceReferenceDate - elapsed
    timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)

    status = true

}

@objc func updateCounter() {

    // Calculate total time since timer started in seconds
    time = Date().timeIntervalSinceReferenceDate - startTime

    // Calculate minutes
    let minutes = UInt8(time / 60.0)
    time -= (TimeInterval(minutes) * 60)

    // Calculate seconds
    let hours = UInt8(time / 3600)
    time -= TimeInterval(hours)

    let seconds = UInt8(time)
    time -= TimeInterval(seconds)

    // Format time vars with leading zero
    let strMinutes = String(format: "%02d", minutes)
    let strHour = String(format: "%02d", hours)
    let strSecond = String(format: "%02d", seconds)

    // Add time vars to relevant labels
    labelMinute.text = strMinutes
    labelHour.text = strHour
    labelSecond.text = strSecond

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let thirdController = segue.destination as! ViewController3
    thirdController.timeHour.text = labelHour.text

    let fourthController = segue.destination as! ViewController3
    fourthController.timeMinute.text = labelMinute.text
}
}
</code>```

Without the last "override" function, the timer on my first view works perfectly, but for some reason it won't transfer the time to the labels on my second view ("timeHour", "timeMinute").

Xcode gives me the errorThread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0), but the cases I found in the internet were different from mine.

Does someone know where the problem is?

答:
First of all (and this is not the real problem, but I need to say it anyway), your code is riddled with exclamation marks. Any of them might cause a crash. Don't use exclamation marks! (Like that one.) Your labelHour could be nil. Your labelMinute could be nil. Your segue destination might not be a ViewController3. All of that code is very dangerous! You need to check all of those things safely, not use exclamation marks.

However, the real problem is this line:

```<code>thirdController.timeHour.text = // ...
</code>```

Your thirdController has not yet loaded its view, so it has no timeHour label yet.

Your timing here is all off. But even worse, the whole spirit of that line is wrong; you must never interfere with another view controller's interface. You need properties in ViewController3 that you can set, and ViewController3 can pick up those values in its viewDidLoad and configure its own interface.

Body:
in my current project I have implemented a timer on one view.
When I click the button that leads to the next view, I want it to show the time the previous view had at the end.

This is my current code:

import UIKit

class ViewController2: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var myTableView: UITableView!

@IBOutlet weak var labelMinute: UILabel!
@IBOutlet weak var labelHour: UILabel!
@IBOutlet weak var labelSecond: UILabel!

weak var timer: Timer?
var startTime: Double = 0
var time: Double = 0
var elapsed: Double = 0
var status: Bool = false

var psgTextField: UITextField?
var emTextField: UITextField?

public var passengers2 = [""]

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return passengers2.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell2 = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell2")
    cell2.textLabel?.text = passengers2[indexPath.row]

    return (cell2)
}

@objc func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){

    if editingStyle == UITableViewCellEditingStyle.delete {

        let alertController = UIAlertController(title: "Do you really want to leave?",
                                                message: nil,
                                                preferredStyle: .alert)

        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

        alertController.addAction(UIAlertAction(title: "Leave", style: .default, handler: { action in self.passengers2.remove(at: indexPath.row)
            self.myTableView.deleteRows(at: [indexPath], with: .automatic)
        } ))

        self.present(alertController, animated: true)

    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    startTime = Date().timeIntervalSinceReferenceDate - elapsed
    timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)

    status = true

}

@objc func updateCounter() {

    // Calculate total time since timer started in seconds
    time = Date().timeIntervalSinceReferenceDate - startTime

    // Calculate minutes
    let minutes = UInt8(time / 60.0)
    time -= (TimeInterval(minutes) * 60)

    // Calculate seconds
    let hours = UInt8(time / 3600)
    time -= TimeInterval(hours)

    let seconds = UInt8(time)
    time -= TimeInterval(seconds)

    // Format time vars with leading zero
    let strMinutes = String(format: "%02d", minutes)
    let strHour = String(format: "%02d", hours)
    let strSecond = String(format: "%02d", seconds)

    // Add time vars to relevant labels
    labelMinute.text = strMinutes
    labelHour.text = strHour
    labelSecond.text = strSecond

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let thirdController = segue.destination as! ViewController3
    thirdController.timeHour.text = labelHour.text

    let fourthController = segue.destination as! ViewController3
    fourthController.timeMinute.text = labelMinute.text
}
}

Without the last "override" function, the timer on my first view works perfectly, but for some reason it won't transfer the time to the labels on my second view ("timeHour", "timeMinute").

Xcode gives me the errorThread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0), but the cases I found in the internet were different from mine.

Does someone know where the problem is?

Body:
First of all (and this is not the real problem, but I need to say it anyway), your code is riddled with exclamation marks. Any of them might cause a crash. Don't use exclamation marks! (Like that one.) Your labelHour could be nil. Your labelMinute could be nil. Your segue destination might not be a ViewController3. All of that code is very dangerous! You need to check all of those things safely, not use exclamation marks.

However, the real problem is this line:

thirdController.timeHour.text = // ...

Your thirdController has not yet loaded its view, so it has no timeHour label yet.

Your timing here is all off. But even worse, the whole spirit of that line is wrong; you must never interfere with another view controller's interface. You need properties in ViewController3 that you can set, and ViewController3 can pick up those values in its viewDidLoad and configure its own interface.


问:
Jenkins Plugins are missing in Available tab
While setting up the Jenkins, I skipped the plugins installation in the wizard. Now I am trying to install it manually. I searched a lot and came to know that I can install it through the web in Manage Jenkins -> Plugin Manager -> Available tab. No plugins are available here. I even typed plugin names in filter but still could not succeed.

Here I am trying to install Git Plugin.

When I click on Check now button, It throws some below Java exception.

Full Stack Trace

```<code>sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
Caused: sun.security.validator.ValidatorException: PKIX path building failed
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
Caused: javax.net.ssl.SSLHandshakeException
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1513)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at hudson.model.DownloadService.loadJSON(DownloadService.java:167)
    at hudson.model.UpdateSite.updateDirectlyNow(UpdateSite.java:190)
    at hudson.PluginManager.doCheckUpdatesServer(PluginManager.java:1624)
    at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:627)
    at org.kohsuke.stapler.Function$MethodFunction.invoke(Function.java:343)
    at org.kohsuke.stapler.interceptor.RequirePOST$Processor.invoke(RequirePOST.java:52)
    at org.kohsuke.stapler.PreInvokeInterceptedFunction.invoke(PreInvokeInterceptedFunction.java:26)
    at org.kohsuke.stapler.Function.bindAndInvoke(Function.java:184)
    at org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:117)
    at org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:129)
    at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:58)
    at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:715)
    at org.kohsuke.stapler.Stapler.invoke(Stapler.java:845)
    at org.kohsuke.stapler.MetaClass$2.doDispatch(MetaClass.java:186)
    at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:58)
    at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:715)
    at org.kohsuke.stapler.Stapler.invoke(Stapler.java:845)
    at org.kohsuke.stapler.Stapler.invoke(Stapler.java:649)
    at org.kohsuke.stapler.Stapler.service(Stapler.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:135)
    at hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:138)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:85)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)
    at hudson.security.UnwrapSecurityExceptionFilter.doFilter(UnwrapSecurityExceptionFilter.java:51)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at jenkins.security.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:117)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:142)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:271)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at jenkins.security.BasicHeaderProcessor.doFilter(BasicHeaderProcessor.java:93)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:249)
    at hudson.security.HttpSessionContextIntegrationFilter2.doFilter(HttpSessionContextIntegrationFilter2.java:67)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:90)
    at hudson.security.HudsonFilter.doFilter(HudsonFilter.java:171)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.kohsuke.stapler.compression.CompressionFilter.doFilter(CompressionFilter.java:49)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at hudson.util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:82)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.kohsuke.stapler.DiagnosticThreadNameFilter.doFilter(DiagnosticThreadNameFilter.java:30)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:616)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2527)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2516)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
</code>```

Can somebody assist me with the plugin installation?

答:
What did work for me is, I replaced the https by http in the URL in the hudson.model.UpdateCenter XML file. This file resides in Jenkins root directory. Below is how it looks now.

```<code><?xml version='1.0' encoding='UTF-8'?>
<sites>
  <site>
    <id>default</id>
    <url>http://updates.jenkins.io/update-center.json</url>
  </site>
</sites>
</code>```

Look at the URL.

Body:
While setting up the Jenkins, I skipped the plugins installation in the wizard. Now I am trying to install it manually. I searched a lot and came to know that I can install it through the web in Manage Jenkins -> Plugin Manager -> Available tab. No plugins are available here. I even typed plugin names in filter but still could not succeed.

Here I am trying to install Git Plugin.

When I click on Check now button, It throws some below Java exception.

Full Stack Trace

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
Caused: sun.security.validator.ValidatorException: PKIX path building failed
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
Caused: javax.net.ssl.SSLHandshakeException
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1513)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at hudson.model.DownloadService.loadJSON(DownloadService.java:167)
    at hudson.model.UpdateSite.updateDirectlyNow(UpdateSite.java:190)
    at hudson.PluginManager.doCheckUpdatesServer(PluginManager.java:1624)
    at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:627)
    at org.kohsuke.stapler.Function$MethodFunction.invoke(Function.java:343)
    at org.kohsuke.stapler.interceptor.RequirePOST$Processor.invoke(RequirePOST.java:52)
    at org.kohsuke.stapler.PreInvokeInterceptedFunction.invoke(PreInvokeInterceptedFunction.java:26)
    at org.kohsuke.stapler.Function.bindAndInvoke(Function.java:184)
    at org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:117)
    at org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:129)
    at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:58)
    at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:715)
    at org.kohsuke.stapler.Stapler.invoke(Stapler.java:845)
    at org.kohsuke.stapler.MetaClass$2.doDispatch(MetaClass.java:186)
    at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:58)
    at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:715)
    at org.kohsuke.stapler.Stapler.invoke(Stapler.java:845)
    at org.kohsuke.stapler.Stapler.invoke(Stapler.java:649)
    at org.kohsuke.stapler.Stapler.service(Stapler.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:135)
    at hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:138)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:85)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)
    at hudson.security.UnwrapSecurityExceptionFilter.doFilter(UnwrapSecurityExceptionFilter.java:51)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at jenkins.security.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:117)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:142)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:271)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at jenkins.security.BasicHeaderProcessor.doFilter(BasicHeaderProcessor.java:93)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:249)
    at hudson.security.HttpSessionContextIntegrationFilter2.doFilter(HttpSessionContextIntegrationFilter2.java:67)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:90)
    at hudson.security.HudsonFilter.doFilter(HudsonFilter.java:171)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.kohsuke.stapler.compression.CompressionFilter.doFilter(CompressionFilter.java:49)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at hudson.util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:82)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.kohsuke.stapler.DiagnosticThreadNameFilter.doFilter(DiagnosticThreadNameFilter.java:30)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:616)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2527)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2516)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

Can somebody assist me with the plugin installation?

Body:
What did work for me is, I replaced the https by http in the URL in the hudson.model.UpdateCenter XML file. This file resides in Jenkins root directory. Below is how it looks now.

<?xml version='1.0' encoding='UTF-8'?>
<sites>
  <site>
    <id>default</id>
    <url>http://updates.jenkins.io/update-center.json</url>
  </site>
</sites>

Look at the URL.


问:
Calling asmx service from .net core api2
i need to call an old asmx web service (I have not control over)from a .net core web api

is it possible?

thanks

答:
The way I was able to do it was fully update Visual Studio 2017, then get the latest WCF Connected Services extension from here:
https://marketplace.visualstudio.com/items?itemName=WCFCORETEAM.VisualStudioWCFConnectedService

After I did that, I was able to add and call the SOAP service like so.

Right click on Connected Services folder (should appear just below your project) and select Add Connected Service. Then select "Add a WCF web service". Put your asmx url into the URI field, set the name space and click Finish. You will now have the reference set.

An example of what my code does...

If you don't have an authorization header, you can ignore that part:

```<code>MySoapClient.EndpointConfiguration endpoint = new MySoapClient.EndpointConfiguration();
MySoapClient myService = new MySoapClient(endpoint, myURL);
AuthorizationSoapHeader MyAuthHeader = new AuthorizationSoapHeader();

MyAuthHeader.AppName = FDSServiceAppName;
MyAuthHeader.AppID = Guid.Parse(MyAppID);

Entry[] entries = MyService.GetUsers().Result;
</code>```

Body:
The way I was able to do it was fully update Visual Studio 2017, then get the latest WCF Connected Services extension from here:
https://marketplace.visualstudio.com/items?itemName=WCFCORETEAM.VisualStudioWCFConnectedService

After I did that, I was able to add and call the SOAP service like so.

Right click on Connected Services folder (should appear just below your project) and select Add Connected Service. Then select "Add a WCF web service". Put your asmx url into the URI field, set the name space and click Finish. You will now have the reference set.

An example of what my code does...

If you don't have an authorization header, you can ignore that part:

MySoapClient.EndpointConfiguration endpoint = new MySoapClient.EndpointConfiguration();
MySoapClient myService = new MySoapClient(endpoint, myURL);
AuthorizationSoapHeader MyAuthHeader = new AuthorizationSoapHeader();

MyAuthHeader.AppName = FDSServiceAppName;
MyAuthHeader.AppID = Guid.Parse(MyAppID);

Entry[] entries = MyService.GetUsers().Result;


问:
Should I redefine or reassign a variable in a loop?
Supposing I need to iterate over something and set a variable in every cycle, like this:

```<code>for (int i=0; i < list.size(); i++) {
    final Object cur = list.get(i);
}
</code>```

this redefines the variable everytime, so I'm concerned that that might pollute the memory.

The alternate option is to define the variable once and then reassign it every iteration, like this:

```<code>Object cur
for (int i=0; i < list.size(); i++) {
    cur = list.get(i);
}
</code>```

would this be better in terms of memory? Would it make any difference? What if cur is a primitive type instead of an Object?

Don't tell me to use foreach, I need the counter, this is just a simplified example

答: I would recommend you use your alternate approach - declaration of a variable - primitive or object outside the loop. However, I notice in your first approach that you are declaring the object with final keyword. This should give a hint to the compiler to optimize the code in a way that the memory utilization at the run time is as less as possible. But you shouldn't rely on that. Just use your alternate approach.

Body:
Supposing I need to iterate over something and set a variable in every cycle, like this:

for (int i=0; i < list.size(); i++) {
    final Object cur = list.get(i);
}

this redefines the variable everytime, so I'm concerned that that might pollute the memory.

The alternate option is to define the variable once and then reassign it every iteration, like this:

Object cur
for (int i=0; i < list.size(); i++) {
    cur = list.get(i);
}

would this be better in terms of memory? Would it make any difference? What if cur is a primitive type instead of an Object?

Don't tell me to use foreach, I need the counter, this is just a simplified example


问:
Impala - Find what tables have a specific column
In Impala, is there a way to check which tables in the database contain a specific column name?

Something like:

```<code>select tablename, columnname
from dbc.columns
where databasename = 'mydatabasename'
and columnname like '%findthis%'
order by tablename
</code>```

The above query works in a teradata environment, but throws an error in Impala.

Thanks,

答:
Impala shares the metastore with Hive. Unlike traditional RDBMS, Hive metadata is stored in a separate database. In most cases it is in MySQL or Postgres. If you have access to the metastore database, you can run SELECT on table TBLS to get the details about the tables and COLUMNS_V2 to get the details about columns.

If you do not have access to the metastore, the only option is to describe each table to get the column names. If you have a lot of databases and tables, you could write a shell script to get the list of tables using "show tables" and loop around the tables to describe them using "desc tablename".

Body:
In Impala, is there a way to check which tables in the database contain a specific column name?

Something like:

select tablename, columnname
from dbc.columns
where databasename = 'mydatabasename'
and columnname like '%findthis%'
order by tablename

The above query works in a teradata environment, but throws an error in Impala.

Thanks,


问:
Yacc actions return 0 for any variable ($)
I'm new to Lex/Yacc. Found these lex & yacc files which parse ansi C.

To experiment I added an action to print part of the parsing:

```<code>constant
    : I_CONSTANT    { printf("I_CONSTANT %d\n", $1); }
    | F_CONSTANT
    | ENUMERATION_CONSTANT  /* after it has been defined as such */
    ;
</code>```

The problem is, no matter where I put the action, and whatever $X I use, I always get value 0.

Here I got printed:

```<code>I_CONSTANT 0
</code>```

Even though my input is:

```<code>int foo(int x)
{
    return 5;
}
</code>```

Any idea?

答: Turns out yylval = atoi(yytext) is not done in the lex file, so I had to add it myself. Also learned I can add extern char *yytext to the yacc file header, and then use yytext directly.

Body:
I'm new to Lex/Yacc. Found these lex & yacc files which parse ansi C.

To experiment I added an action to print part of the parsing:

constant
    : I_CONSTANT    { printf("I_CONSTANT %d\n", $1); }
    | F_CONSTANT
    | ENUMERATION_CONSTANT  /* after it has been defined as such */
    ;

The problem is, no matter where I put the action, and whatever $X I use, I always get value 0.

Here I got printed:

I_CONSTANT 0

Even though my input is:

int foo(int x)
{
    return 5;
}

Any idea?


问:
Convert object to array with Lodash _.map and use map function in React
I encountered an issue when I used map function in React to iterate data because the data is object. So I tried to use _.map function of Lodash to convert from object to array. Then I want to iterate the data using mapfunction.

It didn't work as I had expected and I'm wondering what is the problem.

Should it be working? It is a ok to use _.map to iterate in React?

The data structure

The code

```<code>import React, { Component } from 'react';
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import { addData } from "../actions/index";
import _ from "lodash";

class List extends Component{
    componentDidMount(){
        this.props.addData()
    }
    render(){
        let item;
        if(this.props.data) {
          item = _.map(this.props.data, data => {

         return (
             <li key={data.id}>
             {data.title}
             </li>
         /* this returns empty data. Only bullet button appears.
         In console, it says [Each child in an array or iterator should have a unique "key" prop.]
         Even though I added a key value. */
         )
     })
    }

return(
    <div>{item}</div>
)
}
}

function mapStateToProps(state){
    return {
    data : state.data
}
}

export default connect(mapStateToProps, {addData})(Search);
</code>```

UPDATE:

To debug, did a console.log(this.props.data), which produced this output:

```<code>Object { data: Array[100] }
</code>```

答:
Based on your console.log(this.props.data) output, we can see that value assigned to this.props.data is:

{ data : [ /* fetched objects... */ ] }
```

<p>That array you're trying to call <code>map</code> on is a level deeper than you expected, so indeed it looks like you're not correctly selecting the fetched and stored data in your <code>mapStateToProps</code>.</p>

<p>Try this instead:</p>

<pre class="lang-js prettyprint-override"><code>function mapStateToProps(state){
  return {
    data : state.data.data
  }
}
</code>```

Body:
I encountered an issue when I used map function in React to iterate data because the data is object. So I tried to use _.map function of Lodash to convert from object to array. Then I want to iterate the data using mapfunction.

It didn't work as I had expected and I'm wondering what is the problem.

Should it be working? It is a ok to use _.map to iterate in React?

The data structure

The code

import React, { Component } from 'react';
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import { addData } from "../actions/index";
import _ from "lodash";

class List extends Component{
    componentDidMount(){
        this.props.addData()
    }
    render(){
        let item;
        if(this.props.data) {
          item = _.map(this.props.data, data => {

         return (

             {data.title}

         /* this returns empty data. Only bullet button appears.
         In console, it says [Each child in an array or iterator should have a unique "key" prop.]
         Even though I added a key value. */
         )
     })
    }

return(

{item}

)
}
}

function mapStateToProps(state){
    return {
    data : state.data
}
}

export default connect(mapStateToProps, {addData})(Search);

UPDATE:

To debug, did a console.log(this.props.data), which produced this output:

Object { data: Array[100] }

Body:
Based on your console.log(this.props.data) output, we can see that value assigned to this.props.data is:

{ data : [ /* fetched objects... */ ] }

That array you're trying to call map on is a level deeper than you expected, so indeed it looks like you're not correctly selecting the fetched and stored data in your mapStateToProps.

Try this instead:

function mapStateToProps(state){
  return {
    data : state.data.data
  }
}


问:
Social Network PHP/MySQL Structure
I want to create a social media site. I'm just planning it out at the moment, i'm thinking first to log the user in by username/password combo, checking against a mysql db and seeing if they exist. Thats simple enough. But i'm asking is, like Facebook, for a "Bio" page, would you put stuff like "select from friends where user_id = $user_id", that sort of thing? So do i use the same user_id over the whole page for selecting comments, contact info, friends, pictures, likes, dislikes? Is this right or is this poor application structure?</p>

答:
When you log them in, get their userID from the database (I'm assuming you have an integral user id as well as their username/password in the user table) and store it in the session. You can then use that on all the pages in your site to do queries. If you were to base it on the query string or post data, people can easily change that.

This is a pretty standard way of doing things (in my experience).

You could also store a whole user object in a session variable, but then you'd have to worry about keeping it up-to-date.

Title: Social Network PHP/MySQL Structure

ContentLicense: CC BY-SA 2.5


答:
From a performance point of view, I would say if you need to improve this (you measured that a hot spot was here), you can choose the solution that offers best performances.

That said, I would recommend putting it inside the loop so that you cannot use it by error outside.

BTW, you'll ease debugging and readability by not having a variable that is there without knowing if it should or not be used outside the loop.

Title: Should I redefine or reassign a variable in a loop?


问:
Compute symmetric surface distances [Python]
I want to compute surface distance metrics between 2 binary objects, aka segmentations of liver tumors. I am looking to compute:

Average symmetric surface distance

Root mean square symmetric distance

Hausdorff distance (also known as maximum symmetric distance)

I found two libraries that could help me compute those metrics, but I am getting conflicting results, so i am confused how they work.

SimpleITK:https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1SignedMaurerDistanceMapImageFilter.html
https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1HausdorffDistanceImageFilter.html

MedPy:
https://github.com/loli/medpy/. DOCS

This is my code for Simple ITK and MedPy.

```<code>from medpy import metric
import pandas as pd
import SimpleITK as sitk
import numpy as np
reference_segmentation = sitk.ReadImage('tumorSegm', sitk.sitkUInt8)
segmentation = sitk.ReadImage('tumorSegm2',sitk.sitkUInt8)
class SurfaceDistanceMeasuresITK(Enum):
    hausdorff_distance, max_surface_distance, avg_surface_distance, median_surface_distance, std_surface_distance = range(5)

class MedpyMetricDists(Enum):
    hausdorff_distance, avg_surface_distance, avg_symmetric_surface_distance = range(3)

  surface_distance_results = np.zeros((1,len(SurfaceDistanceMeasuresITK.__members__.items())))
surface_dists_Medpy = np.zeros((1,len(MedpyMetricDists.__members__.items())))
segmented_surface = sitk.LabelContour(segmentation)

# init signed mauerer distance as reference metrics
reference_distance_map = sitk.Abs(sitk.SignedMaurerDistanceMap(reference_segmentation, squaredDistance=False, useImageSpacing=True))

label_intensity_statistics_filter = sitk.LabelIntensityStatisticsImageFilter()
label_intensity_statistics_filter.Execute(segmented_surface, reference_distance_map)

hausdorff_distance_filter = sitk.HausdorffDistanceImageFilter()
hausdorff_distance_filter.Execute(reference_segmentation, segmentation)

surface_distance_results[0,SurfaceDistanceMeasuresITK.hausdorff_distance.value] = hausdorff_distance_filter.GetHausdorffDistance()
surface_distance_results[0,SurfaceDistanceMeasuresITK.max_surface_distance.value] = label_intensity_statistics_filter.GetMaximum(label)
surface_distance_results[0,SurfaceDistanceMeasuresITK.avg_surface_distance.value] = label_intensity_statistics_filter.GetMean(label)
surface_distance_results[0,SurfaceDistanceMeasuresITK.median_surface_distance.value] = label_intensity_statistics_filter.GetMedian(label)
surface_distance_results[0,SurfaceDistanceMeasuresITK.std_surface_distance.value] = label_intensity_statistics_filter.GetStandardDeviation(label)

surface_distance_results_df = pd.DataFrame(data=surface_distance_results, index = list(range(1)),
                              columns=[name for name, _ in SurfaceDistanceMeasuresITK.__members__.items()])

img_array = sitk.GetArrayFromImage(reference_segmentation)
seg_array = sitk.GetArrayFromImage(segmentation)
# reverse array in the order x, y, z
img_array_rev = np.flip(img_array,2)
seg_array_rev = np.flip(seg_array,2)
vxlspacing = segmentation.GetSpacing()

surface_dists_Medpy[0,MedpyMetricDists.hausdorff_distance.value] = metric.binary.hd(seg_array_rev,img_array_rev, voxelspacing=vxlspacing)
surface_dists_Medpy[0,MedpyMetricDists.avg_surface_distance.value] = metric.binary.asd(seg_array_rev,img_array_rev, voxelspacing=vxlspacing)
surface_dists_Medpy[0,MedpyMetricDists.avg_symmetric_surface_distance.value] = metric.binary.assd(seg_array_rev,img_array_rev, voxelspacing=vxlspacing)

surface_dists_Medpy_df = pd.DataFrame(data=surface_dists_Medpy, index = list(range(1)),
                              columns=[name for name, _ in MedpyMetricDists.__members__.items()])
</code>```

At first sight I don't think that SimpleITK computes the
symmetric distances. Is there any implementation for those in that library? How can I obtain them?

Is MedPy a reliable library? Can I calculate the symmetric root mean
square with it?

Other recommendations of libraries for computing surface distance
metrics?

Should I compute the Absolute Values for the Mauerer Distances Map? I am not sure how it affects the results.   reference_distance_map = sitk.Abs(sitk.SignedMaurerDistanceMap(reference_segmentation, squaredDistance=False, useImageSpacing=True))

答:
@Roxanne

I am assuming here that you are being confused by the surface distance measures computed in this SimpleITK notebook?

The rest of the answer refers to that code.

The mean/std/median/maximum are not symmetric (Hausdorff is).

Using SimpleITK you can compute the symmetric mean and standard deviation by
computing the mean and standard deviation for the segmentation and then for the reference (the code does it for the segmentation, so just switch roles and you get it for the reference).

Now you have the mean and standard deviations from two samples. To get the size of a sample just call:

```<code>label_intensity_statistics_filter.GetNumberOfPixels(label)
</code>```

To compute the symmetric mean and standard deviation from knowlege of n1,m1,s1, n2,m2,s2:

```<code>m = (n1*m1 + n2*m2)/(n1+n2)
s = np.sqrt((n1*(s1**2+(m1-m)**2) + n2*(s2**2+(m2-m)**2))/(n1+n2))
</code>```

Note that the sample estimate for the standard deviation is the biased version (similar to the default behavior of numpy.std).

If you have additional questions please post to the ITK discourse forum.

Body:
I want to compute surface distance metrics between 2 binary objects, aka segmentations of liver tumors. I am looking to compute:

Average symmetric surface distance

Root mean square symmetric distance

Hausdorff distance (also known as maximum symmetric distance)

I found two libraries that could help me compute those metrics, but I am getting conflicting results, so i am confused how they work.

SimpleITK:https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1SignedMaurerDistanceMapImageFilter.html
https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1HausdorffDistanceImageFilter.html

MedPy:
https://github.com/loli/medpy/. DOCS

This is my code for Simple ITK and MedPy.

from medpy import metric
import pandas as pd
import SimpleITK as sitk
import numpy as np
reference_segmentation = sitk.ReadImage('tumorSegm', sitk.sitkUInt8)
segmentation = sitk.ReadImage('tumorSegm2',sitk.sitkUInt8)
class SurfaceDistanceMeasuresITK(Enum):
    hausdorff_distance, max_surface_distance, avg_surface_distance, median_surface_distance, std_surface_distance = range(5)

class MedpyMetricDists(Enum):
    hausdorff_distance, avg_surface_distance, avg_symmetric_surface_distance = range(3)

  surface_distance_results = np.zeros((1,len(SurfaceDistanceMeasuresITK.__members__.items())))
surface_dists_Medpy = np.zeros((1,len(MedpyMetricDists.__members__.items())))
segmented_surface = sitk.LabelContour(segmentation)

# init signed mauerer distance as reference metrics
reference_distance_map = sitk.Abs(sitk.SignedMaurerDistanceMap(reference_segmentation, squaredDistance=False, useImageSpacing=True))

label_intensity_statistics_filter = sitk.LabelIntensityStatisticsImageFilter()
label_intensity_statistics_filter.Execute(segmented_surface, reference_distance_map)

hausdorff_distance_filter = sitk.HausdorffDistanceImageFilter()
hausdorff_distance_filter.Execute(reference_segmentation, segmentation)

surface_distance_results[0,SurfaceDistanceMeasuresITK.hausdorff_distance.value] = hausdorff_distance_filter.GetHausdorffDistance()
surface_distance_results[0,SurfaceDistanceMeasuresITK.max_surface_distance.value] = label_intensity_statistics_filter.GetMaximum(label)
surface_distance_results[0,SurfaceDistanceMeasuresITK.avg_surface_distance.value] = label_intensity_statistics_filter.GetMean(label)
surface_distance_results[0,SurfaceDistanceMeasuresITK.median_surface_distance.value] = label_intensity_statistics_filter.GetMedian(label)
surface_distance_results[0,SurfaceDistanceMeasuresITK.std_surface_distance.value] = label_intensity_statistics_filter.GetStandardDeviation(label)

surface_distance_results_df = pd.DataFrame(data=surface_distance_results, index = list(range(1)),
                              columns=[name for name, _ in SurfaceDistanceMeasuresITK.__members__.items()])

img_array = sitk.GetArrayFromImage(reference_segmentation)
seg_array = sitk.GetArrayFromImage(segmentation)
# reverse array in the order x, y, z
img_array_rev = np.flip(img_array,2)
seg_array_rev = np.flip(seg_array,2)
vxlspacing = segmentation.GetSpacing()

surface_dists_Medpy[0,MedpyMetricDists.hausdorff_distance.value] = metric.binary.hd(seg_array_rev,img_array_rev, voxelspacing=vxlspacing)
surface_dists_Medpy[0,MedpyMetricDists.avg_surface_distance.value] = metric.binary.asd(seg_array_rev,img_array_rev, voxelspacing=vxlspacing)
surface_dists_Medpy[0,MedpyMetricDists.avg_symmetric_surface_distance.value] = metric.binary.assd(seg_array_rev,img_array_rev, voxelspacing=vxlspacing)

surface_dists_Medpy_df = pd.DataFrame(data=surface_dists_Medpy, index = list(range(1)),
                              columns=[name for name, _ in MedpyMetricDists.__members__.items()])

At first sight I don't think that SimpleITK computes the
symmetric distances. Is there any implementation for those in that library? How can I obtain them?

Is MedPy a reliable library? Can I calculate the symmetric root mean
square with it?

Other recommendations of libraries for computing surface distance
metrics?

Should I compute the Absolute Values for the Mauerer Distances Map? I am not sure how it affects the results.   reference_distance_map = sitk.Abs(sitk.SignedMaurerDistanceMap(reference_segmentation, squaredDistance=False, useImageSpacing=True))

Body:
@Roxanne

I am assuming here that you are being confused by the surface distance measures computed in this SimpleITK notebook?

The rest of the answer refers to that code.

The mean/std/median/maximum are not symmetric (Hausdorff is).

Using SimpleITK you can compute the symmetric mean and standard deviation by
computing the mean and standard deviation for the segmentation and then for the reference (the code does it for the segmentation, so just switch roles and you get it for the reference).

Now you have the mean and standard deviations from two samples. To get the size of a sample just call:

label_intensity_statistics_filter.GetNumberOfPixels(label)

To compute the symmetric mean and standard deviation from knowlege of n1,m1,s1, n2,m2,s2:

m = (n1*m1 + n2*m2)/(n1+n2)
s = np.sqrt((n1*(s1**2+(m1-m)**2) + n2*(s2**2+(m2-m)**2))/(n1+n2))

Note that the sample estimate for the standard deviation is the biased version (similar to the default behavior of numpy.std).

If you have additional questions please post to the ITK discourse forum.


问:
Can't install ASP.NET Core SDK on Mac
I'm trying to install the ASP.NET Core 2.0 API on my MacBook Pro.

I've downloaded both dotnet-sdk-2.0.2-osx-x64.pkg and dotnet-sdk-2.0.2-osx-gs-x64.pkg and when I try to launch either of them an installer dialog is displayued that just reads:

Verifying "dotnet-sdk-2.0.2-osx-x64.pkg"

or

Verifying "dotnet-sdk-2.0.2-osx-gs-x64.pkg"

but nothing further happens.

I am running OS X El Capitan 10.11.6 and have all of the latest updates for Visual Studio installed.

I can't locate any further information or instructions for installation. Please help.

答:
From https://learn.microsoft.com/en-us/dotnet/core/macos-prerequisites?tabs=netcore2x:

.NET Core 2.x is supported on the following versions of macOS: macOS
  10.12 "Sierra" and later versions

Looks like it might be time to upgrade to Sierra. That represents a change from the 1.1 requirements, which supported El Capitan.

FWIW I'm running Sierra on a mid-2012 MacBook Pro with 8GB of RAM and it performs just fine.

Title: Can't install ASP.NET Core SDK on Mac


问:
Datatables in visual studio using c#
I am a beginner with the datasets and data tables. I have created a C# program using visual studio, I have a dataset with two data tables. One of the tables is for the signing up in which the user will create an account to log in through the app. The user information will be stored in the table, e.g, username and password. The problem is when the user exit the app all the data in the tables will be removed it is like constructing the app again from the beginning. Is it necessary to have a connection for example to SQL server or Microsoft Access ? because I only have created a local dataset in the visual studio. The following class shows the creation of the data tables.

```<code>public sealed class DataAccess
{
    static DataAccess instance = new DataAccess();

    public DataSet ds { get; private set; }
    public DataTable dtemployees { get; private set; }
    public DataTable dttimeline { get; private set; }

    DataAccess()
    {
        this.ds = new DataSet();
        this.dtemployees = new DataTable();
        this.dttimeline = new DataTable();

        ds.Tables.Add("dtemployees");
        dtemployees.Columns.Add("ID", typeof(string));
        dtemployees.Columns.Add("name", typeof(string));
        dtemployees.Columns.Add("password", typeof(string));
        dtemployees.PrimaryKey = new DataColumn[] { dtemployees.Columns["ID"] };

        ds.Tables.Add("dttimeline");
        dttimeline.Columns.Add("ID", typeof(string));
        dttimeline.Columns.Add("name", typeof(string));
        dttimeline.Columns.Add("Date", typeof(string));
        dttimeline.Columns.Add("time", typeof(string));
    }

    public static DataAccess Instance
    {
        get
        {
            return instance;
        }
    }
}
</code>```

**and the following line shows how did I add data to the tables.

```<code>dtemployees.Rows.Add(F2idTB.Text, F2usernameTB.Text, F2passwordTB.Text);
</code>```

答:
I would recommend writing out to a .txt file for data storage if you are a beginner.  You will have to specify a specific format that you want to write in order to read the values back in correctly.

i.e.

dtEmployees.txt

1;John Wick;myPassword;

2;Mary Jane;myPassword;

You will be responsible for:

create a writeToTextFile() method

if file does not exist create one

if file does exist, clear it and write new data

create a readTextFile() method

for loop to read each line then split into array by ';' deliminator

map values to appropriate variables

get next line

Title: Datatables in visual studio using c#

Body:
I am a beginner with the datasets and data tables. I have created a C# program using visual studio, I have a dataset with two data tables. One of the tables is for the signing up in which the user will create an account to log in through the app. The user information will be stored in the table, e.g, username and password. The problem is when the user exit the app all the data in the tables will be removed it is like constructing the app again from the beginning. Is it necessary to have a connection for example to SQL server or Microsoft Access ? because I only have created a local dataset in the visual studio. The following class shows the creation of the data tables.

public sealed class DataAccess
{
    static DataAccess instance = new DataAccess();

    public DataSet ds { get; private set; }
    public DataTable dtemployees { get; private set; }
    public DataTable dttimeline { get; private set; }

    DataAccess()
    {
        this.ds = new DataSet();
        this.dtemployees = new DataTable();
        this.dttimeline = new DataTable();

        ds.Tables.Add("dtemployees");
        dtemployees.Columns.Add("ID", typeof(string));
        dtemployees.Columns.Add("name", typeof(string));
        dtemployees.Columns.Add("password", typeof(string));
        dtemployees.PrimaryKey = new DataColumn[] { dtemployees.Columns["ID"] };

        ds.Tables.Add("dttimeline");
        dttimeline.Columns.Add("ID", typeof(string));
        dttimeline.Columns.Add("name", typeof(string));
        dttimeline.Columns.Add("Date", typeof(string));
        dttimeline.Columns.Add("time", typeof(string));
    }

    public static DataAccess Instance
    {
        get
        {
            return instance;
        }
    }
}

**and the following line shows how did I add data to the tables.

dtemployees.Rows.Add(F2idTB.Text, F2usernameTB.Text, F2passwordTB.Text);


问:
How can I average number of items in our SQLite database by 24-hour period?
Trying to create a report for our support ticketing system and I'm trying to have 2 results in the report that show a rolling average of how many tickets were opened in a day and how many were closed in a day.

Basically, query the entire tickets table, separate out everything by individual days that the tickets were created on, count the number tickets for each individual day, then average that number.

My friend gave me this query:

```<code>SELECT AVG(ticket_count)
FROM (SELECT COUNT(*) AS ticket_count FROM tickets
GROUP BY DATE(created_at, '%Y'), DATE(created_at, '%m'), DATE(created_at, '%d')) AS ticket_part
</code>```

But it's not seeming to work for me. All I get is a single result with the number of tickets created last year.

答:
The middle part of your query is collapsing the table to a single row, so the outer part has nothing upon which to group.  It's hard to say exactly what you need without seeing the schema for ticket_count, but at a guess I'd try this:

```<code>SELECT
  AVG(CAST(TicketsOpened AS REAL))  -- The cast to REAL ensures that { 1, 2 } averages to 1.5 rather than 1
FROM
  (
  SELECT
    CAST(created_at AS DATE) AS Day  -- The cast to DATE truncates any time element; if you're storing date alone, you can omit this
    COUNT(*) AS TicketsOpened
  FROM
    ticket_count
  GROUP BY
    CAST(created_at AS DATE)
  ) AS X
</code>```

Hope that helps!

Body:
Trying to create a report for our support ticketing system and I'm trying to have 2 results in the report that show a rolling average of how many tickets were opened in a day and how many were closed in a day.

Basically, query the entire tickets table, separate out everything by individual days that the tickets were created on, count the number tickets for each individual day, then average that number.

My friend gave me this query:

SELECT AVG(ticket_count)
FROM (SELECT COUNT(*) AS ticket_count FROM tickets
GROUP BY DATE(created_at, '%Y'), DATE(created_at, '%m'), DATE(created_at, '%d')) AS ticket_part

But it's not seeming to work for me. All I get is a single result with the number of tickets created last year.

Body:
The middle part of your query is collapsing the table to a single row, so the outer part has nothing upon which to group.  It's hard to say exactly what you need without seeing the schema for ticket_count, but at a guess I'd try this:

SELECT
  AVG(CAST(TicketsOpened AS REAL))  -- The cast to REAL ensures that { 1, 2 } averages to 1.5 rather than 1
FROM
  (
  SELECT
    CAST(created_at AS DATE) AS Day  -- The cast to DATE truncates any time element; if you're storing date alone, you can omit this
    COUNT(*) AS TicketsOpened
  FROM
    ticket_count
  GROUP BY
    CAST(created_at AS DATE)
  ) AS X

Hope that helps!


问:
Avoid lexicographic ordering of numerical values with Python min() max()
I have a script to pull random numbers from a set of values. However, it broke today because min() and max() sort values by lexicographic order (so 200 is considered greater than 10000). How can I avoid lexicographic order here? Len key is on the right track but not quite right. I couldn't find any other key(s) that would help.

```<code>data_set = 1600.csv, 2405.csv, 6800.csv, 10000.csv, 21005.csv
</code>```

First try:

```<code>highest_value = os.path.splitext(max(data_set))[0]
lowest_value = os.path.splitext(min(data_set))[0]
</code>```

returns: lowest_value = 10000 highest_value = 6800

Second try:

```<code>highest_value = os.path.splitext(max(data_set,key=len))[0]
lowest_value = os.path.splitext(min(data_set,key=len))[0]
</code>```

returns: lowest_value = 1600 highest_value = 10000

Thanks.

答:
You can use key to order by the numeric part of the file:

```<code>data_set = ['1600.csv', '2405.csv', '6800.csv', '10000.csv', '21005.csv']

highest = max(data_set, key=lambda x: int(x.split('.')[0]))
lowest = min(data_set, key=lambda x: int(x.split('.')[0]))

print(highest) # >> 21005.csv
print(lowest)  # >> 1600.csv
</code>```

Body:
I have a script to pull random numbers from a set of values. However, it broke today because min() and max() sort values by lexicographic order (so 200 is considered greater than 10000). How can I avoid lexicographic order here? Len key is on the right track but not quite right. I couldn't find any other key(s) that would help.

data_set = 1600.csv, 2405.csv, 6800.csv, 10000.csv, 21005.csv

First try:

highest_value = os.path.splitext(max(data_set))[0]
lowest_value = os.path.splitext(min(data_set))[0]

returns: lowest_value = 10000 highest_value = 6800

Second try:

highest_value = os.path.splitext(max(data_set,key=len))[0]
lowest_value = os.path.splitext(min(data_set,key=len))[0]

returns: lowest_value = 1600 highest_value = 10000

Thanks.

Body:
You can use key to order by the numeric part of the file:

data_set = ['1600.csv', '2405.csv', '6800.csv', '10000.csv', '21005.csv']

highest = max(data_set, key=lambda x: int(x.split('.')[0]))
lowest = min(data_set, key=lambda x: int(x.split('.')[0]))

print(highest) # >> 21005.csv
print(lowest)  # >> 1600.csv


问:
Build up a list, but mind the previous elements
I've stuck with the following problem:

I want to build up a list one-by-one from head to tail. But I have a condition, which depends on the already inserted elements.

```<code>condition(Sofar, Element) :-
    between(1, 150, Element2),
    \+member(Element2, Sofar),
    Element = Element2.

makelist(L) :- maplist(condition(L), L).
</code>```

An important note:  Sofar must not contain NewElement, what I just try to assert! This is the point, where all my tries failed, because, with maplist, there is a reference in the list which gets the value of NewElement

Of course I have more complicated conditions like this, but if I could solve this, then I could adapt it.

答:
You are trying to map notions you have learned in command-oriented (a.k.a imperative) languages to Prolog. Names like Sofar or makelist imply that you are doing something step-by-step. Try to see all of this from another angle by describing how the list looks like:

A list of elements between 1 and 150 that are all different.

```<code>all_dif([]).
all_dif([E|Es]) :-
   maplist(dif(E), Es),
   all_dif(Es).

speciallist(Es) :-
   all_dif(Es),
   maplist(between(1,150), Es).
</code>```

Or, using clpfd:

```<code>:- use_module(library(clpfd)).

specialfdlist(Es) :-
   Es ins 1..150,
   all_different(Es).
</code>```

Body:
I've stuck with the following problem:

I want to build up a list one-by-one from head to tail. But I have a condition, which depends on the already inserted elements.

condition(Sofar, Element) :-
    between(1, 150, Element2),
    \+member(Element2, Sofar),
    Element = Element2.

makelist(L) :- maplist(condition(L), L).

An important note:  Sofar must not contain NewElement, what I just try to assert! This is the point, where all my tries failed, because, with maplist, there is a reference in the list which gets the value of NewElement

Of course I have more complicated conditions like this, but if I could solve this, then I could adapt it.

Body:
You are trying to map notions you have learned in command-oriented (a.k.a imperative) languages to Prolog. Names like Sofar or makelist imply that you are doing something step-by-step. Try to see all of this from another angle by describing how the list looks like:

A list of elements between 1 and 150 that are all different.

all_dif([]).
all_dif([E|Es]) :-
   maplist(dif(E), Es),
   all_dif(Es).

speciallist(Es) :-
   all_dif(Es),
   maplist(between(1,150), Es).

Or, using clpfd:

:- use_module(library(clpfd)).

specialfdlist(Es) :-
   Es ins 1..150,
   all_different(Es).


问:
Spark UDAF with ArrayType as bufferSchema performance issues
I'm working on a UDAF that returns an array of elements.

The input for each update is a tuple of index and value.

What the UDAF does is to sum all the values under the same index.

Example:

For input(index,value) : (2,1), (3,1), (2,3)

should return (0,0,4,1,...,0)

The logic works fine, but I have an issue with the update method, my implementation only updates 1 cell for each row, but the last assignment in that method actually copies the entire array - which is redundant and extremely time consuming.

This assignment alone is responsible for 98% of my query execution time.

My question is, how can I reduce that time? Is it possible to assign 1 value in the buffer array without having to replace the entire buffer?

P.S.: I'm working with Spark 1.6, and I cannot upgrade it anytime soon, so please stick to solution that would work with this version.

class SumArrayAtIndexUDAF() extends UserDefinedAggregateFunction{

  val bucketSize = 1000

  def inputSchema: StructType =  StructType(StructField("index",LongType) :: StructField("value",LongType) :: Nil)

  def dataType: DataType = ArrayType(LongType)

  def deterministic: Boolean = true

  def bufferSchema: StructType = {
    StructType(
      StructField("buckets", ArrayType(LongType)) :: Nil
    )
  }

  override def initialize(buffer: MutableAggregationBuffer): Unit = {
    buffer(0) = new Array[Long](bucketSize)
  }

  override def update(buffer: MutableAggregationBuffer, input: Row): Unit = {
    val index = input.getLong(0)
    val value = input.getLong(1)

    val arr = buffer.getAs[mutable.WrappedArray[Long]](0)

    buffer(0) = arr   // TODO THIS TAKES WAYYYYY TOO LONG - it actually copies the entire array for every call to this method (which essentially updates only 1 cell)
  }

    override def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = {
    val arr1 = buffer1.getAs[mutable.WrappedArray[Long]](0)
    val arr2 = buffer2.getAs[mutable.WrappedArray[Long]](0)

    for(i <- arr1.indices){
      arr1.update(i, arr1(i) + arr2(i))
    }

    buffer1(0) = arr1
  }

  override def evaluate(buffer: Row): Any = {
    buffer.getAs[mutable.WrappedArray[Long]](0)
  }
}
```

答:
TL;DR Either don't use UDAF or use primitive types in place of ArrayType.

Without UserDefinedFunction

Both solutions should skip expensive juggling between internal and external representation.

Using standard aggregates and pivot

This uses standard SQL aggregations. While optimized internally it might be expensive when number of keys and size of the array grow.

Given input:

```<code>val df = Seq((1, 2, 1), (1, 3, 1), (1, 2, 3)).toDF("id", "index", "value")
</code>```

You can:

```<code>import org.apache.spark.sql.functions.{array, coalesce, col, lit}

val nBuckets = 10
@transient val values = array(
  0 until nBuckets map (c => coalesce(col(c.toString), lit(0))): _*
)

df
  .groupBy("id")
  .pivot("index", 0 until nBuckets)
  .sum("value")
  .select($"id", values.alias("values"))
</code>```

```<code>+---+--------------------+
| id|              values|
+---+--------------------+
|  1|[0, 0, 4, 1, 0, 0...|
+---+--------------------+
</code>```

Using  RDD API with combineByKey / aggregateByKey.

Plain old byKey aggregation with mutable buffer. No bells and whistles but should perform reasonably well with wide range of inputs. If you suspect input to be sparse, you may consider more efficient intermediate representation, like mutable Map.

```<code>rdd
  .aggregateByKey(Array.fill(nBuckets)(0L))(
    { case (acc, (index, value)) => { acc(index) += value; acc }},
    (acc1, acc2) => { for (i <- 0 until nBuckets) acc1(i) += acc2(i); acc1}
  ).toDF
</code>```

+---+--------------------+
| _1|                  _2|
+---+--------------------+
|  1|[0, 0, 4, 1, 0, 0...|
+---+--------------------+
```

<p><strong>Using <code>UserDefinedFunction</code> with primitive types</strong></p>

<p>As far as I understand the internals, performance bottleneck is <code>ArrayConverter.toCatalystImpl</code>. </p>

<p>It look like it is called for each call <code>MutableAggregationBuffer.update</code>, and in turn allocates new <code>GenericArrayData</code> for each <code>Row</code>.</p>

<p>If we redefine <code>bufferSchema</code> as:</p>

```def bufferSchema: StructType = {
  StructType(
    0 to nBuckets map (i => StructField(s"x$i", LongType))
  )
}
```

<p>both <code>update</code> and <code>merge</code> can be expressed as plain replacements of primitive values in the buffer. Call chain will remain pretty long, but it won't require copies / conversions and crazy allocations. Omitting <code>null</code> checks you'll need something similar to</p>

```val index = input.getLong(0)
buffer.update(index, buffer.getLong(index) + input.getLong(1))
```

<p>and </p>

```for(i <- 0 to nBuckets){
  buffer1.update(i, buffer1.getLong(i) + buffer2.getLong(i))
}
```

<p>respectively.</p>

<p>Finally <code>evaluate</code> should take <code>Row</code> and convert it to output <code>Seq</code>:</p>

``` for (i <- 0 to nBuckets)  yield buffer.getLong(i)
```

<p>Please note that in this implementation a possible bottleneck is <code>merge</code>. While it shouldn't introduce any new performance problems, with <em>M</em> buckets, each call to <code>merge</code> is <em>O(M)</em>. </p>

<p>With <em>K</em> unique keys, and <em>P</em> partitions it will be called <em>M * K</em> times in the worst case scenario, where each key, occurs at least once on each partition. This effectively increases complicity of the <code>merge</code> component to <em>O(M * N * K)</em>.</p>

<p>In general there is not much you can do about it. However if you make specific assumptions about the data distribution (data is sparse, key distribution is uniform), you can shortcut things a bit, and shuffle first:</p>

```df
  .repartition(n, $"key")
  .groupBy($"key")
  .agg(SumArrayAtIndexUDAF($"index", $"value"))
```

<p>If the assumptions are satisfied it should:</p>

<ul>
<li>Counterintuitively reduce shuffle size by shuffling sparse pairs, instead of dense array-like <code>Rows</code>.</li>
<li>Aggregate data using updates only (each <em>O(1)</em>) possibly touching only as subset of indices.</li>
</ul>

<p>However if one or both assumptions are not satisfied, you can expect that shuffle size will increase while number of updates will stay the same. At the same time data skews can make things even worse than in <code>update</code> - <code>shuffle</code> - <code>merge</code> scenario.</p>

<p><strong>Using <code>Aggregator</code></strong> with "strongly" typed <code>Dataset</code>:</p>

```import org.apache.spark.sql.expressions.Aggregator
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
import org.apache.spark.sql.{Encoder, Encoders}

class SumArrayAtIndex[I](f: I => (Int, Long))(bucketSize: Int)  extends Aggregator[I, Array[Long], Seq[Long]]
    with Serializable {
  def zero = Array.fill(bucketSize)(0L)
  def reduce(acc: Array[Long], x: I) = {
    val (i, v) = f(x)
    acc(i) += v
    acc
  }

  def merge(acc1: Array[Long], acc2: Array[Long]) = {
    for {
      i <- 0 until bucketSize
    } acc1(i) += acc2(i)
    acc1
  }

  def finish(acc: Array[Long]) = acc.toSeq

  def bufferEncoder: Encoder[Array[Long]] = Encoders.kryo[Array[Long]]
  def outputEncoder: Encoder[Seq[Long]] = ExpressionEncoder()
}
```

<p>which could be used as shown below</p>

```import org.apache.spark.sql.functions.{array, coalesce, col, lit}

val nBuckets = 10
@transient val values = array(
  0 until nBuckets map (c => coalesce(col(c.toString), lit(0))): _*
)

df
  .groupBy("id")
  .pivot("index", 0 until nBuckets)
  .sum("value")
  .select($"id", values.alias("values"))
```0

<pre class="lang-none prettyprint-override"><code>+-----+-------------------------------+
|value|SumArrayAtIndex(scala.Tuple2)  |
+-----+-------------------------------+
|1    |[1, 3, 5, 0, 6, 0, 0, 0, 0, 0] |
|2    |[0, 11, 0, 0, 0, 0, 0, 0, 0, 0]|
+-----+-------------------------------+
</code>```

Note:

See also SPARK-27296 - User Defined Aggregating Functions (UDAFs) have a major efficiency problem

Body:
TL;DR Either don't use UDAF or use primitive types in place of ArrayType.

Without UserDefinedFunction

Both solutions should skip expensive juggling between internal and external representation.

Using standard aggregates and pivot

This uses standard SQL aggregations. While optimized internally it might be expensive when number of keys and size of the array grow.

Given input:

val df = Seq((1, 2, 1), (1, 3, 1), (1, 2, 3)).toDF("id", "index", "value")

You can:

import org.apache.spark.sql.functions.{array, coalesce, col, lit}

val nBuckets = 10
@transient val values = array(
  0 until nBuckets map (c => coalesce(col(c.toString), lit(0))): _*
)

df
  .groupBy("id")
  .pivot("index", 0 until nBuckets)
  .sum("value")
  .select($"id", values.alias("values"))

+---+--------------------+
| id|              values|
+---+--------------------+
|  1|[0, 0, 4, 1, 0, 0...|
+---+--------------------+

Using  RDD API with combineByKey / aggregateByKey.

Plain old byKey aggregation with mutable buffer. No bells and whistles but should perform reasonably well with wide range of inputs. If you suspect input to be sparse, you may consider more efficient intermediate representation, like mutable Map.

rdd
  .aggregateByKey(Array.fill(nBuckets)(0L))(
    { case (acc, (index, value)) => { acc(index) += value; acc }},
    (acc1, acc2) => { for (i <- 0 until nBuckets) acc1(i) += acc2(i); acc1}
  ).toDF

+---+--------------------+
| _1|                  _2|
+---+--------------------+
|  1|[0, 0, 4, 1, 0, 0...|
+---+--------------------+

Using UserDefinedFunction with primitive types

As far as I understand the internals, performance bottleneck is ArrayConverter.toCatalystImpl.

It look like it is called for each call MutableAggregationBuffer.update, and in turn allocates new GenericArrayData for each Row.

If we redefine bufferSchema as:

def bufferSchema: StructType = {
  StructType(
    0 to nBuckets map (i => StructField(s"x$i", LongType))
  )
}

both update and merge can be expressed as plain replacements of primitive values in the buffer. Call chain will remain pretty long, but it won't require copies / conversions and crazy allocations. Omitting null checks you'll need something similar to

val index = input.getLong(0)
buffer.update(index, buffer.getLong(index) + input.getLong(1))

and

for(i <- 0 to nBuckets){
  buffer1.update(i, buffer1.getLong(i) + buffer2.getLong(i))
}

respectively.

Finally evaluate should take Row and convert it to output Seq:

 for (i <- 0 to nBuckets)  yield buffer.getLong(i)

Please note that in this implementation a possible bottleneck is merge. While it shouldn't introduce any new performance problems, with M buckets, each call to merge is O(M).

With K unique keys, and P partitions it will be called M * K times in the worst case scenario, where each key, occurs at least once on each partition. This effectively increases complicity of the merge component to O(M * N * K).

In general there is not much you can do about it. However if you make specific assumptions about the data distribution (data is sparse, key distribution is uniform), you can shortcut things a bit, and shuffle first:

df
  .repartition(n, $"key")
  .groupBy($"key")
  .agg(SumArrayAtIndexUDAF($"index", $"value"))

If the assumptions are satisfied it should:

Counterintuitively reduce shuffle size by shuffling sparse pairs, instead of dense array-like Rows.

Aggregate data using updates only (each O(1)) possibly touching only as subset of indices.

However if one or both assumptions are not satisfied, you can expect that shuffle size will increase while number of updates will stay the same. At the same time data skews can make things even worse than in update - shuffle - merge scenario.

Using Aggregator with "strongly" typed Dataset:

import org.apache.spark.sql.expressions.Aggregator
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
import org.apache.spark.sql.{Encoder, Encoders}

class SumArrayAtIndex[I](f: I => (Int, Long))(bucketSize: Int)  extends Aggregator[I, Array[Long], Seq[Long]]
    with Serializable {
  def zero = Array.fill(bucketSize)(0L)
  def reduce(acc: Array[Long], x: I) = {
    val (i, v) = f(x)
    acc(i) += v
    acc
  }

  def merge(acc1: Array[Long], acc2: Array[Long]) = {
    for {
      i <- 0 until bucketSize
    } acc1(i) += acc2(i)
    acc1
  }

  def finish(acc: Array[Long]) = acc.toSeq

  def bufferEncoder: Encoder[Array[Long]] = Encoders.kryo[Array[Long]]
  def outputEncoder: Encoder[Seq[Long]] = ExpressionEncoder()
}

which could be used as shown below

val ds = Seq((1, (1, 3L)), (1, (2, 5L)), (1, (0, 1L)), (1, (4, 6L))).toDS

ds
  .groupByKey(_._1)
  .agg(new SumArrayAtIndex[(Int, (Int, Long))](_._2)(10).toColumn)
  .show(false)

+-----+-------------------------------+
|value|SumArrayAtIndex(scala.Tuple2)  |
+-----+-------------------------------+
|1    |[1, 3, 5, 0, 6, 0, 0, 0, 0, 0] |
|2    |[0, 11, 0, 0, 0, 0, 0, 0, 0, 0]|
+-----+-------------------------------+

Note:

See also SPARK-27296 - User Defined Aggregating Functions (UDAFs) have a major efficiency problem


问:
Dictionary<string, double>get key from value (error)
I have a dictionary (Dictionary) and I want to get key value from (value) for instance:

suppose the dic contains:

```<code>"som": 12.2345
"roy": 2.677
"toy": 1.67445666777
</code>```

I want to get the roy key from searching about 2.677

I tried:

```<code>var keys = from entry in dic
                           where entry.Value == 2.677
                           select entry.Key;
</code>```

and also :

```<code>var myKey =dic.FirstOrDefault(x => x.Value.Contains(2.677)).Key;
</code>```

in each case I got the same error which is :
Error:  'double' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'double' could be found (are you missing a using directive or an assembly reference?)

any help would be appreciated

答:
You can try this:

```<code>string key = dic.FirstOrDefault(x => x.Value == 2.677)?.Key;
</code>```

Note that FirstOrDefault() can return a null if nothing matches your condition, so use the null conditional operator (?.) to make sure a null reference exception is not thrown.  This just means that key will be null if it cannot find a value that matches.

Body:
I have a dictionary (Dictionary) and I want to get key value from (value) for instance:

suppose the dic contains:

"som": 12.2345
"roy": 2.677
"toy": 1.67445666777

I want to get the roy key from searching about 2.677

I tried:

var keys = from entry in dic
                           where entry.Value == 2.677
                           select entry.Key;

and also :

var myKey =dic.FirstOrDefault(x => x.Value.Contains(2.677)).Key;

in each case I got the same error which is :
Error:  'double' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'double' could be found (are you missing a using directive or an assembly reference?)

any help would be appreciated

Body:
You can try this:

string key = dic.FirstOrDefault(x => x.Value == 2.677)?.Key;

Note that FirstOrDefault() can return a null if nothing matches your condition, so use the null conditional operator (?.) to make sure a null reference exception is not thrown.  This just means that key will be null if it cannot find a value that matches.


答: It has no effect on performance. Both are good. They turn out to be compiled to similar bytecode as pointed by this blogpost :(http://livingtao.blogspot.in/2007/05/myth-defining-loop-variables-inside.html) </p>

Body:
Supposing I need to iterate over something and set a variable in every cycle, like this:

for (int i=0; i < list.size(); i++) {
    final Object cur = list.get(i);
}

this redefines the variable everytime, so I'm concerned that that might pollute the memory.

The alternate option is to define the variable once and then reassign it every iteration, like this:

Object cur
for (int i=0; i < list.size(); i++) {
    cur = list.get(i);
}

would this be better in terms of memory? Would it make any difference? What if cur is a primitive type instead of an Object?

Don't tell me to use foreach, I need the counter, this is just a simplified example


问:
How to implement auto renew subscription in app billing google play
I'm researching method to implement auto renew  subscription in app billing with google play. I read https://developer.android.com/google/play/billing/billing_subscriptions.html and see

Billing continues indefinitely at the interval and price specified for the subscription. At each subscription renewal, Google Play charges the user account automatically, then notifies the user of the charges afterward by email. For monthly and annual subscriptions, billing cycles will always match subscription cycles, based on the purchase date. (Seasonal subscriptions are charged annually, on the first day of the season.)

When the subscription payment is approved, Google Play provides a purchase token back to the purchasing app through the In-app Billing API. Your apps can store the token locally or pass it to your backend servers, which can then use it to validate or cancel the subscription remotely using the Google Play Developer API.

So have any method to my server know when user's subscription was renewed? Instead of google play send new bill subscription to android app after that android app send this new bill to my server just for validate.

Can google play send a notify to my server when user's subscription renewed such as notify the user by email ? I want to google play send me a notify that user's subscription was renewed automatically so that my backend will update expire their subscription in app increase. Don't need android app have to check bill each time user open store to check have new bill from goole play charge automation or not. Do it implement?

My workfollow

Google charge a new cycle subscription and notify to my server { body such as bundId, bill, product_id or subscription package name, expire date...), also sent mail to user about their subscription automation renewed.

My server determine change subscription of the user and validate in app purchase by google play api and change expire package subscription in your app if validate is valid.

Store newest bill in my db

Is that possible?

[Update] Recommend from goolge play api doc

Recommendation: Include business logic in your app to notify your
backend servers of subscription purchases, tokens, and any billing
errors that may occur. Your backend servers can use the server-side
API to query and update your records and follow up with customers
directly, if needed.

How to implement recommend from google api, any doc or tutorials ?

答:
I have currently exactly the same problem. The concept of Google is not well-conceived. There is a possibility to notify your backend server about financial transactions (see here), but I would not recommend this. You rely your business transactions on a lot of Google services and your server uptime. If anything goes wrong or is down or something, you will not be informed and your backend business logic does not work anymore.

This recommendation of Google you mentioned sucks as well. What happens if there is an auto-renawal (which delivers a new purchaseToken to your app) and the user never opens your app. Then the new subscription data will never be transferred to your server. And if you never got a new token, how can you check, if the user is still a subscriber, since this limited Google Play Developer API stupidly needs a purchaseToken as parameter (see here) that you never get as long as the user does not open your app at least once after an auto-renewal (to submit it to your server).

I think about implementing this in this way:

1.) I continuously check the purchase records by cron job. A purchase record is a database entry which contains all data from the initial subscription (orderId, purchaseToken, and so on, all that is needed for the security validation process on the server). Every purchase record is connected to a user's account (some UserID) in my backend system. As long as the autoRenewing attribute of the purchaseRecord is not false, the subscription is valid. Even if the expiryTimeMillis is exceeded, this one user could still have a valid subscription, because of the use case I described above: Subscription will be auto-renewed by Google, but the user never opens the app, so no transfer token is sent to your server and you are still not informed about the subscription update.

2.) If the use cancels his subscription any when, the autoRenewing would be false at any time. That means that the subscription will indeed end at the expiryTimeMillis.

3.) When the user opens your app and transfers the new purchaseToken to your backend, you will get a new purchase record which is again connected to the user account with his User ID. The user will probably have 2 purchase records now. The old one and the new one. If so, you could delete the old one, and the same process repeats with the new purchase record at step 1.

I didn't have implemented the concept so far, so I don't know if this really works like this. Maybe this could work in a different manner, but maybe it's a step into the right direction.


问:
How to return JObject as json object from WebServices
I have a huge dataset of Jobject type created in following format

```<code>{
    "key_1": {"id":"1", "name":"some1", ....},
    "key_2": {"id":"", "name":"some2", .....}
}
</code>```

I have created such object structure using following code:

```<code>JObject res = new JObject();
JObject jo = new JObject();
jo.Add("id", "1");
jo.Add("name", "some1");
res.Add("key_1", jo);
</code>```

and so on;

I have a webservice method which has return type object and attributes as ```[WebMethod][ScriptMethod(ResponseFormat = ResponseFormat.Json)]```

how to return this data as object in ajax request. I tried ```res.ToObject<(object)>()   //added curely braces around object because of html object tag ```  but it returns empty array collection at client side, but my expectation is get result as mentioned above (a proper object). How should i return res to get desired json output

答:
Edited - thanks Brian

There is a difference between things you CAN do and things you SHOULD do...

The right way to return your JObjects is up to you... create real objects that represent them... create key / value pairs that can be serialized...

What you don't want to do is just write the JSON out to the response...  however since that's literally what your asking... here is how you would do it.

```<code>[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetObject()
{
    string json = GetJsonFromDatabase();
    HttpContext.Current.Response.Write(json);
}
</code>```

Note that your method has to return void and you are writing your json straight out to the response stream.  Super dirty... super ugly... please don't do this.  You'll also have to manually turn it into an object on the js side.

Body:
I have a huge dataset of Jobject type created in following format

{
    "key_1": {"id":"1", "name":"some1", ....},
    "key_2": {"id":"", "name":"some2", .....}
}

I have created such object structure using following code:

JObject res = new JObject();
JObject jo = new JObject();
jo.Add("id", "1");
jo.Add("name", "some1");
res.Add("key_1", jo);

and so on;

I have a webservice method which has return type object and attributes as
[WebMethod][ScriptMethod(ResponseFormat = ResponseFormat.Json)]

how to return this data as object in ajax request. I tried
res.ToObject<(object)>()   //added curely braces around object because of html object tag
  but it returns empty array collection at client side, but my expectation is get result as mentioned above (a proper object). How should i return res to get desired json output

Body:
Edited - thanks Brian

There is a difference between things you CAN do and things you SHOULD do...

The right way to return your JObjects is up to you... create real objects that represent them... create key / value pairs that can be serialized...

What you don't want to do is just write the JSON out to the response...  however since that's literally what your asking... here is how you would do it.

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetObject()
{
    string json = GetJsonFromDatabase();
    HttpContext.Current.Response.Write(json);
}

Note that your method has to return void and you are writing your json straight out to the response stream.  Super dirty... super ugly... please don't do this.  You'll also have to manually turn it into an object on the js side.