text:
#include "net/instaweb/rewriter/public/cacheable_resource_base.h"

#include "net/instaweb/http/public/counting_url_async_fetcher.h"
#include "net/instaweb/http/public/http_cache.h"
#include "net/instaweb/http/public/http_cache_failure.h"
#include "net/instaweb/http/public/http_value.h"
#include "net/instaweb/http/public/request_context.h"
#include "net/instaweb/rewriter/cached_result.pb.h"
#include "net/instaweb/rewriter/public/resource.h"  // for Resource, etc
#include "net/instaweb/rewriter/public/rewrite_driver.h"
#include "pagespeed/kernel/base/ref_counted_ptr.h"
#include "pagespeed/kernel/base/statistics.h"
#include "pagespeed/kernel/base/string.h"  // for GoogleString
#include "pagespeed/kernel/base/timer.h"
#include "pagespeed/kernel/cache/lru_cache.h"
#include "pagespeed/kernel/http/content_type.h"
#include "pagespeed/kernel/http/http_names.h"
#include "pagespeed/kernel/http/request_headers.h"
#include "pagespeed/kernel/http/response_headers.h"
#include "test/net/instaweb/http/mock_url_fetcher.h"
#include "test/net/instaweb/rewriter/mock_resource_callback.h"
#include "test/net/instaweb/rewriter/rewrite_test_base.h"
#include "test/pagespeed/kernel/base/gtest.h"
#include "test/pagespeed/kernel/base/mock_message_handler.h"

namespace net_instaweb {

namespace {

const char kTestUrl[] = "http://www.example.com/";
const char kTestRef[] = "http://www.example.com/some_page";
const char kContent[] = "content!";

class TestResource : public CacheableResourceBase {
 public:
  explicit TestResource(RewriteDriver* rewrite_driver)
      : CacheableResourceBase("test", kTestUrl, kTestUrl, nullptr,
                              rewrite_driver),
        cache_key_(kTestUrl),
        do_prepare_request_(false),
        do_prepare_response_(false) {}

  static void InitStats(Statistics* stats) {
    CacheableResourceBase::InitStats("test", stats);
  }

  GoogleString cache_key() const override { return cache_key_; }
  void set_cache_key(StringPiece ck) { ck.CopyToString(&cache_key_); }

  void PrepareRequest(const RequestContextPtr& request_context,
                      RequestHeaders* headers) override {
    if (do_prepare_request_) {
      // To test that this gets invoked properly, we set the referer header
      // since MockUrlFetcher records those.
      headers->Replace(HttpAttributes::kReferer, kTestRef);
      request_context->AddSessionAuthorizedFetchOrigin(kTestUrl);
    }
  }

  void PrepareResponseHeaders(ResponseHeaders* headers) override {
    if (do_prepare_response_) {
      headers->Remove(HttpAttributes::kCacheControl, "private");
    }
  }

  void set_do_prepare_request(bool x) { do_prepare_request_ = x; }

  void set_do_prepare_response(bool x) { do_prepare_response_ = x; }

  // Wipe any loaded values, but not the configuration.
  void Reset() {
    HTTPValue empty_value;
    // Don't want it to totally empty, or it won't get set.
    empty_value.Write("", server_context()->message_handler());
    Link(&empty_value, server_context()->message_handler());
    LinkFallbackValue(&empty_value);
  }

 protected:
  GoogleString cache_key_;
  bool do_prepare_request_;
  bool do_prepare_response_;
};

class MockFreshenCallback : public Resource::FreshenCallback {
 public:
  MockFreshenCallback(const ResourcePtr& resource, InputInfo* input_info)
      : FreshenCallback(resource),
        input_info_(input_info),
        done_(false),
        extend_success_(false) {}

  InputInfo* input_info() override { return input_info_; }

  void Done(bool lock_failure, bool extend_success) override {
    done_ = true;
    extend_success_ = extend_success;
  }

  bool done() const { return done_; }
  bool extend_success() const { return extend_success_; }

 private:
  InputInfo* input_info_;
  bool done_;
  bool extend_success_;
};

}  // namespace

class CacheableResourceBaseTest : public RewriteTestBase {
 protected:
  void SetUp() override {
    RewriteTestBase::SetUp();
    TestResource::InitStats(server_context()->statistics());

    resource_.reset(new TestResource(rewrite_driver()));
  }

  void CheckStats(TestResource* resource, int expect_hits,
                  int expect_recent_fetch_failures,
                  int expect_recent_uncacheables_miss,
                  int expect_recent_uncacheables_failure, int expect_misses) {
    EXPECT_EQ(expect_hits, resource->hits_->Get());
    EXPECT_EQ(expect_recent_fetch_failures,
              resource->recent_fetch_failures_->Get());
    EXPECT_EQ(expect_recent_uncacheables_miss,
              resource->recent_uncacheables_miss_->Get());
    EXPECT_EQ(expect_recent_uncacheables_failure,
              resource->recent_uncacheables_failure_->Get());
    EXPECT_EQ(expect_misses, resource->misses_->Get());
  }

  RefCountedPtr<TestResource> resource_;
};

TEST_F(CacheableResourceBaseTest, BasicCached) {
  SetResponseWithDefaultHeaders(kTestUrl, kContentTypeText, kContent, 1000);

  MockResourceCallback callback(ResourcePtr(resource_.get()),
                                server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback);
  EXPECT_TRUE(callback.done());
  EXPECT_TRUE(callback.success());
  EXPECT_EQ(kContent, resource_->ExtractUncompressedContents());
  EXPECT_EQ(1, counting_url_async_fetcher()->fetch_count());
  CheckStats(resource_.get(), 0, 0, 0, 0, 1);

  // 2nd read should be cached.
  resource_->Reset();
  MockResourceCallback callback2(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback2);
  EXPECT_TRUE(callback2.done());
  EXPECT_TRUE(callback2.success());
  EXPECT_EQ(kContent, resource_->ExtractUncompressedContents());
  EXPECT_EQ(1, counting_url_async_fetcher()->fetch_count());
  CheckStats(resource_.get(), 1, 0, 0, 0, 1);

  // Make sure freshening happens. The rest resource is set to 1000 sec ttl,
  // so forward time 900 seconds ahead.
  AdvanceTimeMs(900 * Timer::kSecondMs);
  resource_->Reset();
  MockResourceCallback callback3(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback3);
  EXPECT_TRUE(callback3.done());
  EXPECT_TRUE(callback3.success());
  EXPECT_EQ(kContent, resource_->ExtractUncompressedContents());
  // Freshening resulted in an extra fetch
  EXPECT_EQ(2, counting_url_async_fetcher()->fetch_count());
  CheckStats(resource_.get(), 2, 0, 0, 0, 1);
}

TEST_F(CacheableResourceBaseTest, Fallback) {
  options()->set_serve_stale_if_fetch_error(true);
  mock_url_fetcher()->Disable();
  mock_url_fetcher()->set_error_message("oww!");

  ResponseHeaders ok_response_headers;
  SetDefaultLongCacheHeaders(&kContentTypeText, &ok_response_headers);

  HTTPValue fallback;
  fallback.SetHeaders(&ok_response_headers);
  EXPECT_TRUE(fallback.Write("fallback", server_context()->message_handler()));
  EXPECT_TRUE(fallback.Flush(server_context()->message_handler()));
  resource_->LinkFallbackValue(&fallback);

  MockResourceCallback callback(ResourcePtr(resource_.get()),
                                server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback);
  EXPECT_TRUE(callback.done());
  EXPECT_TRUE(callback.success());
  EXPECT_STREQ("fallback", resource_->ExtractUncompressedContents());
  EXPECT_EQ(kFetchStatusOtherError, resource_->fetch_response_status());
}

TEST_F(CacheableResourceBaseTest, FallbackEmpty) {
  // Regression test. This used to CHECK-fail in some intermediate revisions.
  options()->set_serve_stale_if_fetch_error(true);
  mock_url_fetcher()->Disable();
  mock_url_fetcher()->set_error_message("oww!");

  ResponseHeaders ok_response_headers;
  SetDefaultLongCacheHeaders(&kContentTypeText, &ok_response_headers);

  HTTPValue fallback;
  fallback.SetHeaders(&ok_response_headers);
  EXPECT_TRUE(fallback.Write("", server_context()->message_handler()));
  EXPECT_TRUE(fallback.Flush(server_context()->message_handler()));
  resource_->LinkFallbackValue(&fallback);

  MockResourceCallback callback(ResourcePtr(resource_.get()),
                                server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback);
  EXPECT_TRUE(callback.done());
  EXPECT_TRUE(callback.success());
  EXPECT_STREQ("", resource_->ExtractUncompressedContents());
  EXPECT_EQ(kFetchStatusOtherError, resource_->fetch_response_status());
}

TEST_F(CacheableResourceBaseTest, Private) {
  ResponseHeaders response_headers;
  SetDefaultLongCacheHeaders(&kContentTypeText, &response_headers);
  response_headers.Add(HttpAttributes::kCacheControl, "private");
  SetFetchResponse(kTestUrl, response_headers, kContent);

  MockResourceCallback callback(ResourcePtr(resource_.get()),
                                server_context()->thread_system());

  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback);
  EXPECT_TRUE(callback.done());
  EXPECT_FALSE(callback.success());
  EXPECT_EQ(1, counting_url_async_fetcher()->fetch_count());
  CheckStats(resource_.get(), 0, 0, 0, 0, 1);

  // The non-cacheability should be cached.
  resource_->Reset();
  MockResourceCallback callback2(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback2);
  EXPECT_TRUE(callback2.done());
  EXPECT_FALSE(callback2.success());
  EXPECT_EQ(1, counting_url_async_fetcher()->fetch_count());
  CheckStats(resource_.get(), 0, 0, 0, 1, 1);
}

TEST_F(CacheableResourceBaseTest, PrivateForFetch) {
  // This test private + kLoadEvenIfNotCacheable.
  ResponseHeaders response_headers;
  SetDefaultLongCacheHeaders(&kContentTypeText, &response_headers);
  response_headers.Add(HttpAttributes::kCacheControl, "private");
  SetFetchResponse(kTestUrl, response_headers, kContent);

  MockResourceCallback callback(ResourcePtr(resource_.get()),
                                server_context()->thread_system());

  resource_->LoadAsync(
      Resource::kLoadEvenIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback);
  EXPECT_TRUE(callback.done());
  EXPECT_TRUE(callback.success());
  EXPECT_EQ(kContent, resource_->ExtractUncompressedContents());
  EXPECT_EQ(1, counting_url_async_fetcher()->fetch_count());
  CheckStats(resource_.get(), 0, 0, 0, 0, 1);

  // Since it's non-cacheable, but we have kLoadEvenIfNotCacheable
  // set, we should re-fetch it.
  resource_->Reset();
  MockResourceCallback callback2(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kLoadEvenIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback2);
  EXPECT_TRUE(callback2.done());
  EXPECT_TRUE(callback2.success());
  EXPECT_EQ(kContent, resource_->ExtractUncompressedContents());
  EXPECT_EQ(2, counting_url_async_fetcher()->fetch_count());
  CheckStats(resource_.get(), 0, 0, 1, 0, 1);
}

TEST_F(CacheableResourceBaseTest, FetchFailure) {
  SetFetchFailOnUnexpected(false);
  MockResourceCallback callback(ResourcePtr(resource_.get()),
                                server_context()->thread_system());

  resource_->LoadAsync(
      Resource::kLoadEvenIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback);
  EXPECT_TRUE(callback.done());
  EXPECT_FALSE(callback.success());
  EXPECT_EQ(1, counting_url_async_fetcher()->fetch_count());
  CheckStats(resource_.get(), 0, 0, 0, 0, 1);

  // Failure should get cached, and we should take advantage of it.
  resource_->Reset();
  MockResourceCallback callback2(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kLoadEvenIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback2);
  EXPECT_TRUE(callback2.done());
  EXPECT_FALSE(callback2.success());
  EXPECT_EQ(1, counting_url_async_fetcher()->fetch_count());
  CheckStats(resource_.get(), 0, 1, 0, 0, 1);

  // Now advance time, should force a refetch.
  int64 remember_sec = server_context()->http_cache()->failure_caching_ttl_sec(
      kFetchStatusOtherError);
  AdvanceTimeMs(2 * remember_sec * Timer::kSecondMs);
  resource_->Reset();
  MockResourceCallback callback3(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kLoadEvenIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback3);
  EXPECT_TRUE(callback3.done());
  EXPECT_FALSE(callback3.success());
  EXPECT_EQ(2, counting_url_async_fetcher()->fetch_count());
  CheckStats(resource_.get(), 0, 1, 0, 0, 2);
}

TEST_F(CacheableResourceBaseTest, DropHandling) {
  // First have mock fetcher emulate a drop.
  ResponseHeaders not_found;
  SetDefaultLongCacheHeaders(&kContentTypeHtml, &not_found);
  not_found.SetStatusAndReason(HttpStatus::kUnavailable);
  not_found.Add(HttpAttributes::kXPsaLoadShed, "1");
  SetFetchResponse("http://www.example.com/", not_found, "hmm");

  MockResourceCallback callback(ResourcePtr(resource_.get()),
                                server_context()->thread_system());

  resource_->LoadAsync(
      Resource::kLoadEvenIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback);
  EXPECT_TRUE(callback.done());
  EXPECT_FALSE(resource_->IsValidAndCacheable());

  resource_->Reset();

  // Now have the fetch result in a 200.
  ResponseHeaders found;
  SetDefaultLongCacheHeaders(&kContentTypeCss, &found);
  found.SetStatusAndReason(HttpStatus::kOK);
  SetFetchResponse("http://www.example.com/", found, "* {display: stylish; }");

  // Still cached since background fetch.
  MockResourceCallback callback2(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());

  resource_->LoadAsync(
      Resource::kLoadEvenIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback2);
  EXPECT_TRUE(callback2.done());
  EXPECT_FALSE(resource_->IsValidAndCacheable());
  resource_->Reset();

  // Marked as a non-background, will retry.
  resource_->set_is_background_fetch(false);
  MockResourceCallback callback3(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());

  resource_->LoadAsync(
      Resource::kLoadEvenIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback3);
  EXPECT_TRUE(callback3.done());
  EXPECT_TRUE(resource_->IsValidAndCacheable());
}

TEST_F(CacheableResourceBaseTest, FreshenInfo) {
  SetResponseWithDefaultHeaders(kTestUrl, kContentTypeText, kContent, 1000);

  MockResourceCallback callback(ResourcePtr(resource_.get()),
                                server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback);
  EXPECT_EQ(1, counting_url_async_fetcher()->fetch_count());
  EXPECT_TRUE(callback.done());
  EXPECT_TRUE(callback.success());

  InputInfo input_info;
  resource_->FillInPartitionInputInfo(Resource::kIncludeInputHash, &input_info);
  InputInfo input_info2 = input_info;

  // Move time ahead so freshening actually does something.
  AdvanceTimeMs(900 * Timer::kSecondMs);

  MockFreshenCallback freshen_cb(ResourcePtr(resource_.get()), &input_info);
  resource_->Freshen(&freshen_cb, message_handler());
  EXPECT_EQ(2, counting_url_async_fetcher()->fetch_count());

  EXPECT_TRUE(freshen_cb.done());
  EXPECT_TRUE(freshen_cb.extend_success());

  // Expiration time must have moved ahead, too.
  EXPECT_EQ(1000 * Timer::kSecondMs + timer()->NowMs(),
            input_info.expiration_time_ms());

  // The above freshened from fetches, now we should be able to do it
  // from cache as well.
  MockFreshenCallback freshen_cb2(ResourcePtr(resource_.get()), &input_info2);
  resource_->Freshen(&freshen_cb2, message_handler());
  EXPECT_EQ(2, counting_url_async_fetcher()->fetch_count());
  EXPECT_TRUE(freshen_cb.done());
  EXPECT_TRUE(freshen_cb.extend_success());
  EXPECT_EQ(1000 * Timer::kSecondMs + timer()->NowMs(),
            input_info2.expiration_time_ms());
}

TEST_F(CacheableResourceBaseTest, SameUrlDifferentKey) {
  SetResponseWithDefaultHeaders(kTestUrl, kContentTypeText, kContent, 1000);

  MockResourceCallback callback(ResourcePtr(resource_.get()),
                                server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback);
  EXPECT_EQ(1, counting_url_async_fetcher()->fetch_count());
  EXPECT_TRUE(callback.done());
  EXPECT_TRUE(callback.success());

  RefCountedPtr<TestResource> resource2(new TestResource(rewrite_driver()));
  resource2->set_cache_key("http://other_key.org/");
  MockResourceCallback callback2(ResourcePtr(resource2.get()),
                                 server_context()->thread_system());
  resource2->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback2);
  // 2 fetches, since we are using different keys.
  EXPECT_EQ(2, counting_url_async_fetcher()->fetch_count());
  EXPECT_TRUE(callback2.done());
  EXPECT_TRUE(callback2.success());

  // Both should be cached successfully now.
  resource_->Reset();
  MockResourceCallback callback3(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback3);
  EXPECT_EQ(2, counting_url_async_fetcher()->fetch_count());
  EXPECT_TRUE(callback3.done());
  EXPECT_TRUE(callback3.success());

  // Delete resource's cache entry to make sure that #2 is really using
  // something different.
  lru_cache()->Delete(HttpCacheKey(kTestUrl));

  // Re-fetch r1, to make sure the delete worked
  resource_->Reset();
  MockResourceCallback callback4(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback4);
  EXPECT_EQ(3, counting_url_async_fetcher()->fetch_count());
  EXPECT_TRUE(callback4.done());
  EXPECT_TRUE(callback4.success());

  // Now fetch r2, should still be hashed.
  resource2->Reset();
  MockResourceCallback callback5(ResourcePtr(resource2.get()),
                                 server_context()->thread_system());
  resource2->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback5);
  EXPECT_EQ(3, counting_url_async_fetcher()->fetch_count());
  EXPECT_TRUE(callback.done());
  EXPECT_TRUE(callback.success());

  // Now test freshening, with both ops happening simultaneously to make
  // sure the locking is correct.
  SetupWaitFetcher();
  InputInfo input_info, input2_info;
  resource_->FillInPartitionInputInfo(Resource::kIncludeInputHash, &input_info);
  resource2->FillInPartitionInputInfo(Resource::kIncludeInputHash,
                                      &input2_info);

  AdvanceTimeMs(900 * Timer::kSecondMs);

  MockFreshenCallback freshen_cb(ResourcePtr(resource_.get()), &input_info);
  resource_->Freshen(&freshen_cb, message_handler());

  MockFreshenCallback freshen2_cb(ResourcePtr(resource2.get()), &input2_info);
  resource2->Freshen(&freshen2_cb, message_handler());

  CallFetcherCallbacks();

  EXPECT_EQ(5, counting_url_async_fetcher()->fetch_count());
  EXPECT_EQ(1000 * Timer::kSecondMs + timer()->NowMs(),
            input_info.expiration_time_ms());

  EXPECT_EQ(1000 * Timer::kSecondMs + timer()->NowMs(),
            input2_info.expiration_time_ms());
}

TEST_F(CacheableResourceBaseTest, PrepareHooks) {
  // Test to see that PrepareRequest works.
  SetResponseWithDefaultHeaders(kTestUrl, kContentTypeText, kContent, 1000);

  MockResourceCallback callback(ResourcePtr(resource_.get()),
                                server_context()->thread_system());
  RequestContextPtr request_context(
      RequestContext::NewTestRequestContext(server_context()->thread_system()));
  resource_->LoadAsync(Resource::kReportFailureIfNotCacheable, request_context,
                       &callback);
  EXPECT_TRUE(callback.done());
  EXPECT_TRUE(callback.success());
  EXPECT_EQ(kContent, resource_->ExtractUncompressedContents());
  EXPECT_EQ(1, counting_url_async_fetcher()->fetch_count());

  // w/o the hook
  EXPECT_EQ("", mock_url_fetcher()->last_referer());
  EXPECT_FALSE(request_context->IsSessionAuthorizedFetchOrigin(kTestUrl));

  // now turn the hook on.
  resource_->Reset();
  resource_->set_do_prepare_request(true);
  lru_cache()->Delete(HttpCacheKey(kTestUrl));
  MockResourceCallback callback2(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());
  RequestContextPtr request_context2(
      RequestContext::NewTestRequestContext(server_context()->thread_system()));
  resource_->LoadAsync(Resource::kReportFailureIfNotCacheable, request_context2,
                       &callback2);
  EXPECT_TRUE(callback2.done());
  EXPECT_TRUE(callback2.success());
  EXPECT_EQ(kContent, resource_->ExtractUncompressedContents());
  EXPECT_EQ(2, counting_url_async_fetcher()->fetch_count());

  // the PrepareRequest() hook should have changed the referrer.
  EXPECT_EQ(kTestRef, mock_url_fetcher()->last_referer());

  // ... And authorized a domain
  EXPECT_TRUE(request_context2->IsSessionAuthorizedFetchOrigin(kTestUrl));

  // Now test with and without PrepareResponseHeaders

  // Set up a private resource.
  ResponseHeaders response_headers;
  SetDefaultLongCacheHeaders(&kContentTypeText, &response_headers);
  response_headers.Add(HttpAttributes::kCacheControl, "private");
  SetFetchResponse(kTestUrl, response_headers, kContent);

  MockResourceCallback callback3(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());

  resource_->Reset();
  lru_cache()->Delete(HttpCacheKey(kTestUrl));
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback3);
  EXPECT_TRUE(callback3.done());
  // By default, fetch fails.
  EXPECT_FALSE(callback3.success());
  EXPECT_EQ(3, counting_url_async_fetcher()->fetch_count());
  // Note: can't check response_headers here since resource wasn't loaded.

  // Now try with a PrepareResponseHeaders removing cache-control: private
  lru_cache()->Delete(HttpCacheKey(kTestUrl));
  resource_->set_do_prepare_response(true);
  MockResourceCallback callback4(ResourcePtr(resource_.get()),
                                 server_context()->thread_system());

  resource_->Reset();
  resource_->LoadAsync(
      Resource::kReportFailureIfNotCacheable,
      RequestContext::NewTestRequestContext(server_context()->thread_system()),
      &callback4);
  EXPECT_TRUE(callback4.done());
  EXPECT_TRUE(callback4.success());  // due to messing with headers.
  EXPECT_FALSE(resource_->response_headers()->HasValue(
      HttpAttributes::kCacheControl, "private"));
  EXPECT_EQ(4, counting_url_async_fetcher()->fetch_count());
}

}  // namespace net_instaweb


text:
<?php

namespace Malenki\Math;
use \Malenki\Math\Number\Complex;
/**
 * Matrix basic implementation.
 *
 * Different ways are available to instanciate a Matrix:
 *
 * - by setting all values using one dimensional array,
 * - by adding columns,
 * - by adding rows
 *
 * So, a little example to show you:
 *
 *     $m = new Matrix(2, 3); // 2 rows, 3 columns
 *
 *     $m->populate(array(1, 2, 3, 4, 5, 6)); // populate into one shot
 *
 *     // or adding columns
 *     $m->addCol(array(1, 4));
 *     $m->addCol(array(2, 5));
 *     $m->addCol(array(3, 6));
 *
 *     // or adding rows
 *     $m->addRow(array(1, 2, 3));
 *     $m->addRow(array(4, 5, 6));
 *
 *
 *  Some actions are available, like __transpose__, __multiply__ with scalar or Matrix,
 *  __add__ Matrix, test if a Matrix is square… See methods to have more information!
 *
 * @todo as a reminder: http://www.latp.univ-mrs.fr/~torresan/CalcMat/cours/node2.html
 *
 * @property-read $cols Amount of columns
 * @property-read $rows Amount of rows
 * @author Michel Petit <petit.michel@gmail.com>
 * @license MIT
 */
class Matrix
{
    /**
     * Items of the matrix, as an array of array.
     *
     * @var array
     * @access protected
     */
    protected $arr = array();

    /**
     * Size of the matrix: column and row number.
     *
     * @var mixed
     * @access protected
     */
    protected $size = null;

    /**
     * Defines magic getter to acces to column's numbers and row's numbers
     *
     * @param  mixed   $name
     * @access public
     * @return integer
     */
    public function __get($name)
    {
        if (in_array($name, array('cols', 'rows'))) {
            return $this->size->$name;
        }

        if ($name == 'array') {
            return $this->arr;
        }

        if ($name == 'is_square' || $name == 'square') {
            return $this->isSquare();
        }

        if ($name == 'is_diagonal' || $name == 'diagonal') {
            return $this->isDiagonal();
        }

        if (in_array($name, array('cofactor', 'adjugate', 'inverse', 'det', 'trace', 'determinant', 'transpose'))) {
            return $this->$name();
        }
    }

    /**
     * Constructs new matrix giving its size.
     *
     * @throw \InvalidArgumentException If numbers of cols or rows are not integers.
     * @throw \InvalidArgumentException If numbers of cols or rows are not positive numbers.
     * @param  integer $int_rows
     * @param  integer $int_cols
     * @access public
     * @return Matrix
     */
    public function __construct($int_rows, $int_cols)
    {
        if (!is_numeric($int_cols) || !is_numeric($int_rows)) {
            throw new \InvalidArgumentException('number of cols and rows must be integers.');
        }

        $int_cols = (integer) $int_cols;
        $int_rows = (integer) $int_rows;

        if ($int_cols <= 0 || $int_rows <= 0) {
            throw new \InvalidArgumentException('Number of cols and rows must be positive not null integers.');
        }

        $this->size = new \stdClass();
        $this->size->cols = $int_cols;
        $this->size->rows = $int_rows;
    }

    /**
     * Puts all values into the matrix.
     *
     * Argument is a simple array of one dimension. If the matrix has 3 columns
     * and 2 rows, the the argument must have 6 elements. Under the hood, the
     * given array is split to fit the matrix.
     *
     * By the way, you can use rows and colunms in place of this method.
     *
     * @param  array  $arrAll
     * @access public
     * @return Matrix
     */
    public function populate($arrAll)
    {
        $this->arr = array_chunk($arrAll, $this->size->cols);

        return $this;
    }

    /**
     * Gets one item at given row and column.
     *
     * @throw \InvalidArgumentException If indexes are not integers
     * @throw \InvalidArgumentException If one of the given indexes does not exist.
     * @param  integer $int_i Row index
     * @param  integer $int_j Column index
     * @access public
     * @return float
     */
    public function get($int_i, $int_j)
    {
        if (!is_integer($int_i) || !is_integer($int_j)) {
            throw new \InvalidArgumentException('Indices must be integers.');
        }

        if(
            $int_i >= $this->size->rows
            ||
            $int_j >= $this->size->cols
            ||
            $int_i < 0
            ||
            $int_j < 0
        )
        {
            throw new \InvalidArgumentException('Row's index or column's index does not exist!');
        }

        return $this->arr[$int_i][$int_j];
    }

    /**
     * Adds a row to populate step by step the matrix
     *
     * @throw \OutOfRangeException If this is called and the number of rows is full.
     * @throw \InvalidArgumentException If row has not the same number of column that previous one.
     * @param  array  $arr_row Data to add
     * @access public
     * @return Matrix
     */
    public function addRow(array $arr_row)
    {
        if (count($this->arr) == $this->size->rows) {
            throw new \OutOfRangeException(sprintf('You cannot add another row! Max number of rows is %d', $this->size->rows));
        }

        if (count($arr_row) != $this->size->cols) {
            throw new \InvalidArgumentException('New row must have same amout of columns than defined into the size matrix');
        }

        $this->arr[] = $arr_row;

        return $this;
    }

    /**
     * Adds a column to populate step by step the matrix
     *
     * @throw \OutOfRangeException If this is called and the number of columns is full.
     * @throw \InvalidArgumentException If colmun has not the same number of rows that previous one.
     * @param  array  $arr_col Data to add
     * @access public
     * @return Matrix
     */
    public function addCol($arr_col)
    {
        if (isset($this->arr[0]) && (count($this->arr[0]) == $this->size->cols)) {
            throw new \OutOfRangeException(sprintf('You cannot add another column! Max number of columns is %d', $this->size->cols));
        }

        if (count($arr_col) != $this->size->rows) {
            throw new \InvalidArgumentException('New column must have same amout of rows than previous columns.');
        }

        $arr_col = array_values($arr_col); //to be sure to have index 0, 1, 2…

        foreach ($arr_col as $k => $v) {
            $this->arr[$k][] = $arr_col[$k];
        }

        return $this;
    }

    /**
     * Gets the row having the given index.
     *
     * @throw \OutOfRangeException If given index does not exist.
     * @param  integer $int Row's index
     * @access public
     * @return mixed
     */
    public function getRow($int = 0)
    {
        if (!isset($this->arr[$int])) {
            throw new \OutOfRangeException('There is no line having this index.');
        }

        return $this->arr[$int];
    }

    /**
     * Gets the column having the given index.
     *
     * @throw \OutOfRangeException If given index does not exist.
     * @param  integer $int column's index
     * @access public
     * @return mixed
     */
    public function getCol($int = 0)
    {
        if ($int >= $this->size->cols) {
            throw new \OutOfRangeException('There is not column having this index.');
        }

        $arr_out = array();

        foreach ($this->arr as $row) {
            $arr_out[] = $row[$int];
        }

        return $arr_out;
    }

    /**
     * Tells whether the current matrix is square or not.
     *
     * @access public
     * @return boolean
     */
    public function isSquare()
    {
        return $this->size->cols == $this->size->rows;
    }

    /**
     * Tests whether the current matrix is diagonal or not.
     *
     * @access public
     * @return boolean
     */
    public function isDiagonal()
    {
        $int_size = min((array) $this->size);

        if($int_size > 0){
            for($i = 0; $i < $int_size; $i++){
                $arr_row = $this->getRow($i);

                if($arr_row[$i] != 0){
                    unset($arr_row[$i]);

                    foreach($arr_row as $v){
                        if($v != 0){
                            return false;
                        }
                    }

                } else {
                    return false;
                }
            }

            return true;
        }

        return false;
    }

    public function exponential()
    {
        //TODO: implement it!
        if(!$this->isSquare()){
            throw new \RuntimeException('Computing exponential of non diagonal Matrix is not yet implemented');
        }

        $int_size = min((array) $this->size);

        $out = new self($this->size->rows, $this->size->cols);

        if($int_size > 0){
            for($i = 0; $i < $int_size; $i++){
                $arr_row = $this->getRow($i);

                if($arr_row[$i] != 0){
                    $arr_row[$i] = exp($arr_row[$i]);
                }

                $out->addRow($arr_row);
            }
        }

        return $out;

    }

    public function exp()
    {
        return $this->exponential();
    }

    /**
     * Tests whether the current matrix is the same as the given one.
     *
     * @param  Matrix  $matrix
     * @access public
     * @return boolean
     */
    public function sameSize($matrix)
    {
        return (
            $this->size->cols == $matrix->cols
            &&
            $this->size->rows == $matrix->rows
        );
    }

    /**
     * Tests whether the current matrix can be multiply with the given one.
     *
     * @param  mixed   $matrix Scalar, complex or matrix
     * @access public
     * @return boolean
     */
    public function multiplyAllow($matrix)
    {
        if (is_numeric($matrix)) {
            return true;
        }

        if ($matrix instanceof Complex) {
            return true;
        }

        if ($matrix instanceof Matrix) {
            return $this->size->cols == $matrix->rows;
        }

        return false;
    }

    /**
     * Returns the transpose of the current matrix.
     *
     * @access public
     * @return Matrix
     */
    public function transpose()
    {
        $out = new self($this->size->cols, $this->size->rows);

        foreach ($this->arr as $row) {
            $out->addCol($row);
        }

        return $out;
    }

    /**
     * Adds the given matrix with the current one to give another new matrix.
     *
     * @throw \InvalidArgumentException If argument is not a Matrix
     * @throw \RuntimeException If given matrix has not the same size.
     * @param  Matrix $matrix Matrix to add
     * @access public
     * @return Matrix
     */
    public function add($matrix)
    {
        if (!($matrix instanceof Matrix)) {
            throw new \InvalidArgumentException('Given argument must be an instance of \Malenki\Math\Matrix');
        }

        if (!$this->sameSize($matrix)) {
            throw new \RuntimeException('Cannot adding given matrix: it has wrong size.');
        }

        $out = new self($this->size->rows, $this->size->cols);

        foreach ($this->arr as $k => $v) {
            $arrOther = $matrix->getRow($k);
            $arrNew = array();

            foreach ($v as $kk => $vv) {
                if ($arrOther[$kk] instanceof Complex) {
                    $arrNew[] = $arrOther[$kk]->add($vv);
                } elseif ($vv instanceof Complex) {
                    $arrNew[] = $vv->add($arrOther[$kk]);
                } else {
                    $arrNew[] = $arrOther[$kk] + $vv;
                }
            }

            $out->addRow($arrNew);
        }

        return $out;
    }

    /**
     * Multiplies current matrix to another one or to a scalar.
     *
     * @throw \RuntimeException If argument is not valid number or if the given
     * matrix has not the right numbers of rows.
     * @param  mixed  $mix Number (Real or Complex) or Matrix
     * @access public
     * @return Matrix
     */
    public function multiply($mix)
    {
        if (!$this->multiplyAllow($mix)) {
            throw new \RuntimeException('Invalid number or matrix has not right number of rows.');
        }

        if ($mix instanceof Matrix) {
            $out = new self($this->size->rows, $mix->cols);

            for ($r = 0; $r < $this->size->rows; $r++) {
                $arrOutRow = array();

                for ($c = 0; $c < $mix->cols; $c++) {
                    $arrCol = $mix->getCol($c);
                    $arrRow = $this->getRow($r);

                    $arrItem = array();
                    $hasComplex = false;

                    foreach ($arrCol as $k => $v) {
                        if ($arrRow[$k] instanceof Complex) {
                            $arrItem[] = $arrRow[$k]->multiply($v);
                            $hasComplex = true;
                        } elseif ($v instanceof Complex) {
                            $arrItem[] = $v->multiply($arrRow[$k]);
                            $hasComplex = true;
                        } else {
                            $arrItem[] = $arrRow[$k] * $v;
                        }
                    }

                    if ($hasComplex) {
                        $sum = new Complex(0, 0);

                        foreach ($arrItem as $item) {
                            if (is_numeric($item)) {
                                $item = new Complex($item, 0);
                            }

                            $sum = $item->add($sum);
                        }

                        $arrOutRow[] = $sum;
                    } else {
                        $arrOutRow[] = array_sum($arrItem);
                    }
                }

                $out->addRow($arrOutRow);
            }

            return $out;
        }

        if (is_numeric($mix) || $mix instanceof Complex) {
            $out = new self($this->size->rows, $this->size->cols);

            for ($r = 0; $r < $this->size->rows; $r++) {
                $arrRow = $this->getRow($r);

                foreach ($arrRow as $k => $v) {
                    if (is_numeric($mix)) {
                        if ($v instanceof Complex) {
                            $arrRow[$k] = $v->multiply($mix);
                        } else {
                            $arrRow[$k] = $mix * $v;
                        }
                    } else {
                        $arrRow[$k] = $mix->multiply($v);
                    }
                }

                $out->addRow($arrRow);
            }

            return $out;
        }
    }

    /**
     * Gets the cofactor matrix
     *
     * @access public
     * @return Matrix
     */
    public function cofactor()
    {
        $c = new self($this->size->rows, $this->size->cols);

        for ($m = 0; $m < $this->size->rows; $m++) {
            $arr_row = array();

            for ($n = 0; $n < $this->size->cols; $n++) {
                if ($this->size->cols == 2) {
                    $arr_row[] = pow(-1, $m + $n) * $this->subMatrix($m, $n)->get(0,0);
                } else {
                    $arr_row[] = pow(-1, $m + $n) * $this->subMatrix($m, $n)->det();
                }
            }

            $c->addRow($arr_row);
        }

        return $c;
    }

    public function adjugate()
    {
        return $this->cofactor()->transpose();
    }

    /**
     * Gets inverse matrix.
     *
     * Inverse matrix is get if and only if the determinant is not null.
     *
     * @throw \RuntimeException If determinant is null.
     * @access public
     * @return Matrix
     */
    public function inverse()
    {
        $det = $this->det();

        if ($det == 0) {
            throw new \RuntimeException('Cannot get inverse matrix: determinant is nul!');
        }

        return $this->adjugate()->multiply(1 / $det);
    }

    /**
     * Gets sub matrix from current.
     *
     * This is usefull for determinant calculus, inverse, etc.
     *
     * @param  integer $int_m Index of rows to ignore
     * @param  integer $int_n Index of column to ignore
     * @access public
     * @return Matrix
     */
    public function subMatrix($int_m, $int_n)
    {
        $sm = new self($this->size->rows - 1, $this->size->cols - 1);

        foreach ($this->arr as $m => $row) {
            if ($m != $int_m) {
                $arr_row = array();

                foreach ($row as $n => $v) {
                    if ($n != $int_n) {
                        $arr_row[] = $v;
                    }
                }

                $sm->addRow($arr_row);
            }
        }

        return $sm;
    }

    /**
     * Computes the determinant.
     *
     * @todo What about complex number?
     * @throw \RuntimeException If matrix is not square.
     * @access public
     * @return float
     */
    public function det()
    {
        if (!$this->isSquare()) {
            throw new \RuntimeException('Cannot compute determinant of non square matrix!');
        }

        if ($this->size->rows == 2) {
            return $this->get(0,0) * $this->get(1,1) - $this->get(0,1) * $this->get(1,0);
        } else {
            $int_out = 0;

            $arr_row = $this->arr[0];

            foreach ($arr_row as $n => $v) {
                $int_out += pow(-1, $n + 2) * $v * $this->subMatrix(0, $n)->det();
            }

            return $int_out;
        }
    }

    public function trace()
    {
        if (!$this->isSquare()) {
            throw new \RuntimeException('Cannot compute trace of non square matrix!');
        }

        $int = 0;

        for ($i = 0; $i < $this->size->rows; $i++) {
            $int += $this->get($i, $i);
        }

        return $int;
    }

    public function determinant()
    {
        return $this->det();
    }

    /**
     * Gets all data of the current matrix as 2 dimensions array
     *
     * @access public
     * @return array
     */
    public function getAll()
    {
        return $this->arr;
    }

    /**
     * Returns human readable matrix string like a pseudo table.
     *
     * @access public
     * @return string
     */
    public function __toString()
    {
        $arr_out = array();
        $arr_col_width = array();

        foreach ($this->arr as $row) {
            $arr_row = array();

            foreach ($row as $k => $item) {
                $arr_row[] = (string) $item;

                $int_length = strlen($arr_row[count($arr_row) - 1]);

                if(
                    (isset($arr_col_width[$k]) && $arr_col_width[$k] < $int_length)
                    ||
                    !isset($arr_col_width[$k])
                )
                {
                    $arr_col_width[$k] = $int_length;
                }
            }

            $arr_out[] = $arr_row;
        }

        foreach ($arr_out as $idx => $row) {
            $arr_row = array();

            foreach ($row as $k => $item) {
                $arr_row[] = str_pad($item, $arr_col_width[$k], ' ' ,STR_PAD_LEFT);
            }

            $arr_out[$idx] = implode('  ', $arr_row);
        }

        return implode(PHP_EOL, $arr_out);
    }
}

redpajama_set_name: RedPajamaGithub


text:
package org.adbcj.mysql.codec.packets;

import org.adbcj.DbException;
import org.adbcj.mysql.codec.MysqlCharacterSet;
import org.adbcj.support.CancellationToken;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

/**
 * @author roman.stoffel@gamlor.info
 * @since 11.04.12
 */
public class StringCommandRequest extends CommandRequest {
    private final String payload;
    private byte[] dataAsBytes = null;

    public StringCommandRequest(Command command, String payload, CancellationToken cancelSupport) {
        super(command,cancelSupport);
        this.payload = payload;
    }

    @Override
    public int getLength() {
        return 1 + payloadAsBinary().length;
    }

    @Override
    protected boolean hasPayload() {
        return payload != null;
    }

    @Override
    protected void writePayLoad(OutputStream out) throws IOException {
        out.write(payloadAsBinary());
    }

    @Override
    public String toString() {
        return "StringCommandRequest{" +
                "payload='" + payload + '\'' +
                ", dataAsBytes=" + payloadAsBinary() +
                '}';
    }

    private byte[] payloadAsBinary() {
        if(null!=dataAsBytes){
            return dataAsBytes;
        }
        try {
            dataAsBytes = payload.getBytes(MysqlCharacterSet.UTF8_UNICODE_CI.getCharsetName());
            return dataAsBytes;
        } catch (UnsupportedEncodingException e) {
            throw DbException.wrap(e);
        }
    }

}


text:
require 'spec_helper'

describe Duckface do

  context 'not_respond_to' do

    it 'works' do
      not_duck = Duckface.new do
        not_respond_to :quack
      end

      expect(not_duck.describes? Duck.new).to eq(false)
      expect(not_duck.describes? Dove.new).to eq(true)
      expect(not_duck.describes? Mandarin.new).to eq(false)
    end

    it 'works in group' do
      not_duck = Duckface.new do
        not_respond_to :quack
        not_respond_to :orange?
      end

      expect(not_duck.describes? Duck.new).to eq(false)
      expect(not_duck.describes? Dove.new).to eq(true)
      expect(not_duck.describes? Mandarin.new).to eq(false)
    end

    it 'works with defult objects' do
      not_struct = Duckface.new do
        not_respond_to :[]
      end

      expect(not_struct.describes?({})     ).to eq(false)
      expect(not_struct.describes? []      ).to eq(false)
      expect(not_struct.describes? 'string').to eq(false)
      expect(not_struct.describes? nil     ).to eq(true)
      expect(not_struct.describes? 17.3    ).to eq(true)
      expect(not_struct.describes? 173     ).to eq(false)
    end

  end

end


text:
package com.prediction.HMM.HMMDecoding;

import com.prediction.HMM.HMMObservation.Observation;
import com.prediction.HMM.HMMs.HMM;

public class DecodeGLRHMM extends Decoding {

	public DecodeGLRHMM(Observation observation, HMM hmm) {
		super.hmm = hmm;
		super.observation = observation;
		super.obsLength= observation.getVectorLength();
		super.stateLength = hmm.getStateLength();
		super.newStateMatrix = new int[obsLength];
	}

	public void Viterbi() {
		updateState();
	}

	public double chi(int obsIndex, int stateIndex) {

		if (obsIndex == 1) {
			return hmm.getPiMatrix(stateIndex)
					* hmm.getEmissionProbMatrix(stateIndex,
							observation.getVector(1));
		} else {
			double max = 0;

			for (int j = 1; j <= stateLength; j++) {

				if (chi(obsIndex - 1, j)
						* hmm.getStateTransitProbMatrix(j, stateIndex) > max) {
					max = chi(obsIndex - 1, j)
							* hmm.getStateTransitProbMatrix(j, stateIndex);
				}
			}

			return max * hmm.getEmissionProbMatrix(stateIndex, observation.getVector(obsIndex));
		}
	}

	public int tau(int obsIndex, int stateIndex) {
		int maxIndex = 0;
		double max = 0;

		for (int j = 1; j <= stateLength; j++) {

			if (chi(obsIndex - 1, j)
					* hmm.getStateTransitProbMatrix(j, stateIndex) > max) {
				maxIndex = j;
				max = chi(obsIndex - 1, j)
						* hmm.getStateTransitProbMatrix(j, stateIndex);
			}
		}

		return maxIndex;
	}

	public int getOptimalState(int obsIndex) {

		if (obsIndex == obsLength) {
			double max = 0;
			int maxIndex = 0;

			for (int j = 1; j <= stateLength; j++) {

				if(chi(obsLength, j) > max) {
					max = chi(obsLength, j);
					maxIndex = j;
				}
			}

			return maxIndex;
		} else {

			return tau(obsIndex + 1, getOptimalState(obsIndex + 1));
		}
	}

	public void updateState() {

		for (int i = 0; i < obsLength; i++) {
			newStateMatrix[i] = getOptimalState(i + 1);
		}
	}

	public int getState(int index) {
		return newStateMatrix[index - 1];
	}

	public void printUpdate() {
		System.out.println("<Optimal States>");

		System.out.print("[ ");
		for (int i = 1; i <= obsLength; i++) {
			System.out.print(getState(i) + " ");
		}
		System.out.print("]");
	}
}


text:
#ifndef TM_H
#define TM_H 1

#include <stdlib.h>

//#define TXOS 1
//#define HOARD 1

#include <osa_simics.h>
#include <osa_opcodes.h>
#include <osa_spinlock.h>
#include <osa_txos.h>
#include <osa_ordertm.h>

#ifdef ORDERTM
#define XBEGIN() ORDERTM_BEGIN()
#define XEND() ORDERTM_END()
#else
static __inline__ void XBEGIN() {
   _xbegin(0);
   TXOS_BEGIN();
}
static __inline__ void XEND() {
   if(TXOS_END()) {
      return;
   }
   _xend(0);
}
#endif /* ORDERTM */

#ifdef HAVE_CONFIG_H
# include "STAMP_config.h"
#endif

//#ifndef STM
//#include <barrier_utils.h>
//#define STM_THREAD_T void
//#else
//#include <stm.h>
//#endif

/* =============================================================================
 * Simulator Specific Interface
 *
 * MAIN(argc, argv)
 *     Declare the main function with argc being the identifier for the argument
 *     count and argv being the name for the argument string list
 *
 * MAIN_RETURN(int_val)
 *     Returns from MAIN function
 *
 * GOTO_SIM()
 *     Switch simulator to simulation mode
 *
 * GOTO_REAL()
 *     Switch simulator to non-simulation (real) mode
 *     Note: use in sequential region only
 *
 * IS_IN_SIM()
 *     Returns true if simulator is in simulation mode
 *
 * SIM_GET_NUM_CPU(var)
 *     Assigns the number of simulated CPUs to "var"
 *
 * P_MEMORY_STARTUP
 *     Start up the memory allocator system that handles malloc/free
 *     in parallel regions (but not in transactions)
 *
 * P_MEMORY_SHUTDOWN
 *     Shutdown the memory allocator system that handles malloc/free
 *     in parallel regions (but not in transactions)
 *
 * =============================================================================
 */

#define PREALLOC_BITS 26

#ifdef SIMULATOR

 #  include <simapi.h>

#  define MAIN(argc, argv)              void mainX (int argc, \
                                                    const char** argv, \
                                                    const char** envp)
#  define MAIN_RETURN(val)              return /* value is ignored */

#  define GOTO_SIM()                    goto_sim()
#  define GOTO_REAL()                   goto_real()
#  define IS_IN_SIM()                   (inSimulation)

#  define SIM_GET_NUM_CPU(var)          ({ \
                                            if (!IS_IN_SIM()) { \
                                                GOTO_SIM(); \
                                                var = Sim_GetNumCpus(); \
                                                GOTO_REAL(); \
                                            } else { \
                                                var = Sim_GetNumCpus(); \
                                            } \
                                            var; \
                                        })

#  define TM_PRINTF                     Sim_Print
#  define TM_PRINT0                     Sim_Print0
#  define TM_PRINT1                     Sim_Print1
#  define TM_PRINT2                     Sim_Print2
#  define TM_PRINT3                     Sim_Print3

#  include "memory.h"
#  define P_MEMORY_STARTUP(numThread)   do {		   \
    bool_t status;							\
    status = memory_init((numThread),					\
			 ((1<<PREALLOC_BITS) / numThread),		\
			 2);						\
    assert(status);							\
  } while (0) /* enforce comma */

#  define P_MEMORY_STARTUP_COLORS(numThread, colors)			\
  do {									\
    bool_t status;							\
    status = memory_init_colors((numThread),				\
				(1<<PREALLOC_BITS / numThread),		\
				2, colors);				\
    assert(status);                                                     \
} while (0) /* enforce comma */

#  define P_MEMORY_SHUTDOWN()

#else /* !SIMULATOR */

#ifdef HOARD

#define P_MEMORY_STARTUP(numThread)
#define P_MEMORY_STARTUP_COLORS(numThread, colors)

#else /* !HOARD */

#  include "memory.h"
#  define P_MEMORY_STARTUP(numThread)   do {		   \
    bool_t status;							\
    status = memory_init((numThread),					\
			 ((1<<PREALLOC_BITS) / numThread),		\
			 2);						\
    assert(status);							\
  } while (0) /* enforce comma */

#  define P_MEMORY_STARTUP_COLORS(numThread, colors)			\
  do {									\
    bool_t status;							\
    status = memory_init_colors((numThread),				\
				(1<<PREALLOC_BITS / numThread),		\
				2, colors);				\
    assert(status);							\
} while (0) /* enforce comma */

#endif /* HOARD */

#  define P_MEMORY_SHUTDOWN()

#  define MAIN(argc, argv)              int main (int argc, char** argv)
#  define MAIN_RETURN(val)              return val

#  define GOTO_SIM()                    /* nothing */
#  define GOTO_REAL()                   /* nothing */
#  define IS_IN_SIM()                   (0)

#  define SIM_GET_NUM_CPU(var)          /* nothing */

#  define TM_PRINTF                     printf
#  define TM_PRINT0                     printf
#  define TM_PRINT1                     printf
#  define TM_PRINT2                     printf
#  define TM_PRINT3                     printf

// #  define P_MEMORY_STARTUP(numThread)   /* nothing */
// #  define P_MEMORY_SHUTDOWN()           /* nothing */

#endif /* !SIMULATOR */

/* =============================================================================
 * Transactional Memory System Interface
 *
 * TM_ARG
 * TM_ARG_ALONE
 * TM_ARGDECL
 * TM_ARGDECL_ALONE
 *     Used to pass TM thread meta data to functions (see Examples below)
 *
 * TM_STARTUP(numThread)
 *     Startup the TM system (call before any other TM calls)
 *
 * TM_SHUTDOWN()
 *     Shutdown the TM system
 *
 * TM_THREAD_ENTER()
 *     Call when thread first enters parallel region
 *
 * TM_THREAD_EXIT()
 *     Call when thread exits last parallel region
 *
 * P_MALLOC(size)
 *     Allocate memory inside parallel region
 *
 * P_FREE(ptr)
 *     Deallocate memory inside parallel region
 *
 * TM_MALLOC(size)
 *     Allocate memory inside atomic block / transaction
 *
 * TM_FREE(ptr)
 *     Deallocate memory inside atomic block / transaction
 *
 * TM_BEGIN()
 *     Begin atomic block / transaction
 *
 * TM_BEGIN_RO()
 *     Begin atomic block / transaction that only reads shared data
 *
 * TM_END()
 *     End atomic block / transaction
 *
 * TM_RESTART()
 *     Restart atomic block / transaction
 *
 * TM_EARLY_RELEASE()
 *     Remove speculatively read line from the read set
 *
 * =============================================================================
 *
 * Example Usage:
 *
 *     MAIN(argc,argv)
 *     {
 *         TM_STARTUP(8);
 *         // create 8 threads and go parallel
 *         TM_SHUTDOWN();
 *     }
 *
 *     void parallel_region ()
 *     {
 *         TM_THREAD_ENTER();
 *         subfunction1(TM_ARG_ALONE);
 *         subfunction2(TM_ARG  1, 2, 3);
 *         TM_THREAD_EXIT();
 *     }
 *
 *     void subfunction1 (TM_ARGDECL_ALONE)
 *     {
 *         TM_BEGIN_RO()
 *         // ... do work that only reads shared data ...
 *         TM_END()
 *
 *         long* array = (long*)P_MALLOC(10 * sizeof(long));
 *         // ... do work ...
 *         P_FREE(array);
 *     }
 *
 *     void subfunction2 (TM_ARGDECL  long a, long b, long c)
 *     {
 *         TM_BEGIN();
 *         long* array = (long*)TM_MALLOC(a * b * c * sizeof(long));
 *         // ... do work that may read or write shared data ...
 *         TM_FREE(array);
 *         TM_END();
 *     }
 *
 * =============================================================================
 */

/* =============================================================================
 * HTM - Hardware Transactional Memory
 * =============================================================================
 */

#ifdef HTM

// #  ifndef SIMULATOR
// #    error HTM requries SIMULATOR
// #  endif

#  include <assert.h>
// #  include <tmapi.h>
#  include "memory.h"
#  include "thread.h"
#  include "types.h"

#  define TM_ARG                        /* nothing */
#  define TM_ARG_ALONE                  /* nothing */
#  define TM_ARGDECL                    /* nothing */
#  define TM_ARGDECL_ALONE              /* nothing */
#  define TM_CALLABLE                   /* nothing */

#  define TM_STARTUP(numThread)         /* nothing */
#  define TM_SHUTDOWN()                 /* nothing */

#  define TM_THREAD_ENTER()             /* nothing */
#  define TM_THREAD_EXIT()              /* nothing */
#  define TM_INIT_LOCAL(x)              init_ordertm_local(x)

#ifdef HOARD

#  define P_MALLOC(size)                malloc(size)
#  define P_MALLOC_COLOR(size, color)   malloc(size)
#  define P_FREE(ptr)                   free(ptr)
#  define P_FREE_COLOR(ptr, color)      free(ptr)
#  define TM_MALLOC(size)               malloc(size)
#  define TM_MALLOC_COLOR(size, color)  TM_MALLOC(size)

// The only sensible thing to do here is just free in the tx.
#  define TM_FREE(ptr)                  free(ptr)
#  define TM_FREE_COLOR(ptr, color)     TM_FREE(ptr)

#else /* Bump allocator */

#  define P_MALLOC(size)                memory_get(thread_getId(), size)
#  define P_MALLOC_COLOR(size, color)   memory_get_color(thread_getId(), size, color)
#  define P_FREE(ptr)                   /* TODO: thread local free is non-trivial */
#  define P_FREE_COLOR(ptr, color)      /* TODO: thread local free is non-trivial */
#  define TM_MALLOC(size)               memory_get(thread_getId(), size)
#  define TM_MALLOC_COLOR(size, color)  memory_get_color(thread_getId(), size, color)

// The only sensible thing to do here is just free in the tx.
#  define TM_FREE(ptr)                  /* TODO: thread local free is non-trivial */
#  define TM_FREE_COLOR(ptr, color)     /* TODO: thread local free is non-trivial */

#endif /* HOARD */

#  define TM_BEGIN()                    XBEGIN()
#  define TM_BEGIN_RO()                 XBEGIN()
#  define TM_END(id)                      XEND(id)
#  define TM_RESTART()                  /* nothing */

#  define OSA_PRINT(str,num)            osa_print(str, num)
#  define START_INSTRUMENTATION()       magic_instruction(OSA_CLEAR_STAT)
#  define STOP_INSTRUMENTATION()        magic_instruction(OSA_OUTPUT_STAT)

#  define TM_EARLY_RELEASE(var)         /* nothing */

/* =============================================================================
 * ORDERTM - Ordered Transactional Memory
 * =============================================================================
 */

#elif defined(ORDERTM)

#  include <string.h>
#  include <stm.h>
#  include <assert.h>
// #  include <tmapi.h>
#  include "memory.h"
#  include "thread.h"
#  include "types.h"

#  define TM_ARG                        STM_SELF,
#  define TM_ARG_ALONE                  STM_SELF
#  define TM_ARGDECL                    STM_THREAD_T* TM_ARG
#  define TM_ARGDECL_ALONE              STM_THREAD_T* TM_ARG_ALONE
#  define TM_CALLABLE                   /* nothing */

#  define TM_STARTUP(numThread)         STM_STARTUP()
#  define TM_SHUTDOWN()                 STM_SHUTDOWN()
#  define TM_THREAD_ENTER()             TM_ARGDECL_ALONE = STM_NEW_THREAD(); \
                                        STM_INIT_THREAD(TM_ARG_ALONE, thread_getId())
#  define TM_THREAD_EXIT()              STM_FREE_THREAD(TM_ARG_ALONE)
#  define TM_INIT_LOCAL(x)              init_ordertm_local(x)

#  define P_MALLOC(size)                memory_get(thread_getId(), size)
#  define P_MALLOC_COLOR(size, color)   memory_get_color(thread_getId(), size, color)
#  define P_FREE(ptr)                   /* TODO: thread local free is non-trivial */
#  define P_FREE_COLOR(ptr, color)      /* TODO: thread local free is non-trivial */
#  define TM_MALLOC(size)               memory_get(thread_getId(), size)
#  define TM_MALLOC_COLOR(size, color)  memory_get_color(thread_getId(), size, color)
#  define TM_FREE(ptr)                  /* TODO: thread local free is non-trivial */
#  define TM_FREE_COLOR(ptr, color)     /* TODO: thread local free is non-trivial */

#  define TM_BEGIN()                    XBEGIN()
#  define TM_BEGIN_RO()                 XBEGIN()
#  define TM_END()                      XEND()
#  define TM_RESTART()                  {if(_xgettxid()) { _xretry(0); } else { STM_RESTART(); }}

#  define OSA_PRINT(str,num)            osa_print(str, num)
#  define START_INSTRUMENTATION()       magic_instruction(OSA_CLEAR_STAT)
#  define STOP_INSTRUMENTATION()        magic_instruction(OSA_OUTPUT_STAT)

#  define TM_EARLY_RELEASE(var)         /* nothing */

/* Need to read and write barriers if
 * the transaction is running in software,
 * otherwise let the hardware do the work.
 */
#  define TM_SHARED_READ(var)           ORDERTM_READ(var)
#  define TM_SHARED_READ_P(var)         ORDERTM_READ_P(var)
#  define TM_SHARED_READ_F(var)         ORDERTM_READ_F(var)

#  define TM_SHARED_WRITE(var, val)     ORDERTM_WRITE(var, val)
#  define TM_SHARED_WRITE_P(var, val)   ORDERTM_WRITE_P(var, val)
#  define TM_SHARED_WRITE_F(var, val)   ORDERTM_WRITE_F(var, val)

#  define TM_LOCAL_WRITE(var, val)      ORDERTM_LOCAL_WRITE(var, val)
#  define TM_LOCAL_WRITE_P(var, val)    ORDERTM_LOCAL_WRITE_P(var, val)
#  define TM_LOCAL_WRITE_F(var, val)    ORDERTM_LOCAL_WRITE_F(var, val)

/* =============================================================================
 * STM - Software Transactional Memory
 * =============================================================================
 */

#elif defined(STM)

#  include <string.h>
#  include <stm.h>
#  include "thread.h"

#  define TM_ARG                        STM_SELF,
#  define TM_ARG_ALONE                  STM_SELF
#  define TM_ARGDECL                    STM_THREAD_T* TM_ARG
#  define TM_ARGDECL_ALONE              STM_THREAD_T* TM_ARG_ALONE
#  define TM_CALLABLE                   /* nothing */

#  ifdef SIMULATOR

#    define TM_STARTUP(numThread)       STM_STARTUP(); \
                                        STM_NEW_THREADS(numThread)
#    define TM_SHUTDOWN()               STM_SHUTDOWN()

#    define TM_THREAD_ENTER()           TM_ARGDECL_ALONE = \
                                            STM_GET_THREAD(thread_getId()); \
                                        STM_SET_SELF(TM_ARG_ALONE)

#    define TM_THREAD_EXIT()            STM_FREE_THREAD(TM_ARG_ALONE)

#    define P_MALLOC(size)              memory_get(thread_getId(), size)
#    define P_FREE(ptr)                 /* TODO: thread local free is non-trivial */
#    define TM_MALLOC(size)             memory_get(thread_getId(), size)
#    define TM_FREE(ptr)                /* TODO: thread local free is non-trivial */

#  else /* !SIMULATOR */

#    define TM_STARTUP(numThread)       STM_STARTUP()
#    define TM_SHUTDOWN()               STM_SHUTDOWN()

#    define TM_THREAD_ENTER()           TM_ARGDECL_ALONE = STM_NEW_THREAD(); \
                                        STM_INIT_THREAD(TM_ARG_ALONE, thread_getId())
#    define TM_THREAD_EXIT()            STM_FREE_THREAD(TM_ARG_ALONE)

#    define P_MALLOC(size)              memory_get(thread_getId(), size)
#    define P_FREE(ptr)                 /* TODO: thread local free is non-trivial */
#    define TM_MALLOC(size)             memory_get(thread_getId(), size)
#    define TM_FREE(ptr)                /* TODO: thread local free is non-trivial */
#    define P_MALLOC_COLOR(size, color) memory_get(thread_getId(), size)
#    define P_FREE_COLOR(ptr, color)    /* TODO */
#    define TM_MALLOC_COLOR(size, color) memory_get(thread_getId(), size)
#    define TM_FREE_COLOR(ptr, color)   /* TODO */

#  endif /* !SIMULATOR */

#  define TM_BEGIN()                    STM_BEGIN_WR()
#  define TM_BEGIN_RO()                 STM_BEGIN_RD()
#  define TM_END()                      STM_END()
#  define TM_RESTART()                  STM_RESTART()

#  define OSA_PRINT(str,num)            osa_print(str, num)
#  define START_INSTRUMENTATION()       magic_instruction(OSA_CLEAR_STAT)
#  define STOP_INSTRUMENTATION()        magic_instruction(OSA_OUTPUT_STAT)

/* =============================================================================
 * Spinlock-based serial execution
 * =============================================================================
 */

#elif defined(LOCK)

#  include <assert.h>

#  define TM_ARG                        /* nothing */
#  define TM_ARG_ALONE                  /* nothing */
#  define TM_ARGDECL                    /* nothing */
#  define TM_ARGDECL_ALONE              /* nothing */
#  define TM_CALLABLE                   /* nothing */
#  define TM_CALLABLE                   /* nothing */

#  define TM_STARTUP(numThread)         /* nothing */
#  define TM_SHUTDOWN()                 /* nothing */

#  define TM_THREAD_ENTER()             /* nothing */
#  define TM_THREAD_EXIT()              /* nothing */

#  ifdef SIMULATOR

#    include "thread.h"

#    define P_MALLOC(size)              memory_get(thread_getId(), size)
#    define P_FREE(ptr)                 /* TODO: thread local free is non-trivial */
#    define TM_MALLOC(size)             memory_get(thread_getId(), size)
#    define TM_FREE(ptr)                /* TODO: thread local free is non-trivial */
#    define P_MALLOC_COLOR(size, color) memory_get_color(thread_getId(), size, color)
#    define P_FREE_COLOR(ptr, color)    /* TODO: thread local free is non-trivial */
#    define TM_MALLOC_COLOR(size, color)memory_get_color(thread_getId(), size, color)
#    define TM_FREE_COLOR(ptr, color)   /* TODO: thread local free is non-trivial */

#  else /* !SIMULATOR */

#    include "thread.h"

#    define P_MALLOC(size)              memory_get(thread_getId(), size)
#    define P_FREE(ptr)                 /* TODO: thread local free is non-trivial */
#    define TM_MALLOC(size)             memory_get(thread_getId(), size)
#    define TM_FREE(ptr)                /* TODO: thread local free is non-trivial */

#     define P_MALLOC_COLOR(size, color) memory_get_color(thread_getId(), size, color)
#     define P_FREE_COLOR(ptr, color)    /* TODO: thread local free is non-trivial */
#     define TM_MALLOC_COLOR(size, color)memory_get_color(thread_getId(), size, color)
#     define TM_FREE_COLOR(ptr, color)   /* TODO: thread local free is non-trivial */
#  endif /* !SIMULATOR */

#  define TM_BEGIN()                    spin_lock(&globalLock)
#  define TM_BEGIN_RO()                 spin_lock(&globalLock)
#  define TM_END()                      spin_unlock(&globalLock)
#  define TM_RESTART()                  assert(0)

#  define OSA_PRINT(str,num)            osa_print(str, num)
#  define START_INSTRUMENTATION()       magic_instruction(OSA_CLEAR_STAT)
#  define STOP_INSTRUMENTATION()        magic_instruction(OSA_OUTPUT_STAT)

#  define TM_EARLY_RELEASE(var)         /* nothing */

/* =============================================================================
 * Sequential execution
 * =============================================================================
 */

#else /* SEQUENTIAL */

#  include <assert.h>

#  define TM_ARG                        /* nothing */
#  define TM_ARG_ALONE                  /* nothing */
#  define TM_ARGDECL                    /* nothing */
#  define TM_ARGDECL_ALONE              /* nothing */
#  define TM_CALLABLE                   /* nothing */
#  define TM_CALLABLE                   /* nothing */

#  define TM_STARTUP(numThread)         /* nothing */
#  define TM_SHUTDOWN()                 /* nothing */

#  define TM_THREAD_ENTER()             /* nothing */
#  define TM_THREAD_EXIT()              /* nothing */

#  ifdef SIMULATOR

#    include "thread.h"

#    define P_MALLOC(size)              memory_get(thread_getId(), size)
#    define P_FREE(ptr)                 /* TODO: thread local free is non-trivial */
#    define TM_MALLOC(size)             memory_get(thread_getId(), size)
#    define TM_FREE(ptr)                /* TODO: thread local free is non-trivial */
#    define P_MALLOC_COLOR(size, color) memory_get_color(thread_getId(), size, color)
#    define P_FREE_COLOR(ptr, color)    /* TODO: thread local free is non-trivial */
#    define TM_MALLOC_COLOR(size, color)memory_get_color(thread_getId(), size, color)
#    define TM_FREE_COLOR(ptr, color)   /* TODO: thread local free is non-trivial */

#  else /* !SIMULATOR */

#    include "thread.h"

#    define P_MALLOC(size)              malloc(size)
#    define P_FREE(ptr)                 free(ptr)
#    define TM_MALLOC(size)             malloc(size)
#    define TM_FREE(ptr)                free(ptr)
#    define P_MALLOC_COLOR(size, color) memory_get_color(thread_getId(), size, color)
#    define P_FREE_COLOR(ptr, color)    /* TODO: thread local free is non-trivial */
#    define TM_MALLOC_COLOR(size, color)memory_get_color(thread_getId(), size, color)
#    define TM_FREE_COLOR(ptr, color)   /* TODO: thread local free is non-trivial */

#  endif /* !SIMULATOR */

#  define TM_BEGIN()                    /* nothing */
#  define TM_BEGIN_RO()                 /* nothing */
#  define TM_END()                      /* nothing */
#  define TM_RESTART()                  // assert(0)

#  define OSA_PRINT(str,num)              printf(str);
#  define START_INSTRUMENTATION()         printf("Stats collection starts here: reset!\n");
#  define STOP_INSTRUMENTATION()          printf("Stats data should be dumped here!\n");

#  define TM_EARLY_RELEASE(var)         /* nothing */

#endif /* SEQUENTIAL */

/* =============================================================================
 * Transactional Memory System interface for shared memory accesses
 *
 * There are 3 flavors of each function:
 *
 * 1) no suffix: for accessing variables of size "long"
 * 2) _P suffix: for accessing variables of type "pointer"
 * 3) _F suffix: for accessing variables of type "float"
 * =============================================================================
 */
#if defined(STM)

#if defined(OTM)

#  define TM_SHARED_READ(var)           (var)
#  define TM_SHARED_READ_P(var)         (var)
#  define TM_SHARED_READ_F(var)         (var)

#  define TM_SHARED_WRITE(var, val)     ({var = val; var;})
#  define TM_SHARED_WRITE_P(var, val)   ({var = val; var;})
#  define TM_SHARED_WRITE_F(var, val)   ({var = val; var;})

#  define TM_LOCAL_WRITE(var, val)      ({var = val; var;})
#  define TM_LOCAL_WRITE_P(var, val)    ({var = val; var;})
#  define TM_LOCAL_WRITE_F(var, val)    ({var = val; var;})

#else /* OTM */

#  define TM_SHARED_READ(var)           STM_READ(var)
#  define TM_SHARED_READ_P(var)         STM_READ_P(var)
#  define TM_SHARED_READ_F(var)         STM_READ_F(var)

#  define TM_SHARED_WRITE(var, val)     STM_WRITE((var), val)
#  define TM_SHARED_WRITE_P(var, val)   STM_WRITE_P((var), val)
#  define TM_SHARED_WRITE_F(var, val)   STM_WRITE_F((var), val)

#  define TM_LOCAL_WRITE(var, val)      STM_LOCAL_WRITE(var, val)
#  define TM_LOCAL_WRITE_P(var, val)    STM_LOCAL_WRITE_P(var, val)
#  define TM_LOCAL_WRITE_F(var, val)    STM_LOCAL_WRITE_F(var, val)

#endif /* !OTM */

#else /* !STM */

#if !defined(ORDERTM)

#  define TM_SHARED_READ(var)           (var)
#  define TM_SHARED_READ_P(var)         (var)
#  define TM_SHARED_READ_F(var)         (var)

#  define TM_SHARED_WRITE(var, val)     ({var = val; var;})
#  define TM_SHARED_WRITE_P(var, val)   ({var = val; var;})
#  define TM_SHARED_WRITE_F(var, val)   ({var = val; var;})

#  define TM_LOCAL_WRITE(var, val)      ({var = val; var;})
#  define TM_LOCAL_WRITE_P(var, val)    ({var = val; var;})
#  define TM_LOCAL_WRITE_F(var, val)    ({var = val; var;})

#endif /* ORDERTM */
#endif /* !STM */

#endif /* TM_H */

/* =============================================================================
 *
 * End of tm.h
 *
 * =============================================================================
 */


text:
Examples
========

Examples to demonstrate the features of batchOpenMPI follow.

Multi-ref example
-----------------

If the *multiRef* flag in enabled for a **batchOpenMPI.batchFunction** instances, then duplicate inputs searched for as to save computing resources.

.. literalinclude:: ../examples/ex2.py
   :lines: 5-
   :emphasize-lines: 4

Call expected example
---------------------

.. literalinclude:: ../examples/ex3.py
   :lines: 5-
   :emphasize-lines: 7

Dependency examples
----------------------------

.. literalinclude:: ../examples/ex4.py
   :lines: 5-

.. literalinclude:: ../examples/ex4b.py
   :lines: 5-

.. literalinclude:: ../examples/ex4c.py
   :lines: 5-

Downward dependency examples

.. literalinclude:: ../examples/ex5.py
   :lines: 5-

.. literalinclude:: ../examples/ex5b.py
   :lines: 5-

Individual working directory for each process
----------------------

.. literalinclude:: ../examples/ex7.py
   :lines: 5-


text:
export BWA_HOME="/Users/mhanna/src/bwa"
export JAVA_INCLUDE="/System/Library/Frameworks/JavaVM.framework/Headers"
export TARGET_LIB="libbwa.dylib"
export EXTRA_LIBS="-lc -lz -lsupc++"
export LIBTOOL_COMMAND="libtool -dynamic"
make


text:
namespace crostini {

constexpr char kUmaPrefix[] = "Crostini";

CrostiniEngagementMetricsService*
CrostiniEngagementMetricsService::Factory::GetForProfile(Profile* profile) {
  return static_cast<CrostiniEngagementMetricsService*>(
      GetInstance()->GetServiceForBrowserContext(profile, true));
}

CrostiniEngagementMetricsService::Factory*
CrostiniEngagementMetricsService::Factory::GetInstance() {
  static base::NoDestructor<CrostiniEngagementMetricsService::Factory> factory;
  return factory.get();
}

CrostiniEngagementMetricsService::Factory::Factory()
    : BrowserContextKeyedServiceFactory(
          "CrostiniEngagementMetricsService",
          BrowserContextDependencyManager::GetInstance()) {}

CrostiniEngagementMetricsService::Factory::~Factory() = default;

KeyedService*
CrostiniEngagementMetricsService::Factory::BuildServiceInstanceFor(
    content::BrowserContext* context) const {
  Profile* profile = Profile::FromBrowserContext(context);
  return new CrostiniEngagementMetricsService(profile);
}

bool CrostiniEngagementMetricsService::Factory::
    ServiceIsCreatedWithBrowserContext() const {
  return true;
}

bool CrostiniEngagementMetricsService::Factory::ServiceIsNULLWhileTesting()
    const {
  // Checking whether Crostini is allowed requires more setup than is present
  // in most unit tests.
  return true;
}

CrostiniEngagementMetricsService::CrostiniEngagementMetricsService(
    Profile* profile) {
  if (!CrostiniFeatures::Get()->IsEnabled(profile))
    return;
  guest_os_engagement_metrics_ =
      std::make_unique<guest_os::GuestOsEngagementMetrics>(
          profile->GetPrefs(), base::BindRepeating(IsCrostiniWindow),
          prefs::kEngagementPrefsPrefix, kUmaPrefix);
}

CrostiniEngagementMetricsService::~CrostiniEngagementMetricsService() = default;

void CrostiniEngagementMetricsService::SetBackgroundActive(
    bool background_active) {
  // If policy changes to enable Crostini, we won't have created the helper
  // object. This should be relatively rare so for now we don't track this
  // case.
  if (!guest_os_engagement_metrics_)
    return;
  guest_os_engagement_metrics_->SetBackgroundActive(background_active);
}

}  // namespace crostini


text:
//
// Module: assemblies
// ==================
//
'use strict';
var helper = require('./helper');
//
//
// Constructor
// -----------
//
// #### Code
function Assemblies (opts, uri) {
  helper.init.call(this, opts, uri);
}

//
//
// Public: send an assembly to Transloadit API
// -------------------------------------------
//
// #### Parameters
//
// + `files` - `File Object` JSON (it may have several files)
// + `params`
// + `callback` - `Callback Function`
//
// #### Returns
//
// A `Callback` function with `(err, body)` from the request.
//
// #### Code
Assemblies.prototype.post = function (files, params, callback) {
  var self = this;
  var fs = require('fs');
  var request = require('request');
  var r = request.post(self.uri.base + self.uri.assemblies, function(err, response, body) {
    /* Here's the callback, we clean up the `response` and only return `body` */
    if (err)
      return callback(err);

    return callback(null, body);
  });

  var SignatureLib = require('./signature');
  var signature = new SignatureLib(self.opts);

  var form = r.form();

  var x = signature.create(params);

  (Object.keys(files)).forEach(function (file) {
    form.append(files[file].name, fs.createReadStream(files[file].path), {
      filename: files[file].name
    });
  });

  form.append('signature', x.signature);
  form.append('params', JSON.stringify(x.params));
};

//
//
// Public: cancel an assembly
// --------------------------
//
// #### Parameters
//
// + `url` - The assembly's URL returned when the assembly was created on the field `assembly_url`
// + `signature` - (Optional) signature with which the assembly was created
// + `callback` - `Callback Function`
//
// #### Returns
//
// A `Callback` function with `(err, body)` from the request.
//
// #### Code
Assemblies.prototype.cancel = function (url, signature, callback) {
  if (url == null || typeof(url) !== 'string')
    return callback(new Error('The URL (first) argument is required'));

  if (signature != null)
    url += '?signature=' + signature;

  var request = require('request');
  request({
    uri: url,
    method: 'DELETE'
  }, function (err, res, body) {
    return callback(err, body);
  });
};

module.exports = Assemblies;


text:
class AddAreaToProfileQuizzes < ActiveRecord::Migration
  def change
    add_column :profile_quizzes, :area, :integer
  end
end


text:
package dos

const FILE_ATTRIBUTE_NORMAL = 0x00000080
const FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400
const FILE_ATTRIBUTE_HIDDEN = 0x00000002
const CP_THREAD_ACP = 3


text:
function [isFun,Syntax] = isFunction(file)
% checks whether docFile is a function or a script file
%
% Input
%  file   - a @DocFile
%
% Output
%  isFun  - |true| / |false|
%  Syntax - if a function, get the Syntax of the Function

str = read(file);
% expect the first function string to be syntax and not comment ?!

hasKeyword = strfind(str,'function ');
isFun = false;
if ~isempty(hasKeyword)
  % Syntax = regexp(str,'(?<=^function )|(?<=\n[ ]*function ))(.*?)\n','match');

  lineBreak = regexp(str,'\n');
  if hasKeyword(1) == 1
    Syntax = str(10:lineBreak(1));
    isFun = true;
  else
    lastLineBreak = lineBreak(lineBreak<hasKeyword(1));
    lineTillFuncStr = str(lastLineBreak+1:hasKeyword(1)+7);

    nextLineBreak = lineBreak(lineBreak > hasKeyword(1));
    Syntax = str(hasKeyword(1)+9:nextLineBreak(1));
    if ~contains(lineTillFuncStr,'%%'), isFun = true; end
  end

  if exist('Syntax','var'), Syntax = ['%   ' strtrim(Syntax)]; end
else
  Syntax = [];
end


text:
package com.github.lisicnu.easydownload.utils;

import java.util.ArrayList;

/**
 *

 *

 * Author: Eden Lee

 * Date: 2016/4/10

 * Email: checkway@outlook.com

 * Version: 1.0

 */
public final class utils {
    public static ArrayList<String> splitString(String source, String separator, boolean removeEmpty) {
        if (source == null || source.isEmpty())
            return null;

        ArrayList<String> values = new ArrayList<String>();
        if (separator == null || separator.isEmpty()) {
            values.add(source);
            return values;
        }

        String tmpStr = new String(source);

        int idx = 0;
        String tt;
        while (true) {
            int tmp = tmpStr.indexOf(separator, idx);
            if (tmp == -1) {
                tt = tmpStr.substring(idx);

                if (tt != null && !tt.isEmpty())
                    values.add(tmpStr.substring(idx));

                break;
            }

            tt = tmpStr.substring(idx, tmp);
            if (tt != null && !tt.isEmpty())
                values.add(tmpStr.substring(idx, tmp));

            idx = tmp + separator.length();
        }

        return values;
    }
}


text:
package we.love.pluto.visualizer;

import java.util.Random;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;

/**
 * @author Michal Gajdos
 */
final class Test {

    public static void main(String[] args) throws Exception {
        final String[] words = generateRandomWords(20);
        final WebTarget target = ClientBuilder.newClient().target("http://localhost:5001/space-object/of-the-moment");

        while (true) {
            target.request().post(Entity.text(words[new Random().nextInt(words.length)]));
            Thread.sleep(1000);
        }
    }

    public static String[] generateRandomWords(int numberOfWords) {
        String[] randomStrings = new String[numberOfWords];
        Random random = new Random();
        for (int i = 0; i < numberOfWords; i++) {
            char[] word = new char[random.nextInt(8) + 3]; // words of length 3 through 10. (1 and 2 letter words are boring.)
            for (int j = 0; j < word.length; j++) {
                word[j] = (char) ('a' + random.nextInt(26));
            }
            randomStrings[i] = new String(word);
        }
        return randomStrings;
    }
}


text:
import { Component, OnInit, Input } from '@angular/core';
import { PlocicaComponent } from './plocica.component';
import { IMeni } from '../../../shared/modeli/meni.interface';
import { IRadnik } from '../../../shared/modeli/radnik';
import { TestLoginService } from '../../../shared/api/loginservice/test.login.service';
import { ToggleMenuService } from '../nav/topnav/toggle.service';

@Component({
    moduleId: module.id,
    selector: 'naslovna',
    templateUrl: 'naslovna.component.html',
    directives: [PlocicaComponent],
    styles: ['.state-overview {padding-left: 20px;}'],
    providers: [TestLoginService, ToggleMenuService]
})
export class NaslovnaComponent implements OnInit {
    title: string = 'Naslovna';
    poruka: string = 'Dobrodošli na Portal Pošte Srpske';
    $meni: IMeni[];
    @Input() meni: IMeni[];
    errorMsg: string;
    constructor(private _testLogin: TestLoginService,
                private _toggleSer: ToggleMenuService) {

                }

    ngOnInit() {
        this._testLogin.testPostLogin({SIFRA_RADNIKA: '685', TAJNA_SIFRA: '685'})
            .subscribe(
                radnik => this.$meni = radnik.VRSTA_ZADATAKA[0].DecaZadaci,
                error => this.errorMsg = error
            );
     }

}


text:
package scalaListsSuite

/**
 * Created by cganoo on 6/23/14.
 */
object ScalaListOps extends App {

  def customLast[T](xs: List[T]): T = xs match {
    case Nil => throw new Error("last of empty list is undefined")
    case y :: Nil => y
    case y :: ys => customLast(ys)
  }

  def customInit[T](xs: List[T]): List[T] = xs match {
    case Nil => throw new Error("init of empty list is undefined")
    case List(y) => Nil
    case y :: ys => y :: customInit(ys)
  }

  def customConcat[T](xs: List[T], ys: List[T]): List[T] = xs match {
    case Nil => ys
    case z :: zs => z :: customConcat(zs, ys)
  }

  def customReverse[T](xs: List[T]): List[T] = xs match {
    case Nil => xs
    case y :: ys => customReverse(ys) ++ List(y)
  }

  def customRemoveAt[T](n: Int, xs: List[T]): List[T] = (xs take n) ::: (xs drop n + 1)

  /**
   * Flattens the specified list by flattening any contained nested lists
   * Type erasure will cause abstract type to be unchecked in the type pattern used by the method
   * Hence using Any
   * @param xs the list to be flattened
   * @tparam Any
   * @return the flattened list devoid of any nested lists
   */
  def customFlatten[Any](xs: List[Any]): List[Any] = xs match {
    case Nil => xs
    case (y: List[Any]) :: ys => customFlatten(y) ::: customFlatten(ys)
    case _ => xs.head :: customFlatten(xs.tail)
  }
}


text:
#ifndef _LEPTON_AGC_H_
   #define _LEPTON_AGC_H_
   #ifdef __cplusplus
extern "C"
{
   #endif

/******************************************************************************/
/** INCLUDE FILES                                                            **/
/******************************************************************************/
   #include "LEPTON_Types.h"

/******************************************************************************/
/** EXPORTED DEFINES                                                         **/
/******************************************************************************/

/* AGC Module Command IDs
*/
   #define LEP_AGC_MODULE_BASE                     0x0100

   #define LEP_CID_AGC_ENABLE_STATE                (LEP_AGC_MODULE_BASE + 0x0000 )
   #define LEP_CID_AGC_POLICY                      (LEP_AGC_MODULE_BASE + 0x0004 )
   #define LEP_CID_AGC_ROI                         (LEP_AGC_MODULE_BASE + 0x0008 )
   #define LEP_CID_AGC_STATISTICS                  (LEP_AGC_MODULE_BASE + 0x000C )
   #define LEP_CID_AGC_HISTOGRAM_CLIP_PERCENT      (LEP_AGC_MODULE_BASE + 0x0010 )
   #define LEP_CID_AGC_HISTOGRAM_TAIL_SIZE         (LEP_AGC_MODULE_BASE + 0x0014 )
   #define LEP_CID_AGC_LINEAR_MAX_GAIN             (LEP_AGC_MODULE_BASE + 0x0018 )
   #define LEP_CID_AGC_LINEAR_MIDPOINT             (LEP_AGC_MODULE_BASE + 0x001C )
   #define LEP_CID_AGC_LINEAR_DAMPENING_FACTOR     (LEP_AGC_MODULE_BASE + 0x0020 )
   #define LEP_CID_AGC_HEQ_DAMPENING_FACTOR        (LEP_AGC_MODULE_BASE + 0x0024 )
   #define LEP_CID_AGC_HEQ_MAX_GAIN                (LEP_AGC_MODULE_BASE + 0x0028 )
   #define LEP_CID_AGC_HEQ_CLIP_LIMIT_HIGH         (LEP_AGC_MODULE_BASE + 0x002C )
   #define LEP_CID_AGC_HEQ_CLIP_LIMIT_LOW          (LEP_AGC_MODULE_BASE + 0x0030 )
   #define LEP_CID_AGC_HEQ_BIN_EXTENSION           (LEP_AGC_MODULE_BASE + 0x0034 )
   #define LEP_CID_AGC_HEQ_MIDPOINT                (LEP_AGC_MODULE_BASE + 0x0038 )
   #define LEP_CID_AGC_HEQ_EMPTY_COUNTS            (LEP_AGC_MODULE_BASE + 0x003C )
   #define LEP_CID_AGC_HEQ_NORMALIZATION_FACTOR    (LEP_AGC_MODULE_BASE + 0x0040 )
   #define LEP_CID_AGC_HEQ_SCALE_FACTOR            (LEP_AGC_MODULE_BASE + 0x0044 )
   #define LEP_CID_AGC_CALC_ENABLE_STATE           (LEP_AGC_MODULE_BASE + 0x0048 )
   #define LEP_CID_AGC_HEQ_LINEAR_PERCENT          (LEP_AGC_MODULE_BASE + 0x004C )

/* AGC Module Attribute Scaling and Module Attribute Limits
*/
/* Linear
*/
   #define LEP_AGC_MAX_HISTOGRAM_CLIP_PERCENT      100         /* Scale is 10x  100 == 10.0%  */
   #define LEP_AGC_MAX_HISTOGRAM_TAIL_SIZE         (80*60)     /* Scale is 1x  units: pixels  */
   #define LEP_AGC_MIN_LINEAR_MAX_GAIN              1          /* Scale is 1x    */
   #define LEP_AGC_MAX_LINEAR_MAX_GAIN              4          /* Scale is 1x    */
   #define LEP_AGC_MIN_LINEAR_MIDPOINT              1          /* Scale is 1x    */
   #define LEP_AGC_MAX_LINEAR_MIDPOINT              254        /* Scale is 1x    */
   #define LEP_AGC_MAX_LINEAR_DAMPENING_FACTOR      100        /* Scale is 1x  units: percent  */

/* Histogram Equalization
*/
   #define LEP_AGC_MAX_HEQ_DAMPENING_FACTOR         100        /* Scale is 1x  units: percent  */
   #define LEP_AGC_MIN_HEQ_MAX_GAIN                 1          /* Scale is 1x    */
   #define LEP_AGC_MAX_HEQ_MAX_GAIN                 4          /* Scale is 1x    */

   #define LEP_AGC_HEQ_CLIP_LIMIT_HIGH             (80*60)     /* Scale is 1x  units: pixels  */
   #define LEP_AGC_HEQ_CLIP_LIMIT_LOW              (80*60)     /* Scale is 1x  units: pixels  */
   #define LEP_AGC_HEQ_MAX_BIN_EXTENSION            16         /* Scale is 1x  units: bins  */

   #define LEP_AGC_MIN_HEQ_MIDPOINT                127         /* Scale is 1x    */
   #define LEP_AGC_MAX_HEQ_MIDPOINT                65534       /* Scale is 1x    */

/* ROI
*/
   #define LEP_AGC_MAX_COL                         79
   #define LEP_AGC_MAX_ROW                         59
   #define LEP_AGC_MIN_COL                         0
   #define LEP_AGC_MIN_ROW                         0

/******************************************************************************/
/** EXPORTED TYPE DEFINITIONS                                                **/
/******************************************************************************/
typedef LEP_UINT16  LEP_AGC_HEQ_EMPTY_COUNT_T, *LEP_AGC_HEQ_EMPTY_COUNT_T_PTR;
typedef LEP_UINT16  LEP_AGC_HEQ_NORMALIZATION_FACTOR_T, *LEP_AGC_HEQ_NORMALIZATION_FACTOR_T_PTR;

/* AGC Enable Enum
*/
typedef enum LEP_AGC_ENABLE_TAG
{
   LEP_AGC_DISABLE = 0,
   LEP_AGC_ENABLE,
   LEP_END_AGC_ENABLE

}LEP_AGC_ENABLE_E, *LEP_AGC_ENABLE_E_PTR;

/* AGC Policy Enum
*/
typedef enum LEP_AGC_POLICY_TAG
{
   LEP_AGC_LINEAR = 0,
   LEP_AGC_HEQ,
   LEP_END_AGC_POLICY

}LEP_AGC_POLICY_E, *LEP_AGC_POLICY_E_PTR;

/* AGC ROI Structure
*/
typedef struct LEP_AGC_ROI_TAG
{
   LEP_UINT16 startCol;
   LEP_UINT16 startRow;
   LEP_UINT16 endCol;
   LEP_UINT16 endRow;

}LEP_AGC_ROI_T, *LEP_AGC_ROI_T_PTR;

/* AGC Histogram Statistics Structure
*/
typedef struct LEP_AGC_HISTOGRAM_STATISTICS_TAG
{
   LEP_UINT16  minIntensity;
   LEP_UINT16  maxIntensity;
   LEP_UINT16  meanIntensity;
   LEP_UINT16  numPixels;

}LEP_AGC_HISTOGRAM_STATISTICS_T, *LEP_AGC_HISTOGRAM_STATISTICS_T_PTR;

/* AGC Output Scale Factor Structure
*/
typedef enum LEP_AGC_SCALE_FACTOR_E_TAG
{
   LEP_AGC_SCALE_TO_8_BITS = 0,
   LEP_AGC_SCALE_TO_14_BITS,

   LEP_AGC_END_SCALE_TO
}LEP_AGC_HEQ_SCALE_FACTOR_E, *LEP_AGC_HEQ_SCALE_FACTOR_E_PTR;

/******************************************************************************/
/** EXPORTED PUBLIC DATA                                                     **/
/******************************************************************************/

/******************************************************************************/
/** EXPORTED PUBLIC FUNCTIONS                                                **/
/******************************************************************************/

/* General AGC Controls
*/

extern LEP_RESULT LEP_GetAgcEnableState( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                         LEP_AGC_ENABLE_E_PTR agcEnableStatePtr );
extern LEP_RESULT LEP_SetAgcEnableState( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                         LEP_AGC_ENABLE_E agcEnableState );

extern LEP_RESULT LEP_GetAgcPolicy( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                    LEP_AGC_POLICY_E_PTR agcPolicyPtr );
extern LEP_RESULT LEP_SetAgcPolicy( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                    LEP_AGC_POLICY_E agcPolicy );

extern LEP_RESULT LEP_GetAgcROI( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                 LEP_AGC_ROI_T_PTR agcROIPtr );
extern LEP_RESULT LEP_SetAgcROI( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                 LEP_AGC_ROI_T agcROI );

extern LEP_RESULT LEP_GetAgcHistogramStatistics( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                                 LEP_AGC_HISTOGRAM_STATISTICS_T_PTR *agcHistogramStatisticsPtr );

/* Linear AGC Policy Controls
*/
extern LEP_RESULT LEP_GetAgcLinearHistogramTailSize( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                                     LEP_UINT16 *agcLinearHistogramTailSizePtr );
extern LEP_RESULT LEP_SetAgcLinearHistogramTailSize( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                                     LEP_UINT16 agcLinearHistogramTailSize );

extern LEP_RESULT LEP_GetAgcLinearHistogramClipPercent( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                                  LEP_UINT16 *agcLinearClipPercentPtr );
extern LEP_RESULT LEP_SetAgcLinearHistogramClipPercent( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                                  LEP_UINT16 agcLinearClipPercent );

extern LEP_RESULT LEP_GetAgcLinearMaxGain( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                           LEP_UINT16 *agcLinearMaxGainPtr );
extern LEP_RESULT LEP_SetAgcLinearMaxGain( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                           LEP_UINT16 agcLinearMaxGain );

extern LEP_RESULT LEP_GetAgcLinearMidPoint( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                            LEP_UINT16 *agcLinearMidPointPtr );
extern LEP_RESULT LEP_SetAgcLinearMidPoint( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                            LEP_UINT16 agcLinearMidPoint );

extern LEP_RESULT LEP_GetAgcLinearDampeningFactor( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                                   LEP_UINT16 *agcLinearDampeningFactorPtr );
extern LEP_RESULT LEP_SetAgcLinearDampeningFactor( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                                   LEP_UINT16 agcLinearDampeningFactor );

/* Heq AGC Policy Controls
*/
extern LEP_RESULT LEP_GetAgcHeqDampingFactor( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                              LEP_UINT16 *agcHeqDampingFactorPtr );
extern LEP_RESULT LEP_SetAgcHeqDampingFactor( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                              LEP_UINT16 agcHeqDampingFactor );

extern LEP_RESULT LEP_GetAgcHeqMaxGain( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                        LEP_UINT16 *agcHeqMaxGainPtr );
extern LEP_RESULT LEP_SetAgcHeqMaxGain( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                        LEP_UINT16 agcHeqMaxGain );

extern LEP_RESULT LEP_GetAgcHeqClipLimitHigh( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                              LEP_UINT16 *agcHeqClipLimitHighPtr );
extern LEP_RESULT LEP_SetAgcHeqClipLimitHigh( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                              LEP_UINT16 agcHeqClipLimitHigh );

extern LEP_RESULT LEP_GetAgcHeqClipLimitLow( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                             LEP_UINT16 *agcHeqClipLimitLowPtr );
extern LEP_RESULT LEP_SetAgcHeqClipLimitLow( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                             LEP_UINT16 agcHeqClipLimitLow );

extern LEP_RESULT LEP_GetAgcHeqBinExtension( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                             LEP_UINT16 *agcHeqBinExtensionPtr );
extern LEP_RESULT LEP_SetAgcHeqBinExtension( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                             LEP_UINT16 agcHeqBinExtension );

extern LEP_RESULT LEP_GetAgcHeqMidPoint( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                         LEP_UINT16 *agcHeqMidPointPtr );
extern LEP_RESULT LEP_SetAgcHeqMidPoint( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                         LEP_UINT16 agcHeqMidPoint );

extern LEP_RESULT LEP_GetAgcHeqEmptyCount( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                           LEP_AGC_HEQ_EMPTY_COUNT_T_PTR emptyCountPtr );
extern LEP_RESULT LEP_SetAgcHeqEmptyCount( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                           LEP_AGC_HEQ_EMPTY_COUNT_T emptyCount );

extern LEP_RESULT LEP_GetAgcHeqNormalizationFactor( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                                    LEP_AGC_HEQ_NORMALIZATION_FACTOR_T_PTR normalizationFactorPtr );
extern LEP_RESULT LEP_SetAgcHeqNormalizationFactor( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                                    LEP_AGC_HEQ_NORMALIZATION_FACTOR_T normalizationFactor );

extern LEP_RESULT LEP_GetAgcHeqScaleFactor( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                            LEP_AGC_HEQ_SCALE_FACTOR_E_PTR scaleFactorPtr );
extern LEP_RESULT LEP_SetAgcHeqScaleFactor( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                            LEP_AGC_HEQ_SCALE_FACTOR_E scaleFactor );

extern LEP_RESULT LEP_GetAgcCalcEnableState( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                             LEP_AGC_ENABLE_E_PTR agcCalculationEnableStatePtr );

extern LEP_RESULT LEP_SetAgcCalcEnableState( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                             LEP_AGC_ENABLE_E agcCalculationEnableState );

extern LEP_RESULT LEP_GetAgcHeqLinearPercent( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                              LEP_UINT16 *agcHeqLinearPercentPtr);

extern LEP_RESULT LEP_SetAgcHeqLinearPercent( LEP_CAMERA_PORT_DESC_T_PTR portDescPtr,
                                              LEP_UINT16 agcHeqLinearPercent);

   #ifdef __cplusplus

}
   #endif

#endif  /* _LEPTON_AGC_H_ */