<commit_before>#include <stdio.h>
#include <assert.h>
#include <string.h>

#include <google/dense_hash_set>

#include <locale>
#include <list>
#include <iostream>
#include <string>

#include <re2/re2.h>

#include "smart_git.h"
#include "timer.h"

using google::dense_hash_set;
using re2::RE2;
using re2::StringPiece;
using namespace std;

#define CHUNK_SIZE (1 << 20)

struct search_file {
    string path;
    const char *ref;
    git_oid oid;
};

struct chunk_file {
    search_file *file;
    int left;
    int right;
};

#define CHUNK_MAGIC 0xC407FADE

struct chunk {
    int size;
    unsigned magic;
    vector<chunk_file> files;
    char data[0];

    chunk()
        : size(0), magic(CHUNK_MAGIC), files() {
    }

    chunk_file &get_chunk_file(search_file *sf, const char *p) {
        if (files.empty() || files.back().file != sf) {
            int off = p - data;
            files.push_back(chunk_file());
            chunk_file &cf = files.back();
            cf.file = sf;
            cf.left = cf.right = off;
        }
        return files.back();
    }
};

#define CHUNK_SPACE  (CHUNK_SIZE - (sizeof(chunk)))

chunk *alloc_chunk() {
    void *p;
    if (posix_memalign(&p, CHUNK_SIZE, CHUNK_SIZE) != 0)
        return NULL;
    return new(p) chunk;
};

class chunk_allocator {
public:
    chunk_allocator() : current_() {
        new_chunk();
    }

    char *alloc(size_t len) {
        assert(len < CHUNK_SPACE);
        if ((current_->size + len) > CHUNK_SPACE)
            new_chunk();
        char *out = current_->data + current_->size;
        current_->size += len;
        return out;
    }

    list<chunk*>::iterator begin () {
        return chunks_.begin();
    }

    typename list<chunk*>::iterator end () {
        return chunks_.end();
    }

    chunk *current_chunk() {
        return current_;
    }

protected:
    void new_chunk() {
        current_ = alloc_chunk();
        chunks_.push_back(current_);
    }

    list<chunk*> chunks_;
    chunk *current_;
};

/*
 * We special-case data() == NULL to provide an "empty" element for
 * dense_hash_set.
 *
 * StringPiece::operator== will consider a zero-length string equal to a
 * zero-length string with a NULL data().
 */
struct eqstr {
    bool operator()(const StringPiece& lhs, const StringPiece& rhs) const {
        if (lhs.data() == NULL && rhs.data() == NULL)
            return true;
        if (lhs.data() == NULL || rhs.data() == NULL)
            return false;
        return lhs == rhs;
    }
};

struct hashstr {
    locale loc;
    size_t operator()(const StringPiece &str) const {
        const collate<char> &coll = use_facet<collate<char> >(loc);
        return coll.hash(str.data(), str.data() + str.size());
    }
};

const StringPiece empty_string(NULL, 0);

typedef dense_hash_set<StringPiece, hashstr, eqstr> string_hash;

class code_counter {
public:
    code_counter(git_repository *repo)
        : repo_(repo), stats_()
    {
        lines_.set_empty_key(empty_string);
    }

    void walk_ref(const char *ref) {
        smart_object<git_commit> commit;
        smart_object<git_tree> tree;
        resolve_ref(commit, ref);
        git_commit_tree(tree, commit);

        walk_tree(ref, "", tree);
    }

    void dump_stats() {
        printf("Bytes: %ld (dedup: %ld)\n", stats_.bytes, stats_.dedup_bytes);
        printf("Lines: %ld (dedup: %ld)\n", stats_.lines, stats_.dedup_lines);
    }

    bool match(RE2& pat) {
        list<chunk*>::iterator it;
        StringPiece match;
        int matches = 0;

        for (it = alloc_.begin(); it != alloc_.end(); it++) {
            StringPiece str((*it)->data, (*it)->size);
            int pos = 0;
            while (pos < str.size()) {
                    if (!pat.Match(str, pos, str.size(), RE2::UNANCHORED, &match, 1))
                        break;
                    assert(memchr(match.data(), '\n', match.size()) == NULL);
                    StringPiece line = find_line(str, match);
                    printf("%.*s\n", line.size(), line.data());
                    print_files(line.data());
                    pos = line.size() + line.data() - str.data();
                    if (++matches == 10)
                        return true;
                }
        }
        return matches > 0;
    }
protected:
    void print_files (const char *p) {
        chunk *c = find_chunk(p);
        int off = p - c->data;
        for(vector<chunk_file>::iterator it = c->files.begin();
            it != c->files.end(); it++) {
            if (off >= it->left && off < it->right) {
                printf(" (%s:%s)\n", it->file->ref, it->file->path.c_str());
            }
        }
    }
    StringPiece find_line(const StringPiece& chunk, const StringPiece& match) {
        const char *start, *end;
        assert(match.data() >= chunk.data());
        assert(match.data() < chunk.data() + chunk.size());
        assert(match.size() < (chunk.size() - (match.data() - chunk.data())));
        start = static_cast<const char*>
            (memrchr(chunk.data(), '\n', match.data() - chunk.data()));
        if (start == NULL)
            start = chunk.data();
        else
            start++;
        end = static_cast<const char*>
            (memchr(match.data() + match.size(), '\n',
                    chunk.size() - (match.data() - chunk.data()) - match.size()));
        if (end == NULL)
            end = chunk.data() + chunk.size();
        return StringPiece(start, end - start);
    }

    void walk_tree(const char *ref, const string& pfx, git_tree *tree) {
        string path;
        int entries = git_tree_entrycount(tree);
        int i;
        for (i = 0; i < entries; i++) {
            const git_tree_entry *ent = git_tree_entry_byindex(tree, i);
            path = pfx + "/" + git_tree_entry_name(ent);
            smart_object<git_object> obj;
            git_tree_entry_2object(obj, repo_, ent);
            if (git_tree_entry_type(ent) == GIT_OBJ_TREE) {
                walk_tree(ref, path, obj);
            } else if (git_tree_entry_type(ent) == GIT_OBJ_BLOB) {
                update_stats(ref, path, obj);
            }
        }
    }

    chunk* find_chunk(const char *p) {
        chunk *out = reinterpret_cast<chunk*>
            (reinterpret_cast<uintptr_t>(p) & ~(CHUNK_SIZE - 1));
        assert(out->magic == CHUNK_MAGIC);
        return out;
    }

    void update_stats(const char *ref, const string& path, git_blob *blob) {
        size_t len = git_blob_rawsize(blob);
        const char *p = static_cast<const char*>(git_blob_rawcontent(blob));
        const char *end = p + len;
        const char *f;
        string_hash::iterator it;
        search_file *sf = new search_file;
        sf->path = path;
        sf->ref = ref;
        git_oid_cpy(&sf->oid, git_object_id(reinterpret_cast<git_object*>(blob)));
        chunk *c;
        const char *line;

        while ((f = static_cast<const char*>(memchr(p, '\n', end - p))) != 0) {
            it = lines_.find(StringPiece(p, f - p));
            if (it == lines_.end()) {
                stats_.dedup_bytes += (f - p) + 1;
                stats_.dedup_lines ++;

                // Include the trailing '\n' in the chunk buffer
                char *alloc = alloc_.alloc(f - p + 1);
                memcpy(alloc, p, f - p + 1);
                lines_.insert(StringPiece(alloc, f - p));
                c = alloc_.current_chunk();
                line = alloc;
            } else {
                line = it->data();
                c = find_chunk(line);
            }
            chunk_file &cf = c->get_chunk_file(sf, line);
            cf.left = min(static_cast<long>(cf.left), p - c->data);
            cf.right = max(static_cast<long>(cf.right), f - c->data);
            p = f + 1;
            stats_.lines++;
        }

        stats_.bytes += len;
    }

    void resolve_ref(smart_object<git_commit> &out, const char *refname) {
        git_reference *ref;
        const git_oid *oid;
        git_oid tmp;
        smart_object<git_object> obj;
        if (git_oid_fromstr(&tmp, refname) == GIT_SUCCESS) {
            git_object_lookup(obj, repo_, &tmp, GIT_OBJ_ANY);
        } else {
            git_reference_lookup(&ref, repo_, refname);
            git_reference_resolve(&ref, ref);
            oid = git_reference_oid(ref);
            git_object_lookup(obj, repo_, oid, GIT_OBJ_ANY);
        }
        if (git_object_type(obj) == GIT_OBJ_TAG) {
            git_tag_target(out, obj);
        } else {
            out = obj.release();
        }
    }

    git_repository *repo_;
    string_hash lines_;
    struct {
        unsigned long bytes, dedup_bytes;
        unsigned long lines, dedup_lines;
    } stats_;
    chunk_allocator alloc_;
};

int main(int argc, char **argv) {
    git_repository *repo;
    git_repository_open(&repo, ".git");

    code_counter counter(repo);

    for (int i = 1; i < argc; i++) {
        timer tm;
        struct timeval elapsed;
        printf("Walking %s...", argv[i]);
        fflush(stdout);
        counter.walk_ref(argv[i]);
        elapsed = tm.elapsed();
        printf(" done in %d.%06ds\n",
               (int)elapsed.tv_sec, (int)elapsed.tv_usec);
    }
    counter.dump_stats();
    RE2::Options opts;
    opts.set_never_nl(true);
    opts.set_one_line(false);
    opts.set_posix_syntax(true);
    while (true) {
        printf("regex> ");
        string line;
        getline(cin, line);
        if (cin.eof())
            break;
        RE2 re(line, opts);
        if (re.ok()) {
            timer tm;
            struct timeval elapsed;
            if (!counter.match(re)) {
                printf("no match\n");
            }
            elapsed = tm.elapsed();
            printf("Match completed in %d.%06ds.\n",
                   (int)elapsed.tv_sec, (int)elapsed.tv_usec);
        }
    }

    return 0;
}
<commit_msg>Fix the tracking of 'left' and 'right' indices in chunk_file.<commit_after>#include <stdio.h>
#include <assert.h>
#include <string.h>

#include <google/dense_hash_set>

#include <locale>
#include <list>
#include <iostream>
#include <string>

#include <re2/re2.h>

#include "smart_git.h"
#include "timer.h"

using google::dense_hash_set;
using re2::RE2;
using re2::StringPiece;
using namespace std;

#define CHUNK_SIZE (1 << 20)

struct search_file {
    string path;
    const char *ref;
    git_oid oid;
};

struct chunk_file {
    search_file *file;
    unsigned int left;
    unsigned int right;
};

#define CHUNK_MAGIC 0xC407FADE

struct chunk {
    int size;
    unsigned magic;
    vector<chunk_file> files;
    char data[0];

    chunk()
        : size(0), magic(CHUNK_MAGIC), files() {
    }

    chunk_file &get_chunk_file(search_file *sf, const char *p) {
        if (files.empty() || files.back().file != sf) {
            int off = p - data;
            files.push_back(chunk_file());
            chunk_file &cf = files.back();
            cf.file = sf;
            cf.left = cf.right = off;
        }
        return files.back();
    }
};

#define CHUNK_SPACE  (CHUNK_SIZE - (sizeof(chunk)))

chunk *alloc_chunk() {
    void *p;
    if (posix_memalign(&p, CHUNK_SIZE, CHUNK_SIZE) != 0)
        return NULL;
    return new(p) chunk;
};

class chunk_allocator {
public:
    chunk_allocator() : current_() {
        new_chunk();
    }

    char *alloc(size_t len) {
        assert(len < CHUNK_SPACE);
        if ((current_->size + len) > CHUNK_SPACE)
            new_chunk();
        char *out = current_->data + current_->size;
        current_->size += len;
        return out;
    }

    list<chunk*>::iterator begin () {
        return chunks_.begin();
    }

    typename list<chunk*>::iterator end () {
        return chunks_.end();
    }

    chunk *current_chunk() {
        return current_;
    }

protected:
    void new_chunk() {
        current_ = alloc_chunk();
        chunks_.push_back(current_);
    }

    list<chunk*> chunks_;
    chunk *current_;
};

/*
 * We special-case data() == NULL to provide an "empty" element for
 * dense_hash_set.
 *
 * StringPiece::operator== will consider a zero-length string equal to a
 * zero-length string with a NULL data().
 */
struct eqstr {
    bool operator()(const StringPiece& lhs, const StringPiece& rhs) const {
        if (lhs.data() == NULL && rhs.data() == NULL)
            return true;
        if (lhs.data() == NULL || rhs.data() == NULL)
            return false;
        return lhs == rhs;
    }
};

struct hashstr {
    locale loc;
    size_t operator()(const StringPiece &str) const {
        const collate<char> &coll = use_facet<collate<char> >(loc);
        return coll.hash(str.data(), str.data() + str.size());
    }
};

const StringPiece empty_string(NULL, 0);

typedef dense_hash_set<StringPiece, hashstr, eqstr> string_hash;

class code_counter {
public:
    code_counter(git_repository *repo)
        : repo_(repo), stats_()
    {
        lines_.set_empty_key(empty_string);
    }

    void walk_ref(const char *ref) {
        smart_object<git_commit> commit;
        smart_object<git_tree> tree;
        resolve_ref(commit, ref);
        git_commit_tree(tree, commit);

        walk_tree(ref, "", tree);
    }

    void dump_stats() {
        printf("Bytes: %ld (dedup: %ld)\n", stats_.bytes, stats_.dedup_bytes);
        printf("Lines: %ld (dedup: %ld)\n", stats_.lines, stats_.dedup_lines);
    }

    bool match(RE2& pat) {
        list<chunk*>::iterator it;
        StringPiece match;
        int matches = 0;

        for (it = alloc_.begin(); it != alloc_.end(); it++) {
            StringPiece str((*it)->data, (*it)->size);
            int pos = 0;
            while (pos < str.size()) {
                    if (!pat.Match(str, pos, str.size(), RE2::UNANCHORED, &match, 1))
                        break;
                    assert(memchr(match.data(), '\n', match.size()) == NULL);
                    StringPiece line = find_line(str, match);
                    printf("%.*s\n", line.size(), line.data());
                    print_files(line.data());
                    pos = line.size() + line.data() - str.data();
                    if (++matches == 10)
                        return true;
                }
        }
        return matches > 0;
    }
protected:
    void print_files (const char *p) {
        chunk *c = find_chunk(p);
        int off = p - c->data;
        for(vector<chunk_file>::iterator it = c->files.begin();
            it != c->files.end(); it++) {
            if (off >= it->left && off < it->right) {
                printf(" (%s:%s)\n", it->file->ref, it->file->path.c_str());
            }
        }
    }
    StringPiece find_line(const StringPiece& chunk, const StringPiece& match) {
        const char *start, *end;
        assert(match.data() >= chunk.data());
        assert(match.data() < chunk.data() + chunk.size());
        assert(match.size() < (chunk.size() - (match.data() - chunk.data())));
        start = static_cast<const char*>
            (memrchr(chunk.data(), '\n', match.data() - chunk.data()));
        if (start == NULL)
            start = chunk.data();
        else
            start++;
        end = static_cast<const char*>
            (memchr(match.data() + match.size(), '\n',
                    chunk.size() - (match.data() - chunk.data()) - match.size()));
        if (end == NULL)
            end = chunk.data() + chunk.size();
        return StringPiece(start, end - start);
    }

    void walk_tree(const char *ref, const string& pfx, git_tree *tree) {
        string path;
        int entries = git_tree_entrycount(tree);
        int i;
        for (i = 0; i < entries; i++) {
            const git_tree_entry *ent = git_tree_entry_byindex(tree, i);
            path = pfx + "/" + git_tree_entry_name(ent);
            smart_object<git_object> obj;
            git_tree_entry_2object(obj, repo_, ent);
            if (git_tree_entry_type(ent) == GIT_OBJ_TREE) {
                walk_tree(ref, path, obj);
            } else if (git_tree_entry_type(ent) == GIT_OBJ_BLOB) {
                update_stats(ref, path, obj);
            }
        }
    }

    chunk* find_chunk(const char *p) {
        chunk *out = reinterpret_cast<chunk*>
            (reinterpret_cast<uintptr_t>(p) & ~(CHUNK_SIZE - 1));
        assert(out->magic == CHUNK_MAGIC);
        return out;
    }

    void update_stats(const char *ref, const string& path, git_blob *blob) {
        size_t len = git_blob_rawsize(blob);
        const char *p = static_cast<const char*>(git_blob_rawcontent(blob));
        const char *end = p + len;
        const char *f;
        string_hash::iterator it;
        search_file *sf = new search_file;
        sf->path = path;
        sf->ref = ref;
        git_oid_cpy(&sf->oid, git_object_id(reinterpret_cast<git_object*>(blob)));
        chunk *c;
        const char *line;

        while ((f = static_cast<const char*>(memchr(p, '\n', end - p))) != 0) {
            it = lines_.find(StringPiece(p, f - p));
            if (it == lines_.end()) {
                stats_.dedup_bytes += (f - p) + 1;
                stats_.dedup_lines ++;

                // Include the trailing '\n' in the chunk buffer
                char *alloc = alloc_.alloc(f - p + 1);
                memcpy(alloc, p, f - p + 1);
                lines_.insert(StringPiece(alloc, f - p));
                c = alloc_.current_chunk();
                line = alloc;
            } else {
                line = it->data();
                c = find_chunk(line);
            }
            chunk_file &cf = c->get_chunk_file(sf, line);
            cf.left = min(static_cast<long>(cf.left), p - blob_data);
            cf.right = max(static_cast<long>(cf.right), f - blob_data);
            assert(cf.left < CHUNK_SPACE);
            assert(cf.right < CHUNK_SPACE);
            p = f + 1;
            stats_.lines++;
        }

        stats_.bytes += len;
    }

    void resolve_ref(smart_object<git_commit> &out, const char *refname) {
        git_reference *ref;
        const git_oid *oid;
        git_oid tmp;
        smart_object<git_object> obj;
        if (git_oid_fromstr(&tmp, refname) == GIT_SUCCESS) {
            git_object_lookup(obj, repo_, &tmp, GIT_OBJ_ANY);
        } else {
            git_reference_lookup(&ref, repo_, refname);
            git_reference_resolve(&ref, ref);
            oid = git_reference_oid(ref);
            git_object_lookup(obj, repo_, oid, GIT_OBJ_ANY);
        }
        if (git_object_type(obj) == GIT_OBJ_TAG) {
            git_tag_target(out, obj);
        } else {
            out = obj.release();
        }
    }

    git_repository *repo_;
    string_hash lines_;
    struct {
        unsigned long bytes, dedup_bytes;
        unsigned long lines, dedup_lines;
    } stats_;
    chunk_allocator alloc_;
};

int main(int argc, char **argv) {
    git_repository *repo;
    git_repository_open(&repo, ".git");

    code_counter counter(repo);

    for (int i = 1; i < argc; i++) {
        timer tm;
        struct timeval elapsed;
        printf("Walking %s...", argv[i]);
        fflush(stdout);
        counter.walk_ref(argv[i]);
        elapsed = tm.elapsed();
        printf(" done in %d.%06ds\n",
               (int)elapsed.tv_sec, (int)elapsed.tv_usec);
    }
    counter.dump_stats();
    RE2::Options opts;
    opts.set_never_nl(true);
    opts.set_one_line(false);
    opts.set_posix_syntax(true);
    while (true) {
        printf("regex> ");
        string line;
        getline(cin, line);
        if (cin.eof())
            break;
        RE2 re(line, opts);
        if (re.ok()) {
            timer tm;
            struct timeval elapsed;
            if (!counter.match(re)) {
                printf("no match\n");
            }
            elapsed = tm.elapsed();
            printf("Match completed in %d.%06ds.\n",
                   (int)elapsed.tv_sec, (int)elapsed.tv_usec);
        }
    }

    return 0;
}
<|endoftext|>


<commit_before>/*
* Startup Self Tests
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/selftest.h>
#include <botan/filters.h>
#include <botan/def_eng.h>
#include <botan/stl_util.h>

namespace Botan {

namespace {

/*
* Perform a Known Answer Test
*/
bool test_filter_kat(Filter* filter,
                     const std::string& input,
                     const std::string& output)
   {
   Pipe pipe(new Hex_Decoder, filter, new Hex_Encoder);
   pipe.process_msg(input);

   return (output == pipe.read_all_as_string());
   }

}

/*
* Run a set of KATs
*/
std::map<std::string, bool>
algorithm_kat(const SCAN_Name& algo_name,
              const std::map<std::string, std::string>& vars,
              Algorithm_Factory& af)
   {
   const std::string& algo = algo_name.algo_name_and_args();

   std::vector<std::string> providers = af.providers_of(algo);
   std::map<std::string, bool> all_results;

   if(providers.empty()) // no providers, nothing to do
      return all_results;

   const std::string input = search_map(vars, std::string("input"));
   const std::string output = search_map(vars, std::string("output"));
   const std::string key = search_map(vars, std::string("key"));
   const std::string iv = search_map(vars, std::string("iv"));

   for(u32bit i = 0; i != providers.size(); ++i)
      {
      const std::string provider = providers[i];

      if(const HashFunction* proto =
            af.prototype_hash_function(algo, provider))
         {
         Filter* filt = new Hash_Filter(proto->clone());
         all_results[provider] = test_filter_kat(filt, input, output);
         }
      else if(const MessageAuthenticationCode* proto =
                 af.prototype_mac(algo, provider))
         {
         Keyed_Filter* filt = new MAC_Filter(proto->clone(), key);
         all_results[provider] = test_filter_kat(filt, input, output);
         }
      else if(const StreamCipher* proto =
                 af.prototype_stream_cipher(algo, provider))
         {
         Keyed_Filter* filt = new StreamCipher_Filter(proto->clone());
         filt->set_key(key);
         filt->set_iv(iv);

         all_results[provider] = test_filter_kat(filt, input, output);
         }
      else if(const BlockCipher* proto =
                 af.prototype_block_cipher(algo, provider))
         {
         Keyed_Filter* enc = get_cipher_mode(proto, ENCRYPTION,
                                             algo_name.cipher_mode(),
                                             algo_name.cipher_mode_pad());

         Keyed_Filter* dec = get_cipher_mode(proto, DECRYPTION,
                                             algo_name.cipher_mode(),
                                             algo_name.cipher_mode_pad());

         if(!enc || !dec)
            {
            delete enc;
            delete dec;
            continue;
            }

         enc->set_key(key);
         enc->set_iv(iv);

         dec->set_key(key);
         dec->set_iv(iv);

         bool enc_ok = test_filter_kat(enc, input, output);
         bool dec_ok = test_filter_kat(dec, output, input);

         all_results[provider] = enc_ok && dec_ok;
         }
      }

   return all_results;
   }

namespace {

void do_kat(const std::string& in, const std::string& out,
            const std::string& algo_name, Filter* filter)
   {
   if(!test_filter_kat(filter, in, out))
      throw Self_Test_Failure(algo_name + " startup test");
   }

void verify_results(const std::string& algo,
                    const std::map<std::string, bool>& results)
   {
   for(std::map<std::string, bool>::const_iterator i = results.begin();
       i != results.end(); ++i)
      {
      if(!i->second)
         throw Self_Test_Failure(algo + " self-test failed, provider "+
                                 i->first);
      }
   }

void hash_test(Algorithm_Factory& af,
               const std::string& name,
               const std::string& in,
               const std::string& out)
   {
   std::map<std::string, std::string> vars;
   vars["input"] = in;
   vars["output"] = out;

   verify_results(name, algorithm_kat(name, vars, af));
   }

void mac_test(Algorithm_Factory& af,
              const std::string& name,
              const std::string& in,
              const std::string& out,
              const std::string& key)
   {
   std::map<std::string, std::string> vars;
   vars["input"] = in;
   vars["output"] = out;
   vars["key"] = key;

   verify_results(name, algorithm_kat(name, vars, af));
   }

/*
* Perform a KAT for a cipher
*/
void cipher_kat(Algorithm_Factory& af,
                const std::string& algo,
                const std::string& key_str,
                const std::string& iv_str,
                const std::string& in,
                const std::string& ecb_out,
                const std::string& cbc_out,
                const std::string& cfb_out,
                const std::string& ofb_out,
                const std::string& ctr_out)
   {
   SymmetricKey key(key_str);
   InitializationVector iv(iv_str);

   std::map<std::string, std::string> vars;
   vars["key"] = key_str;
   vars["iv"] = iv_str;
   vars["input"] = in;

   std::map<std::string, bool> results;

   vars["output"] = ecb_out;
   verify_results(algo + "/ECB", algorithm_kat(algo + "/ECB", vars, af));

   vars["output"] = cbc_out;
   verify_results(algo + "/CBC",
                  algorithm_kat(algo + "/CBC/NoPadding", vars, af));

   vars["output"] = cfb_out;
   verify_results(algo + "/CFB", algorithm_kat(algo + "/CFB", vars, af));

   vars["output"] = ofb_out;
   verify_results(algo + "/OFB", algorithm_kat(algo + "/OFB", vars, af));

   vars["output"] = ctr_out;
   verify_results(algo + "/CTR", algorithm_kat(algo + "/CTR-BE", vars, af));
   }

}

/*
* Perform Self Tests
*/
bool passes_self_tests(Algorithm_Factory& af)
  {
  try
     {
     cipher_kat(af, "DES",
                "0123456789ABCDEF", "1234567890ABCDEF",
                "4E6F77206973207468652074696D6520666F7220616C6C20",
                "3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53",
                "E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6",
                "F3096249C7F46E51A69E839B1A92F78403467133898EA622",
                "F3096249C7F46E5135F24A242EEB3D3F3D6D5BE3255AF8C3",
                "F3096249C7F46E51163A8CA0FFC94C27FA2F80F480B86F75");

     cipher_kat(af, "TripleDES",
                "385D7189A5C3D485E1370AA5D408082B5CCCCB5E19F2D90E",
                "C141B5FCCD28DC8A",
                "6E1BD7C6120947A464A6AAB293A0F89A563D8D40D3461B68",
                "64EAAD4ACBB9CEAD6C7615E7C7E4792FE587D91F20C7D2F4",
                "6235A461AFD312973E3B4F7AA7D23E34E03371F8E8C376C9",
                "E26BA806A59B0330DE40CA38E77A3E494BE2B212F6DD624B",
                "E26BA806A59B03307DE2BCC25A08BA40A8BA335F5D604C62",
                "E26BA806A59B03303C62C2EFF32D3ACDD5D5F35EBCC53371");

     cipher_kat(af, "AES-128",
                "2B7E151628AED2A6ABF7158809CF4F3C",
                "000102030405060708090A0B0C0D0E0F",
                "6BC1BEE22E409F96E93D7E117393172A"
                "AE2D8A571E03AC9C9EB76FAC45AF8E51",
                "3AD77BB40D7A3660A89ECAF32466EF97"
                "F5D3D58503B9699DE785895A96FDBAAF",
                "7649ABAC8119B246CEE98E9B12E9197D"
                "5086CB9B507219EE95DB113A917678B2",
                "3B3FD92EB72DAD20333449F8E83CFB4A"
                "C8A64537A0B3A93FCDE3CDAD9F1CE58B",
                "3B3FD92EB72DAD20333449F8E83CFB4A"
                "7789508D16918F03F53C52DAC54ED825",
                "3B3FD92EB72DAD20333449F8E83CFB4A"
                "010C041999E03F36448624483E582D0E");

     hash_test(af, "SHA-1",
               "", "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709");

     hash_test(af, "SHA-1",
               "616263", "A9993E364706816ABA3E25717850C26C9CD0D89D");

     hash_test(af, "SHA-1",
               "6162636462636465636465666465666765666768666768696768696A"
               "68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F7071",
               "84983E441C3BD26EBAAE4AA1F95129E5E54670F1");

     mac_test(af, "HMAC(SHA-1)",
              "4869205468657265",
              "B617318655057264E28BC0B6FB378C8EF146BE00",
              "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B");

     hash_test(af, "SHA-256",
               "",
               "E3B0C44298FC1C149AFBF4C8996FB924"
               "27AE41E4649B934CA495991B7852B855");

     hash_test(af, "SHA-256",
               "616263",
               "BA7816BF8F01CFEA414140DE5DAE2223"
               "B00361A396177A9CB410FF61F20015AD");

     hash_test(af, "SHA-256",
               "6162636462636465636465666465666765666768666768696768696A"
               "68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F7071",
               "248D6A61D20638B8E5C026930C3E6039"
               "A33CE45964FF2167F6ECEDD419DB06C1");

     mac_test(af, "HMAC(SHA-256)",
              "4869205468657265",
              "198A607EB44BFBC69903A0F1CF2BBDC5"
              "BA0AA3F3D9AE3C1C7A3B1696A0B68CF7",
              "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B"
              "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B");
  }
  catch(Self_Test_Failure)
     {
     return false;
     }

  return true;
  }

}
<commit_msg>Remove a dead function<commit_after>/*
* Startup Self Tests
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/selftest.h>
#include <botan/filters.h>
#include <botan/def_eng.h>
#include <botan/stl_util.h>

namespace Botan {

namespace {

/*
* Perform a Known Answer Test
*/
bool test_filter_kat(Filter* filter,
                     const std::string& input,
                     const std::string& output)
   {
   Pipe pipe(new Hex_Decoder, filter, new Hex_Encoder);
   pipe.process_msg(input);

   return (output == pipe.read_all_as_string());
   }

}

/*
* Run a set of KATs
*/
std::map<std::string, bool>
algorithm_kat(const SCAN_Name& algo_name,
              const std::map<std::string, std::string>& vars,
              Algorithm_Factory& af)
   {
   const std::string& algo = algo_name.algo_name_and_args();

   std::vector<std::string> providers = af.providers_of(algo);
   std::map<std::string, bool> all_results;

   if(providers.empty()) // no providers, nothing to do
      return all_results;

   const std::string input = search_map(vars, std::string("input"));
   const std::string output = search_map(vars, std::string("output"));
   const std::string key = search_map(vars, std::string("key"));
   const std::string iv = search_map(vars, std::string("iv"));

   for(u32bit i = 0; i != providers.size(); ++i)
      {
      const std::string provider = providers[i];

      if(const HashFunction* proto =
            af.prototype_hash_function(algo, provider))
         {
         Filter* filt = new Hash_Filter(proto->clone());
         all_results[provider] = test_filter_kat(filt, input, output);
         }
      else if(const MessageAuthenticationCode* proto =
                 af.prototype_mac(algo, provider))
         {
         Keyed_Filter* filt = new MAC_Filter(proto->clone(), key);
         all_results[provider] = test_filter_kat(filt, input, output);
         }
      else if(const StreamCipher* proto =
                 af.prototype_stream_cipher(algo, provider))
         {
         Keyed_Filter* filt = new StreamCipher_Filter(proto->clone());
         filt->set_key(key);
         filt->set_iv(iv);

         all_results[provider] = test_filter_kat(filt, input, output);
         }
      else if(const BlockCipher* proto =
                 af.prototype_block_cipher(algo, provider))
         {
         Keyed_Filter* enc = get_cipher_mode(proto, ENCRYPTION,
                                             algo_name.cipher_mode(),
                                             algo_name.cipher_mode_pad());

         Keyed_Filter* dec = get_cipher_mode(proto, DECRYPTION,
                                             algo_name.cipher_mode(),
                                             algo_name.cipher_mode_pad());

         if(!enc || !dec)
            {
            delete enc;
            delete dec;
            continue;
            }

         enc->set_key(key);
         enc->set_iv(iv);

         dec->set_key(key);
         dec->set_iv(iv);

         bool enc_ok = test_filter_kat(enc, input, output);
         bool dec_ok = test_filter_kat(dec, output, input);

         all_results[provider] = enc_ok && dec_ok;
         }
      }

   return all_results;
   }

namespace {

void verify_results(const std::string& algo,
                    const std::map<std::string, bool>& results)
   {
   for(std::map<std::string, bool>::const_iterator i = results.begin();
       i != results.end(); ++i)
      {
      if(!i->second)
         throw Self_Test_Failure(algo + " self-test failed, provider "+
                                 i->first);
      }
   }

void hash_test(Algorithm_Factory& af,
               const std::string& name,
               const std::string& in,
               const std::string& out)
   {
   std::map<std::string, std::string> vars;
   vars["input"] = in;
   vars["output"] = out;

   verify_results(name, algorithm_kat(name, vars, af));
   }

void mac_test(Algorithm_Factory& af,
              const std::string& name,
              const std::string& in,
              const std::string& out,
              const std::string& key)
   {
   std::map<std::string, std::string> vars;
   vars["input"] = in;
   vars["output"] = out;
   vars["key"] = key;

   verify_results(name, algorithm_kat(name, vars, af));
   }

/*
* Perform a KAT for a cipher
*/
void cipher_kat(Algorithm_Factory& af,
                const std::string& algo,
                const std::string& key_str,
                const std::string& iv_str,
                const std::string& in,
                const std::string& ecb_out,
                const std::string& cbc_out,
                const std::string& cfb_out,
                const std::string& ofb_out,
                const std::string& ctr_out)
   {
   SymmetricKey key(key_str);
   InitializationVector iv(iv_str);

   std::map<std::string, std::string> vars;
   vars["key"] = key_str;
   vars["iv"] = iv_str;
   vars["input"] = in;

   std::map<std::string, bool> results;

   vars["output"] = ecb_out;
   verify_results(algo + "/ECB", algorithm_kat(algo + "/ECB", vars, af));

   vars["output"] = cbc_out;
   verify_results(algo + "/CBC",
                  algorithm_kat(algo + "/CBC/NoPadding", vars, af));

   vars["output"] = cfb_out;
   verify_results(algo + "/CFB", algorithm_kat(algo + "/CFB", vars, af));

   vars["output"] = ofb_out;
   verify_results(algo + "/OFB", algorithm_kat(algo + "/OFB", vars, af));

   vars["output"] = ctr_out;
   verify_results(algo + "/CTR", algorithm_kat(algo + "/CTR-BE", vars, af));
   }

}

/*
* Perform Self Tests
*/
bool passes_self_tests(Algorithm_Factory& af)
  {
  try
     {
     cipher_kat(af, "DES",
                "0123456789ABCDEF", "1234567890ABCDEF",
                "4E6F77206973207468652074696D6520666F7220616C6C20",
                "3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53",
                "E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6",
                "F3096249C7F46E51A69E839B1A92F78403467133898EA622",
                "F3096249C7F46E5135F24A242EEB3D3F3D6D5BE3255AF8C3",
                "F3096249C7F46E51163A8CA0FFC94C27FA2F80F480B86F75");

     cipher_kat(af, "TripleDES",
                "385D7189A5C3D485E1370AA5D408082B5CCCCB5E19F2D90E",
                "C141B5FCCD28DC8A",
                "6E1BD7C6120947A464A6AAB293A0F89A563D8D40D3461B68",
                "64EAAD4ACBB9CEAD6C7615E7C7E4792FE587D91F20C7D2F4",
                "6235A461AFD312973E3B4F7AA7D23E34E03371F8E8C376C9",
                "E26BA806A59B0330DE40CA38E77A3E494BE2B212F6DD624B",
                "E26BA806A59B03307DE2BCC25A08BA40A8BA335F5D604C62",
                "E26BA806A59B03303C62C2EFF32D3ACDD5D5F35EBCC53371");

     cipher_kat(af, "AES-128",
                "2B7E151628AED2A6ABF7158809CF4F3C",
                "000102030405060708090A0B0C0D0E0F",
                "6BC1BEE22E409F96E93D7E117393172A"
                "AE2D8A571E03AC9C9EB76FAC45AF8E51",
                "3AD77BB40D7A3660A89ECAF32466EF97"
                "F5D3D58503B9699DE785895A96FDBAAF",
                "7649ABAC8119B246CEE98E9B12E9197D"
                "5086CB9B507219EE95DB113A917678B2",
                "3B3FD92EB72DAD20333449F8E83CFB4A"
                "C8A64537A0B3A93FCDE3CDAD9F1CE58B",
                "3B3FD92EB72DAD20333449F8E83CFB4A"
                "7789508D16918F03F53C52DAC54ED825",
                "3B3FD92EB72DAD20333449F8E83CFB4A"
                "010C041999E03F36448624483E582D0E");

     hash_test(af, "SHA-1",
               "", "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709");

     hash_test(af, "SHA-1",
               "616263", "A9993E364706816ABA3E25717850C26C9CD0D89D");

     hash_test(af, "SHA-1",
               "6162636462636465636465666465666765666768666768696768696A"
               "68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F7071",
               "84983E441C3BD26EBAAE4AA1F95129E5E54670F1");

     mac_test(af, "HMAC(SHA-1)",
              "4869205468657265",
              "B617318655057264E28BC0B6FB378C8EF146BE00",
              "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B");

     hash_test(af, "SHA-256",
               "",
               "E3B0C44298FC1C149AFBF4C8996FB924"
               "27AE41E4649B934CA495991B7852B855");

     hash_test(af, "SHA-256",
               "616263",
               "BA7816BF8F01CFEA414140DE5DAE2223"
               "B00361A396177A9CB410FF61F20015AD");

     hash_test(af, "SHA-256",
               "6162636462636465636465666465666765666768666768696768696A"
               "68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F7071",
               "248D6A61D20638B8E5C026930C3E6039"
               "A33CE45964FF2167F6ECEDD419DB06C1");

     mac_test(af, "HMAC(SHA-256)",
              "4869205468657265",
              "198A607EB44BFBC69903A0F1CF2BBDC5"
              "BA0AA3F3D9AE3C1C7A3B1696A0B68CF7",
              "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B"
              "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B");
  }
  catch(Self_Test_Failure)
     {
     return false;
     }

  return true;
  }

}
<|endoftext|>


<commit_before>#include "Halide.h"
#include <stdio.h>

#include "test/common/gpu_object_lifetime_tracker.h"

using namespace Halide;

Internal::GpuObjectLifetimeTracker tracker;

void halide_print(void *user_context, const char *str) {
    printf("%s", str);

    tracker.record_gpu_debug(str);
}

int main(int argc, char **argv) {

    Internal::JITHandlers handlers;
    handlers.custom_print = halide_print;
    Internal::JITSharedRuntime::set_default_handlers(handlers);

    Target target = get_jit_target_from_environment();

    // We need debug output to record object creation.
    target.set_feature(Target::Debug);

    {
        Halide::Buffer<float> buf(100, 100);

        {
            // Make a shallow copy of the original buf, and trigger a gpu copy of it.
            Halide::Buffer<float> copy = buf;
            Func f;
            Var x, y;
            f(x, y) = copy(x, y);

            if (target.has_gpu_feature()) {
                f.gpu_tile(x, y, 8, 8);
            } else if (target.features_any_of({Target::HVX_64, Target::HVX_128})) {
                f.hexagon();
            }

            f.set_custom_print(halide_print);
            f.realize(50, 50, target);

            // The copy now has a non-zero dev field, but the original
            // buf is unaware of that fact. It should get cleaned up
            // here.
            assert(copy.has_device_allocation());
        }

        Halide::Internal::JITSharedRuntime::release_all();

        assert(!buf.has_device_allocation());

        // At this point, the device allocation should have been cleaned up, even though the original buffer still lives.
        if (tracker.validate_gpu_object_lifetime(true /* allow_globals */,
                                                 true /* allow_none */,
                                                 1 /* max_globals */)) {
            return -1;
        }
    }

    printf("Success!\n");
    return 0;
}
<commit_msg>Fix faulty assert<commit_after>#include "Halide.h"
#include <stdio.h>

#include "test/common/gpu_object_lifetime_tracker.h"

using namespace Halide;

Internal::GpuObjectLifetimeTracker tracker;

void halide_print(void *user_context, const char *str) {
    printf("%s", str);

    tracker.record_gpu_debug(str);
}

int main(int argc, char **argv) {

    Internal::JITHandlers handlers;
    handlers.custom_print = halide_print;
    Internal::JITSharedRuntime::set_default_handlers(handlers);

    Target target = get_jit_target_from_environment();

    // We need debug output to record object creation.
    target.set_feature(Target::Debug);

    {
        Halide::Buffer<float> buf(100, 100);

        {
            // Make a shallow copy of the original buf, and trigger a gpu copy of it.
            Halide::Buffer<float> copy = buf;
            Func f;
            Var x, y;
            f(x, y) = copy(x, y);

            if (target.has_gpu_feature()) {
                f.gpu_tile(x, y, 8, 8);
            } else if (target.features_any_of({Target::HVX_64, Target::HVX_128})) {
                f.hexagon();
            }

            f.set_custom_print(halide_print);
            f.realize(50, 50, target);

            // The copy now has a non-zero dev field, but the original
            // buf is unaware of that fact. It should get cleaned up
            // here.
            if (target.has_gpu_feature()) {
                assert(copy.has_device_allocation());
            }
        }

        Halide::Internal::JITSharedRuntime::release_all();

        assert(!buf.has_device_allocation());

        // At this point, the device allocation should have been cleaned up, even though the original buffer still lives.
        if (tracker.validate_gpu_object_lifetime(true /* allow_globals */,
                                                 true /* allow_none */,
                                                 1 /* max_globals */)) {
            return -1;
        }
    }

    printf("Success!\n");
    return 0;
}
<|endoftext|>


<commit_before>#include <Halide.h>
#include <stdio.h>
#include <math.h>

using namespace Halide;

// A version of pow that tracks usage so we can check how many times it was called.
#ifdef _MSC_VER
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
#endif

int call_count;
extern "C" DLLEXPORT float my_powf(float x, float y) {
    call_count++;
    return powf(x, y);
}
HalideExtern_2(float, my_powf, float, float);

int main(int argc, char **argv) {
    // Brighten some tiles of an image, where that region is given by
    // a lower-res bitmap.

    ImageParam bitmap(Bool(), 2);
    ImageParam image(Float(32), 2);
    const int tile_size = 16;

    Var x("x"), y("y"), xi("xi"), yi("yi"), t("t");

    // Brighten the image
    Func brighter("brighter");
    brighter(x, y) = my_powf(image(x, y), 0.8f);

    // Select either the brighter or the input depending on the bitmap
    Func output("output");
    output(x, y) = select(bitmap(x/tile_size, y/tile_size), brighter(x, y), image(x, y));

    // Compute the output in tiles of the appropriate size
    output.tile(x, y, xi, yi, tile_size, tile_size);

    // Vectorize within tiles, parallelize across tiles.
    output.vectorize(xi, 4).fuse(x, y, t).parallel(t);

    // Compute brighter per output tile
    brighter.compute_at(output, t);

    // Assert that the output is a whole number of tiles. Otherwise
    // the tiles in the schedule don't match up to the tiles in the
    // algorithm, and you can't safely skip any work.
    output.bound(x, 0, (image.extent(0)/tile_size)*tile_size);
    output.bound(y, 0, (image.extent(1)/tile_size)*tile_size);
    output.compile_jit();

    Image<bool> bitmap_buf(10, 10);
    bitmap_buf(5, 5) = 1;
    bitmap.set(bitmap_buf);

    Image<float> image_buf = lambda(x, y, (sin(x+y)+1)/2).realize(10 * tile_size, 10 * tile_size);
    image.set(image_buf);

    call_count = 0;
    Image<float> result = output.realize(10 * tile_size, 10 * tile_size);

    // Check the right number of calls to powf occurred
    if (call_count != tile_size*tile_size) {
        printf("call_count = %d instead of %d\n", call_count, tile_size * tile_size);
        return -1;
    }

    // Check the output is correct
    for (int y = 0; y < result.height(); y++) {
        for (int x = 0; x < result.width(); x++) {
            bool active = bitmap_buf(x/tile_size, y/tile_size);
            float correct = active ? my_powf(image_buf(x, y), 0.8f) : image_buf(x, y);
            if (fabs(correct - result(x, y)) > 0.001f) {
                printf("result(%d, %d) = %f instead of %f\n",
                       x, y, result(x, y), correct);
                return -1;
            }
        }
    }

    printf("Success!\n");
    return 0;
}
<commit_msg>Fix race condition in process_some_tiles<commit_after>#include <Halide.h>
#include <stdio.h>
#include <math.h>

using namespace Halide;

// A version of pow that tracks usage so we can check how many times it was called.
#ifdef _MSC_VER
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
#endif

int call_count;
extern "C" DLLEXPORT float my_powf(float x, float y) {
    call_count++;
    return powf(x, y);
}
HalideExtern_2(float, my_powf, float, float);

int main(int argc, char **argv) {
    // Brighten some tiles of an image, where that region is given by
    // a lower-res bitmap.

    ImageParam bitmap(Bool(), 2);
    ImageParam image(Float(32), 2);
    const int tile_size = 16;

    Var x("x"), y("y"), xi("xi"), yi("yi"), t("t");

    // Brighten the image
    Func brighter("brighter");
    brighter(x, y) = my_powf(image(x, y), 0.8f);

    // Select either the brighter or the input depending on the bitmap
    Func output("output");
    output(x, y) = select(bitmap(x/tile_size, y/tile_size), brighter(x, y), image(x, y));

    // Compute the output in tiles of the appropriate size
    output.tile(x, y, xi, yi, tile_size, tile_size);

    // Vectorize within tiles. We would also parallelize across tiles,
    // but that introduces a race condition in the call_count.
    output.vectorize(xi, 4).fuse(x, y, t);

    // Compute brighter per output tile
    brighter.compute_at(output, t);

    // Assert that the output is a whole number of tiles. Otherwise
    // the tiles in the schedule don't match up to the tiles in the
    // algorithm, and you can't safely skip any work.
    output.bound(x, 0, (image.extent(0)/tile_size)*tile_size);
    output.bound(y, 0, (image.extent(1)/tile_size)*tile_size);
    output.compile_jit();

    Image<bool> bitmap_buf(10, 10);
    bitmap_buf(5, 5) = 1;
    bitmap.set(bitmap_buf);

    Image<float> image_buf = lambda(x, y, (sin(x+y)+1)/2).realize(10 * tile_size, 10 * tile_size);
    image.set(image_buf);

    call_count = 0;
    Image<float> result = output.realize(10 * tile_size, 10 * tile_size);

    // Check the right number of calls to powf occurred
    if (call_count != tile_size*tile_size) {
        printf("call_count = %d instead of %d\n", call_count, tile_size * tile_size);
        return -1;
    }

    // Check the output is correct
    for (int y = 0; y < result.height(); y++) {
        for (int x = 0; x < result.width(); x++) {
            bool active = bitmap_buf(x/tile_size, y/tile_size);
            float correct = active ? my_powf(image_buf(x, y), 0.8f) : image_buf(x, y);
            if (fabs(correct - result(x, y)) > 0.001f) {
                printf("result(%d, %d) = %f instead of %f\n",
                       x, y, result(x, y), correct);
                return -1;
            }
        }
    }

    printf("Success!\n");
    return 0;
}
<|endoftext|>


<commit_before>#include "stdafx.h"

#include "ThreadManager.h"
#include "Ini.h"
#include "SceneThread.h"
#include "SceneManager.h"
#include "Config.h"

ThreadManager* g_pThreadManager = NULL ;

ThreadManager::ThreadManager()
{
    __ENTER_FUNCTION

        m_pThreadPool = new ThreadPool ;
        Assert( m_pThreadPool ) ;

        m_pServerThread = new ServerThread ;
        Assert( m_pServerThread ) ;

        m_nThreads = 0 ;

    __LEAVE_FUNCTION
}

ThreadManager::~ThreadManager()
{
    __ENTER_FUNCTION

        SAFE_DELETE( m_pThreadPool) ;
        SAFE_DELETE( m_pServerThread ) ;

    __LEAVE_FUNCTION
}

BOOL ThreadManager::Init( UINT MaxSceneCount )
{
    __ENTER_FUNCTION

        BOOL ret = FALSE ;

        //根据配置文件读取需要使用的场景，为每个场景分配一个线程；
        //读取场景数量
        UINT count = MaxSceneCount ;

        Assert( MAX_SCENE >= count ) ;

        UINT uMaxThreadCount = 0 ;
        UINT i ;
        for ( i = 0; i < count; i++ )
        {
            //读取场景
            SceneID_t SceneID = ( SceneID_t )( g_Config.m_SceneInfo.m_pScene[i].m_SceneID ) ;
            Assert( MAX_SCENE > SceneID ) ;

            UINT ServerID = g_Config.m_SceneInfo.m_pScene[i].m_ServerID ;
            if ( ServerID != g_Config.m_ConfigInfo.m_ServerID )
            {//不是当前服务器的程序运行的场景
                continue ;
            }
            if ( 0 == g_Config.m_SceneInfo.m_pScene[i].m_IsActive )
            {//不是激活的场景
                continue ;
            }

            if ( ( ID_t )uMaxThreadCount < g_Config.m_SceneInfo.m_pScene[i].m_ThreadIndex )
            {
                uMaxThreadCount = g_Config.m_SceneInfo.m_pScene[i].m_ThreadIndex ;
            }
        }

        SceneThread* pSceneThread = NULL ;
        for ( i = 0; i <= uMaxThreadCount; i++ )
        {
            pSceneThread = new SceneThread ;
            Assert( pSceneThread ) ;

            ret = m_pThreadPool->AddThread( i, pSceneThread ) ;
            Assert( ret ) ;

            m_nThreads ++ ;
        }

        /** //my debug output
        for ( i = 0; i <= uMaxThreadCount; i++ )
        {
            Thread** m_pThread = m_pThreadPool->GetThread();
            SceneThread* pSceneThread1 = (SceneThread*)( m_pThread[ i ] );
            LERR( "FILE: %s, LINE: %d, pSceneThread1: %d, index: %d", __FILE__, __LINE__, pSceneThread1, i );
        }
        **/
        MySleep( 3600 );
        for ( i = 0; i < count; i++ )
        {
            //读取场景
            SceneID_t SceneID = ( SceneID_t )( g_Config.m_SceneInfo.m_pScene[i].m_SceneID ) ;
            Assert( MAX_SCENE > SceneID ) ;

            UINT ServerID = g_Config.m_SceneInfo.m_pScene[i].m_ServerID ;
            if ( ServerID != g_Config.m_ConfigInfo.m_ServerID )
            {//不是当前服务器的程序运行的场景
                continue ;
            }
            if ( 0 == g_Config.m_SceneInfo.m_pScene[i].m_IsActive )
            {//不是激活的场景
                continue ;
            }

            SceneThread* pSceneThread = ( SceneThread* )( m_pThreadPool->GetThreadByIndex( g_Config.m_SceneInfo.m_pScene[i].m_ThreadIndex ) ) ;
            if ( NULL == pSceneThread )
            {
                AssertEx( FALSE, "没有创建所需的线程" ) ;
            }
            else
            {

                Scene* pScene = g_pSceneManager->GetScene( SceneID ) ;
                pSceneThread->AddScene( pScene );
            }
        }

        if ( m_pServerThread->IsActive() )
        {
            m_nThreads ++ ;
        }

        return TRUE ;

    __LEAVE_FUNCTION

        return FALSE ;
}

BOOL ThreadManager::Start()
{
    __ENTER_FUNCTION

        BOOL ret ;
        m_pServerThread->start() ;
        MySleep( 500 ) ;
        ret = m_pThreadPool->Start( ) ;

        return ret ;

    __LEAVE_FUNCTION

        return FALSE ;
}

BOOL ThreadManager::Stop()
{
    __ENTER_FUNCTION

        if ( m_pServerThread )
        {
            m_pServerThread->stop() ;
        }

        return m_pThreadPool->Stop() ;

    __LEAVE_FUNCTION

        return FALSE ;
}
<commit_msg>fix bug<commit_after>#include "stdafx.h"

#include "ThreadManager.h"
#include "Ini.h"
#include "SceneThread.h"
#include "SceneManager.h"
#include "Config.h"

ThreadManager* g_pThreadManager = NULL ;

ThreadManager::ThreadManager()
{
    __ENTER_FUNCTION

        m_pThreadPool = new ThreadPool ;
        Assert( m_pThreadPool ) ;

        m_pServerThread = new ServerThread ;
        Assert( m_pServerThread ) ;

        m_nThreads = 0 ;

    __LEAVE_FUNCTION
}

ThreadManager::~ThreadManager()
{
    __ENTER_FUNCTION

        SAFE_DELETE( m_pThreadPool) ;
        SAFE_DELETE( m_pServerThread ) ;

    __LEAVE_FUNCTION
}

BOOL ThreadManager::Init( UINT MaxSceneCount )
{
    __ENTER_FUNCTION

        BOOL ret = FALSE ;

        //根据配置文件读取需要使用的场景，为每个场景分配一个线程；
        //读取场景数量
        UINT count = MaxSceneCount ;

        Assert( MAX_SCENE >= count ) ;

        UINT uMaxThreadCount = 0 ;
        UINT i ;
        for ( i = 0; i < count; i++ )
        {
            //读取场景
            SceneID_t SceneID = ( SceneID_t )( g_Config.m_SceneInfo.m_pScene[i].m_SceneID ) ;
            Assert( MAX_SCENE > SceneID ) ;

            UINT ServerID = g_Config.m_SceneInfo.m_pScene[i].m_ServerID ;
            if ( ServerID != g_Config.m_ConfigInfo.m_ServerID )
            {//不是当前服务器的程序运行的场景
                continue ;
            }
            if ( 0 == g_Config.m_SceneInfo.m_pScene[i].m_IsActive )
            {//不是激活的场景
                continue ;
            }

            if ( ( ID_t )uMaxThreadCount < g_Config.m_SceneInfo.m_pScene[i].m_ThreadIndex )
            {
                uMaxThreadCount = g_Config.m_SceneInfo.m_pScene[i].m_ThreadIndex ;
            }
        }

        SceneThread* pSceneThread = NULL ;
        for ( i = 0; i <= uMaxThreadCount; i++ )
        {
            pSceneThread = new SceneThread ;
            Assert( pSceneThread ) ;

            ret = m_pThreadPool->AddThread( i, pSceneThread ) ;
            Assert( ret ) ;

            m_nThreads ++ ;
        }

        /** //my debug output
        for ( i = 0 ; i <= uMaxThreadCount ; i++ )
        {
            Thread** m_pThread = m_pThreadPool->GetThread() ;
            SceneThread* pSceneThread1 = (SceneThread*)( m_pThread[ i ] ) ;
            LERR( "FILE: %s, LINE: %d, pSceneThread1: %d, index: %d", __FILE__, __LINE__, pSceneThread1, i ) ;
        }
        **/
        MySleep( 3600 ) ;
        for ( i = 0; i < count; i++ )
        {
            //读取场景
            SceneID_t SceneID = ( SceneID_t )( g_Config.m_SceneInfo.m_pScene[i].m_SceneID ) ;
            Assert( MAX_SCENE > SceneID ) ;

            UINT ServerID = g_Config.m_SceneInfo.m_pScene[i].m_ServerID ;
            if ( ServerID != g_Config.m_ConfigInfo.m_ServerID )
            {//不是当前服务器的程序运行的场景
                continue ;
            }
            if ( 0 == g_Config.m_SceneInfo.m_pScene[i].m_IsActive )
            {//不是激活的场景
                continue ;
            }

            SceneThread* pSceneThread = ( SceneThread* )( m_pThreadPool->GetThreadByIndex( g_Config.m_SceneInfo.m_pScene[i].m_ThreadIndex ) ) ;
            if ( NULL == pSceneThread )
            {
                AssertEx( FALSE, "没有创建所需的线程" ) ;
            }
            else
            {

                Scene* pScene = g_pSceneManager->GetScene( SceneID ) ;
                pSceneThread->AddScene( pScene ) ;
            }
        }

        if ( m_pServerThread->IsActive() )
        {
            m_nThreads ++ ;
        }

        return TRUE ;

    __LEAVE_FUNCTION

        return FALSE ;
}

BOOL ThreadManager::Start()
{
    __ENTER_FUNCTION

        BOOL ret ;
        m_pServerThread->start() ;
        MySleep( 500 ) ;
        ret = m_pThreadPool->Start() ;

        return ret ;

    __LEAVE_FUNCTION

        return FALSE ;
}

BOOL ThreadManager::Stop()
{
    __ENTER_FUNCTION

        if ( m_pServerThread )
        {
            m_pServerThread->stop() ;
        }

        return m_pThreadPool->Stop() ;

    __LEAVE_FUNCTION

        return FALSE ;
}
<|endoftext|>


<commit_before>#ifndef COFFEE_MILL_REWEIGHT_WHAM_HPP
#define COFFEE_MILL_REWEIGHT_WHAM_HPP

#include "Histogram.hpp"
#include "ProbabilityDensity.hpp"
#include "PotentialFunction.hpp"
#include "ReactionCoordinate.hpp"

#include <mill/util/logger.hpp>
#include <vector>
#include <cmath>

namespace mill
{

class WHAMSolver
{
  public:

    WHAMSolver(const double kBT, const double tolerance,
               const std::size_t max_iteration)
        : kBT_(kBT), beta_(1.0 / kBT), tolerance_(tolerance), max_iteration_(max_iteration)
    {}

    ProbabilityDensity operator()(const std::vector<std::pair<
            std::vector<double>, std::unique_ptr<PotentialFunction>>>& trajs,
            const std::size_t bins = 100)
    {
        return this->reconstruct(this->solve_f(trajs), trajs, bins);
    }

  private:

    double relative_diff(const double lhs, const double rhs) const noexcept
    {
        return (lhs - rhs) / ((lhs + rhs) * 0.5);
    }

    std::vector<double> solve_f(const std::vector<std::pair<
            std::vector<double>, std::unique_ptr<PotentialFunction>>>& trajs);

    ProbabilityDensity reconstruct(const std::vector<double>& fs,
            const std::vector<std::pair<std::vector<double>,
                std::unique_ptr<PotentialFunction>>>& trajs,
            const std::size_t bins);

  private:
    double kBT_, beta_, tolerance_;
    std::size_t max_iteration_;
};

inline std::vector<double> WHAMSolver::solve_f(const std::vector<std::pair<
        std::vector<double>, std::unique_ptr<PotentialFunction>>>& trajs)
{
    const auto nwin = trajs.size();
    std::vector<double> expfs_prev(nwin, 1.0);

    // (l, i) -> l * nwin + i
    std::vector<std::vector<double>> cache_expW(nwin * nwin);
    for(std::size_t l = 0; l < nwin; ++l)
    {
        if(not trajs.at(l).second)
        {
            log::fatal("traj ", l, " does not have potential function");
        }

        const auto& Vl = *(trajs.at(l).second);
        for(std::size_t i = 0; i < nwin; ++i)
        {
            const auto& traj_i = trajs.at(i).first;
            std::vector<double> expWs_i(traj_i.size());
            for(std::size_t j=0; j < traj_i.size(); ++j)
            {
                expWs_i.at(j) = std::exp(-beta_ * Vl(traj_i.at(j)));
            }
            cache_expW.at(l * nwin + i) = expWs_i;
        }
    }
    log::info("expW cached");

    for(std::size_t iteration = 0; iteration < max_iteration_; ++iteration)
    {
        std::vector<double> expfs(trajs.size(), 0.0);

        for(std::size_t l = 0; l < nwin; ++l)
        {
            auto& expf = expfs.at(l);
            for(std::size_t i = 0; i < nwin; ++i)
            {
                const auto& expW   = cache_expW.at(l * nwin + i);
                const auto& traj_i = trajs.at(i).first;

                for(std::size_t j = 0; j < traj_i.size(); ++j)
                {
                    const double numer = expW.at(j);
                    double denom = 0.0;
                    for(std::size_t k = 0; k < nwin; ++k)
                    {
                        denom += trajs.at(k).first.size() *
                                 cache_expW.at(k * nwin + i).at(j) *
                                 expfs_prev.at(k);
                    }
                    expf += numer / denom;
                }
            }
        }

        for(auto& expf : expfs)
        {
            expf = 1.0 / expf;
        }

        double max_relative_diff = 0.0;
        for(std::size_t i=0; i<expfs.size(); ++i)
        {
            max_relative_diff = std::max(max_relative_diff,
                    relative_diff(expfs.at(i), expfs_prev.at(i)));
        }

        log::info(iteration, "-th iteration: diff = ", max_relative_diff);
        if(max_relative_diff < tolerance_)
        {
            log::info("parameter converged: ", expfs);
            return expfs;
        }
        expfs_prev = expfs;
    }
    log::fatal("WHAM does not converge after ", max_iteration_, " iteration.");
}

inline ProbabilityDensity WHAMSolver::reconstruct(const std::vector<double>& expfs,
    const std::vector<std::pair<std::vector<double>, std::unique_ptr<PotentialFunction>>>& trajs,
    const std::size_t bins)
{
    double start =  std::numeric_limits<double>::max();
    double stop  = -std::numeric_limits<double>::max();
    for(const auto& [traj, rc] : trajs)
    {
        for(const auto& x : traj)
        {
            start = std::min(start, x);
            stop  = std::max(stop,  x);
        }
    }
    const double dx = (stop - start) / (bins - 1);
    start -= dx * 0.5;
    stop  += dx * 0.5;
    log::info("WHAM: range = [", start, ", ", stop, ") dx = ", dx);

    Histogram<double, double> unbiased(bins, start, stop);
    for(const auto& [traj, rc] : trajs)
    {
        for(const auto& x: traj)
        {
            double denom = 0.0;
            for(std::size_t i=0; i<trajs.size(); ++i)
            {
                denom += trajs.at(i).first.size() * expfs.at(i) *
                         std::exp(-1.0 * this->beta_ * (*trajs.at(i).second)(x));
            }
            unbiased.at(x) += 1.0 / denom;
        }
    }
    return ProbabilityDensity(unbiased);
}

} // mill
#endif// COFFEE_MILL_MATH_HISTOGRAM_HPP
<commit_msg>:rotating_light: remove structural binding<commit_after>#ifndef COFFEE_MILL_REWEIGHT_WHAM_HPP
#define COFFEE_MILL_REWEIGHT_WHAM_HPP

#include "Histogram.hpp"
#include "ProbabilityDensity.hpp"
#include "PotentialFunction.hpp"
#include "ReactionCoordinate.hpp"

#include <mill/util/logger.hpp>
#include <vector>
#include <cmath>

namespace mill
{

class WHAMSolver
{
  public:

    WHAMSolver(const double kBT, const double tolerance,
               const std::size_t max_iteration)
        : kBT_(kBT), beta_(1.0 / kBT), tolerance_(tolerance), max_iteration_(max_iteration)
    {}

    ProbabilityDensity operator()(const std::vector<std::pair<
            std::vector<double>, std::unique_ptr<PotentialFunction>>>& trajs,
            const std::size_t bins = 100)
    {
        return this->reconstruct(this->solve_f(trajs), trajs, bins);
    }

  private:

    double relative_diff(const double lhs, const double rhs) const noexcept
    {
        return (lhs - rhs) / ((lhs + rhs) * 0.5);
    }

    std::vector<double> solve_f(const std::vector<std::pair<
            std::vector<double>, std::unique_ptr<PotentialFunction>>>& trajs);

    ProbabilityDensity reconstruct(const std::vector<double>& fs,
            const std::vector<std::pair<std::vector<double>,
                std::unique_ptr<PotentialFunction>>>& trajs,
            const std::size_t bins);

  private:
    double kBT_, beta_, tolerance_;
    std::size_t max_iteration_;
};

inline std::vector<double> WHAMSolver::solve_f(const std::vector<std::pair<
        std::vector<double>, std::unique_ptr<PotentialFunction>>>& trajs)
{
    const auto nwin = trajs.size();
    std::vector<double> expfs_prev(nwin, 1.0);

    // (l, i) -> l * nwin + i
    std::vector<std::vector<double>> cache_expW(nwin * nwin);
    for(std::size_t l = 0; l < nwin; ++l)
    {
        if(not trajs.at(l).second)
        {
            log::fatal("traj ", l, " does not have potential function");
        }

        const auto& Vl = *(trajs.at(l).second);
        for(std::size_t i = 0; i < nwin; ++i)
        {
            const auto& traj_i = trajs.at(i).first;
            std::vector<double> expWs_i(traj_i.size());
            for(std::size_t j=0; j < traj_i.size(); ++j)
            {
                expWs_i.at(j) = std::exp(-beta_ * Vl(traj_i.at(j)));
            }
            cache_expW.at(l * nwin + i) = expWs_i;
        }
    }
    log::info("expW cached");

    for(std::size_t iteration = 0; iteration < max_iteration_; ++iteration)
    {
        std::vector<double> expfs(trajs.size(), 0.0);

        for(std::size_t l = 0; l < nwin; ++l)
        {
            auto& expf = expfs.at(l);
            for(std::size_t i = 0; i < nwin; ++i)
            {
                const auto& expW   = cache_expW.at(l * nwin + i);
                const auto& traj_i = trajs.at(i).first;

                for(std::size_t j = 0; j < traj_i.size(); ++j)
                {
                    const double numer = expW.at(j);
                    double denom = 0.0;
                    for(std::size_t k = 0; k < nwin; ++k)
                    {
                        denom += trajs.at(k).first.size() *
                                 cache_expW.at(k * nwin + i).at(j) *
                                 expfs_prev.at(k);
                    }
                    expf += numer / denom;
                }
            }
        }

        for(auto& expf : expfs)
        {
            expf = 1.0 / expf;
        }

        double max_relative_diff = 0.0;
        for(std::size_t i=0; i<expfs.size(); ++i)
        {
            max_relative_diff = std::max(max_relative_diff,
                    relative_diff(expfs.at(i), expfs_prev.at(i)));
        }

        log::info(iteration, "-th iteration: diff = ", max_relative_diff);
        if(max_relative_diff < tolerance_)
        {
            log::info("parameter converged: ", expfs);
            return expfs;
        }
        expfs_prev = expfs;
    }
    log::fatal("WHAM does not converge after ", max_iteration_, " iteration.");
}

inline ProbabilityDensity WHAMSolver::reconstruct(const std::vector<double>& expfs,
    const std::vector<std::pair<std::vector<double>, std::unique_ptr<PotentialFunction>>>& trajs,
    const std::size_t bins)
{
    double start =  std::numeric_limits<double>::max();
    double stop  = -std::numeric_limits<double>::max();
    for(const auto& traj_rc : trajs)
    {
        for(const auto& x : traj_rc.first)
        {
            start = std::min(start, x);
            stop  = std::max(stop,  x);
        }
    }
    const double dx = (stop - start) / (bins - 1);
    start -= dx * 0.5;
    stop  += dx * 0.5;
    log::info("WHAM: range = [", start, ", ", stop, ") dx = ", dx);

    Histogram<double, double> unbiased(bins, start, stop);
    for(const auto& traj_rc : trajs)
    {
        for(const auto& x: traj_rc.first)
        {
            double denom = 0.0;
            for(std::size_t i=0; i<trajs.size(); ++i)
            {
                denom += trajs.at(i).first.size() * expfs.at(i) *
                         std::exp(-1.0 * this->beta_ * (*trajs.at(i).second)(x));
            }
            unbiased.at(x) += 1.0 / denom;
        }
    }
    return ProbabilityDensity(unbiased);
}

} // mill
#endif// COFFEE_MILL_MATH_HISTOGRAM_HPP
<|endoftext|>


<commit_before>/*
Resembla: Word-based Japanese similar sentence search library
https://github.com/tuem/resembla

Copyright 2017 Takashi Uemura

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "feature_extractor.hpp"

#include <iostream>

#include "../../string_util.hpp"

namespace resembla {

const char FeatureExtractor::FEATURE_DELIMITER = '&';
const char FeatureExtractor::KEYVALUE_DELIMITER = '=';

FeatureExtractor::FeatureExtractor(const std::string base_similarity_key):
    base_similarity_key(base_similarity_key)
{}

void FeatureExtractor::append(Feature::key_type key, std::shared_ptr<Function> func)
{
    functions[key] = func;
}

FeatureExtractor::output_type FeatureExtractor::operator()(const std::string& raw_text, const std::string& raw_features) const
{
#ifdef DEBUG
    std::cerr << "load features from saved data: text=" << raw_text << ", features=" << raw_features << std::endl;
#else
    (void)raw_text;
#endif
    output_type features;
    for(const auto& f: split(raw_features, FEATURE_DELIMITER)){
        auto kv = split(f, KEYVALUE_DELIMITER);
        if(kv.size() == 2){
#ifdef DEBUG
            std::cerr << "load feature: key=" << kv[0] << ", value=" << kv[1] << std::endl;
#endif
            features[kv[0]] = kv[1];
        }
    }
    return features;
}

FeatureExtractor::output_type FeatureExtractor::operator()(
        const resembla::ResemblaResponse& data, const output_type& given_features) const
{
#ifdef DEBUG
    std::cerr << "extract features" << std::endl;
#endif
    output_type features(given_features);
    features[base_similarity_key] = Feature::toText(data.score);
    for(const auto& i: functions){
        auto k = features.find(i.first);
        if(k == std::end(features)){
#ifdef DEBUG
            std::cerr << "extract feature: " << i.first << std::endl;
#endif
            features[i.first] = (*i.second)(data.text);
        }
        else{
#ifdef DEBUG
            std::cerr << "skip already computed feature: " << i.first << std::endl;
#endif
        }
    }
    return features;
}

FeatureExtractor::output_type FeatureExtractor::operator()(const string_type& text) const
{
#ifdef DEBUG
    std::cerr << "extract features from text" << std::endl;
#endif
    output_type features;
    for(const auto& i: functions){
#ifdef DEBUG
        std::cerr << "extract feature: " << i.first << std::endl;
#endif
        features[i.first] = (*i.second)(text);
    }
    return features;
}

}
<commit_msg>add todo<commit_after>/*
Resembla: Word-based Japanese similar sentence search library
https://github.com/tuem/resembla

Copyright 2017 Takashi Uemura

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "feature_extractor.hpp"

#include <iostream>

#include "../../string_util.hpp"

namespace resembla {

const char FeatureExtractor::FEATURE_DELIMITER = '&';
const char FeatureExtractor::KEYVALUE_DELIMITER = '=';

FeatureExtractor::FeatureExtractor(const std::string base_similarity_key):
    base_similarity_key(base_similarity_key)
{}

void FeatureExtractor::append(Feature::key_type key, std::shared_ptr<Function> func)
{
    functions[key] = func;
}

FeatureExtractor::output_type FeatureExtractor::operator()(const std::string& raw_text, const std::string& raw_features) const
{
#ifdef DEBUG
    std::cerr << "load features from saved data: text=" << raw_text << ", features=" << raw_features << std::endl;
#else
    (void)raw_text;
#endif
    output_type features;
    for(const auto& f: split(raw_features, FEATURE_DELIMITER)){
        auto kv = split(f, KEYVALUE_DELIMITER);
        if(kv.size() == 2){
#ifdef DEBUG
            std::cerr << "load feature: key=" << kv[0] << ", value=" << kv[1] << std::endl;
#endif
            features[kv[0]] = kv[1];
        }
    }
    return features;
}

FeatureExtractor::output_type FeatureExtractor::operator()(
        const resembla::ResemblaResponse& data, const output_type& given_features) const
{
#ifdef DEBUG
    std::cerr << "extract features" << std::endl;
#endif
    output_type features(given_features);
    // TODO: remove this code. FeatureExtractor should accept multiple base similarities
    features[base_similarity_key] = Feature::toText(data.score);
    for(const auto& i: functions){
        auto k = features.find(i.first);
        if(k == std::end(features)){
#ifdef DEBUG
            std::cerr << "extract feature: " << i.first << std::endl;
#endif
            features[i.first] = (*i.second)(data.text);
        }
        else{
#ifdef DEBUG
            std::cerr << "skip already computed feature: " << i.first << std::endl;
#endif
        }
    }
    return features;
}

FeatureExtractor::output_type FeatureExtractor::operator()(const string_type& text) const
{
#ifdef DEBUG
    std::cerr << "extract features from text" << std::endl;
#endif
    output_type features;
    for(const auto& i: functions){
#ifdef DEBUG
        std::cerr << "extract feature: " << i.first << std::endl;
#endif
        features[i.first] = (*i.second)(text);
    }
    return features;
}

}
<|endoftext|>


<commit_before>#include "sdlwrap.hpp"

namespace rs {
	void Window::SetStdGLAttributes(int major, int minor, int depth) {
		SetGLAttributes(SDL_GL_CONTEXT_MAJOR_VERSION, major,
						SDL_GL_CONTEXT_MINOR_VERSION, minor,
						SDL_GL_DOUBLEBUFFER, 1,
						SDL_GL_DEPTH_SIZE, depth);
	}
	SPWindow Window::Create(const std::string& title, int w, int h, uint32_t flag) {
		return Create(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, flag|SDL_WINDOW_OPENGL);
	}
	SPWindow Window::Create(const std::string& title, int x, int y, int w, int h, uint32_t flag) {
		return SPWindow(new Window(SDLEC_P(Trap, SDL_CreateWindow, title.c_str(), x, y, w, h, flag|SDL_WINDOW_OPENGL)));
	}
	Window::Window(SDL_Window* w): _window(w) {
		_checkState();
	}
	Window::~Window() {
		SDLEC_P(Warn, SDL_DestroyWindow, _window);
	}
	void Window::_checkState() {
		uint32_t f = getSDLFlag();
		if(f & SDL_WINDOW_SHOWN)
			_stat = Stat::Hidden;
		else if(f & SDL_WINDOW_FULLSCREEN)
			_stat = Stat::Fullscreen;
		else if(f & SDL_WINDOW_MINIMIZED)
			_stat = Stat::Minimized;
		else if(f & SDL_WINDOW_MAXIMIZED)
			_stat = Stat::Maximized;
		else
			_stat = Stat::Shown;
	}
	void Window::setFullscreen(bool bFull) {
		SDLEC_P(Warn, SDL_SetWindowFullscreen, _window, bFull ? SDL_WINDOW_FULLSCREEN : 0);
		_checkState();
	}
	void Window::setGrab(bool bGrab) {
		SDLEC_P(Warn, SDL_SetWindowGrab, _window, bGrab ? SDL_TRUE : SDL_FALSE);
	}
	void Window::setMaximumSize(int w, int h) {
		SDLEC_P(Warn, SDL_SetWindowMaximumSize, _window, w, h);
	}
	void Window::setMinimumSize(int w, int h) {
		SDLEC_P(Warn, SDL_SetWindowMinimumSize, _window, w, h);
	}
	void Window::setSize(int w, int h) {
		SDLEC_P(Warn, SDL_SetWindowSize, _window, w, h);
	}
	void Window::show(bool bShow) {
		if(bShow)
			SDLEC_P(Warn, SDL_ShowWindow, _window);
		else
			SDLEC_P(Warn, SDL_HideWindow, _window);
		_checkState();
	}
	void Window::setTitle(const std::string& title) {
		SDLEC_P(Warn, SDL_SetWindowTitle, _window, title.c_str());
	}
	void Window::maximize() {
		SDLEC_P(Warn, SDL_MaximizeWindow, _window);
		_checkState();
	}
	void Window::minimize() {
		SDLEC_P(Warn, SDL_MinimizeWindow, _window);
		_checkState();
	}
	void Window::restore() {
		SDLEC_P(Warn, SDL_RestoreWindow, _window);
		_checkState();
	}
	void Window::setPosition(int x, int y) {
		SDLEC_P(Warn, SDL_SetWindowPosition, _window, x, y);
	}
	void Window::raise() {
		SDLEC_P(Warn, SDL_RaiseWindow, _window);
		_checkState();
	}
	uint32_t Window::getID() const {
		return SDLEC_P(Warn, SDL_GetWindowID, _window);
	}
	Window::Stat Window::getState() const {
		return _stat;
	}
	bool Window::isGrabbed() const {
		return SDLEC_P(Warn, SDL_GetWindowGrab, _window) == SDL_TRUE;
	}
	bool Window::isResizable() const {
		return getSDLFlag() & SDL_WINDOW_RESIZABLE;
	}
	bool Window::hasInputFocus() const {
		return getSDLFlag() & SDL_WINDOW_INPUT_FOCUS;
	}
	bool Window::hasMouseFocus() const {
		return getSDLFlag() & SDL_WINDOW_MOUSE_FOCUS;
	}
	namespace {
		spn::Size GetSize(void (*func)(SDL_Window*,int*,int*), SDL_Window* wd) {
			int w,h;
			SDLEC_P(Warn, func, wd, &w, &h);
			return spn::Size(w,h);
		}
	}
	spn::Size Window::getSize() const {
		return GetSize(SDL_GetWindowSize, _window);
	}
	spn::Size Window::getMaximumSize() const {
		return GetSize(SDL_GetWindowMaximumSize, _window);
	}
	spn::Size Window::getMinimumSize() const {
		return GetSize(SDL_GetWindowMinimumSize, _window);
	}
	uint32_t Window::getSDLFlag() const {
		return SDLEC_P(Warn, SDL_GetWindowFlags, _window);
	}
	SDL_Window* Window::getWindow() const {
		return _window;
	}

	void Window::EnableScreenSaver(bool bEnable) {
		if(bEnable)
			SDLEC_P(Warn, SDL_EnableScreenSaver);
		else
			SDLEC_P(Warn, SDL_DisableScreenSaver);
	}
}
<commit_msg>sdlw_window: 初期化位置をSDL_WINDOWPOS_UNDEFINEDにするとwindows環境で妙な位置にセットされるので固定<commit_after>#include "sdlwrap.hpp"

namespace rs {
	void Window::SetStdGLAttributes(int major, int minor, int depth) {
		SetGLAttributes(SDL_GL_CONTEXT_MAJOR_VERSION, major,
						SDL_GL_CONTEXT_MINOR_VERSION, minor,
						SDL_GL_DOUBLEBUFFER, 1,
						SDL_GL_DEPTH_SIZE, depth);
	}
	SPWindow Window::Create(const std::string& title, int w, int h, uint32_t flag) {
		return Create(title, 128, 128, w, h, flag|SDL_WINDOW_OPENGL);
	}
	SPWindow Window::Create(const std::string& title, int x, int y, int w, int h, uint32_t flag) {
		return SPWindow(new Window(SDLEC_P(Warn, SDL_CreateWindow, title.c_str(), x, y, w, h, flag|SDL_WINDOW_OPENGL)));
	}
	Window::Window(SDL_Window* w): _window(w) {
		_checkState();
	}
	Window::~Window() {
		SDLEC_P(Warn, SDL_DestroyWindow, _window);
	}
	void Window::_checkState() {
		uint32_t f = getSDLFlag();
		if(f & SDL_WINDOW_SHOWN)
			_stat = Stat::Hidden;
		else if(f & SDL_WINDOW_FULLSCREEN)
			_stat = Stat::Fullscreen;
		else if(f & SDL_WINDOW_MINIMIZED)
			_stat = Stat::Minimized;
		else if(f & SDL_WINDOW_MAXIMIZED)
			_stat = Stat::Maximized;
		else
			_stat = Stat::Shown;
	}
	void Window::setFullscreen(bool bFull) {
		SDLEC_P(Warn, SDL_SetWindowFullscreen, _window, bFull ? SDL_WINDOW_FULLSCREEN : 0);
		_checkState();
	}
	void Window::setGrab(bool bGrab) {
		SDLEC_P(Warn, SDL_SetWindowGrab, _window, bGrab ? SDL_TRUE : SDL_FALSE);
	}
	void Window::setMaximumSize(int w, int h) {
		SDLEC_P(Warn, SDL_SetWindowMaximumSize, _window, w, h);
	}
	void Window::setMinimumSize(int w, int h) {
		SDLEC_P(Warn, SDL_SetWindowMinimumSize, _window, w, h);
	}
	void Window::setSize(int w, int h) {
		SDLEC_P(Warn, SDL_SetWindowSize, _window, w, h);
	}
	void Window::show(bool bShow) {
		if(bShow)
			SDLEC_P(Warn, SDL_ShowWindow, _window);
		else
			SDLEC_P(Warn, SDL_HideWindow, _window);
		_checkState();
	}
	void Window::setTitle(const std::string& title) {
		SDLEC_P(Warn, SDL_SetWindowTitle, _window, title.c_str());
	}
	void Window::maximize() {
		SDLEC_P(Warn, SDL_MaximizeWindow, _window);
		_checkState();
	}
	void Window::minimize() {
		SDLEC_P(Warn, SDL_MinimizeWindow, _window);
		_checkState();
	}
	void Window::restore() {
		SDLEC_P(Warn, SDL_RestoreWindow, _window);
		_checkState();
	}
	void Window::setPosition(int x, int y) {
		SDLEC_P(Warn, SDL_SetWindowPosition, _window, x, y);
	}
	void Window::raise() {
		SDLEC_P(Warn, SDL_RaiseWindow, _window);
		_checkState();
	}
	uint32_t Window::getID() const {
		return SDLEC_P(Warn, SDL_GetWindowID, _window);
	}
	Window::Stat Window::getState() const {
		return _stat;
	}
	bool Window::isGrabbed() const {
		return SDLEC_P(Warn, SDL_GetWindowGrab, _window) == SDL_TRUE;
	}
	bool Window::isResizable() const {
		return getSDLFlag() & SDL_WINDOW_RESIZABLE;
	}
	bool Window::hasInputFocus() const {
		return getSDLFlag() & SDL_WINDOW_INPUT_FOCUS;
	}
	bool Window::hasMouseFocus() const {
		return getSDLFlag() & SDL_WINDOW_MOUSE_FOCUS;
	}
	namespace {
		spn::Size GetSize(void (*func)(SDL_Window*,int*,int*), SDL_Window* wd) {
			int w,h;
			SDLEC_P(Warn, func, wd, &w, &h);
			return spn::Size(w,h);
		}
	}
	spn::Size Window::getSize() const {
		return GetSize(SDL_GetWindowSize, _window);
	}
	spn::Size Window::getMaximumSize() const {
		return GetSize(SDL_GetWindowMaximumSize, _window);
	}
	spn::Size Window::getMinimumSize() const {
		return GetSize(SDL_GetWindowMinimumSize, _window);
	}
	uint32_t Window::getSDLFlag() const {
		return SDLEC_P(Warn, SDL_GetWindowFlags, _window);
	}
	SDL_Window* Window::getWindow() const {
		return _window;
	}

	void Window::EnableScreenSaver(bool bEnable) {
		if(bEnable)
			SDLEC_P(Warn, SDL_EnableScreenSaver);
		else
			SDLEC_P(Warn, SDL_DisableScreenSaver);
	}
}
<|endoftext|>


<commit_before>/*
 * Copyright (c) 2008, Willow Garage, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Willow Garage, Inc. nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <OgreSceneNode.h>
#include <OgreSceneManager.h>

#include <ros/time.h>

#include <laser_geometry/laser_geometry.h>

#include <rviz/default_plugin/point_cloud_common.h>
#include <rviz/display_context.h>
#include <rviz/frame_manager.h>
#include <rviz/ogre_helpers/point_cloud.h>
#include <rviz/properties/int_property.h>
#include <rviz/validate_floats.h>

#include "laser_scan_display.h"

namespace rviz
{

LaserScanDisplay::LaserScanDisplay()
  : point_cloud_common_( new PointCloudCommon( this ))
  , projector_( new laser_geometry::LaserProjection() )
{
  // PointCloudCommon sets up a callback queue with a thread for each
  // instance.  Use that for processing incoming messages.
  update_nh_.setCallbackQueue( point_cloud_common_->getCallbackQueue() );
}

LaserScanDisplay::~LaserScanDisplay()
{
  delete point_cloud_common_;
  delete projector_;
}

void LaserScanDisplay::onInitialize()
{
  MFDClass::onInitialize();
  point_cloud_common_->initialize( context_, scene_node_ );
}

void LaserScanDisplay::processMessage( const sensor_msgs::LaserScanConstPtr& scan )
{
  sensor_msgs::PointCloudPtr cloud( new sensor_msgs::PointCloud );

  std::string frame_id = scan->header.frame_id;

  // Compute tolerance necessary for this scan
  ros::Duration tolerance(scan->time_increment * scan->ranges.size());
  if (tolerance > filter_tolerance_)
  {
    filter_tolerance_ = tolerance;
    tf_filter_->setTolerance(filter_tolerance_);
  }

  try
  {
    // TODO(wjwwood): remove this and use tf2 interface instead
#ifndef _WIN32
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif

    auto tf_client = context_->getTFClient();

#ifndef _WIN32
# pragma GCC diagnostic pop
#endif
    projector_->transformLaserScanToPointCloud( fixed_frame_.toStdString(), *scan, *cloud, *tf_client,
                                                laser_geometry::channel_option::Intensity );
  }
  catch (tf::TransformException& e)
  {
    ROS_DEBUG( "LaserScan [%s]: failed to transform scan: %s.  This message should not repeat (tolerance should now be set on our tf::MessageFilter).",
               qPrintable( getName() ), e.what() );
    return;
  }

  point_cloud_common_->addMessage( cloud );
}

void LaserScanDisplay::update( float wall_dt, float ros_dt )
{
  point_cloud_common_->update( wall_dt, ros_dt );
}

void LaserScanDisplay::reset()
{
  MFDClass::reset();
  point_cloud_common_->reset();
}

} // namespace rviz

#include <pluginlib/class_list_macros.hpp>
PLUGINLIB_EXPORT_CLASS( rviz::LaserScanDisplay, rviz::Display )
<commit_msg>tf2: port LaserScanDisplay<commit_after>/*
 * Copyright (c) 2008, Willow Garage, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Willow Garage, Inc. nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <OgreSceneNode.h>
#include <OgreSceneManager.h>

#include <ros/time.h>

#include <laser_geometry/laser_geometry.h>

#include <rviz/default_plugin/point_cloud_common.h>
#include <rviz/display_context.h>
#include <rviz/frame_manager.h>
#include <rviz/ogre_helpers/point_cloud.h>
#include <rviz/properties/int_property.h>
#include <rviz/validate_floats.h>

#include "laser_scan_display.h"

namespace rviz
{

LaserScanDisplay::LaserScanDisplay()
  : point_cloud_common_( new PointCloudCommon( this ))
  , projector_( new laser_geometry::LaserProjection() )
{
  // PointCloudCommon sets up a callback queue with a thread for each
  // instance.  Use that for processing incoming messages.
  update_nh_.setCallbackQueue( point_cloud_common_->getCallbackQueue() );
}

LaserScanDisplay::~LaserScanDisplay()
{
  delete point_cloud_common_;
  delete projector_;
}

void LaserScanDisplay::onInitialize()
{
  MFDClass::onInitialize();
  point_cloud_common_->initialize( context_, scene_node_ );
}

void LaserScanDisplay::processMessage( const sensor_msgs::LaserScanConstPtr& scan )
{
  sensor_msgs::PointCloud2Ptr cloud( new sensor_msgs::PointCloud2 );

  // Compute tolerance necessary for this scan
  ros::Duration tolerance(scan->time_increment * scan->ranges.size());
  if (tolerance > filter_tolerance_)
  {
    filter_tolerance_ = tolerance;
    tf_filter_->setTolerance(filter_tolerance_);
  }

  try
  {
    auto tf = context_->getTF2BufferPtr();

    projector_->transformLaserScanToPointCloud( fixed_frame_.toStdString(), *scan, *cloud, *tf,
                                                laser_geometry::channel_option::Intensity );
  }
  catch (tf2::TransformException& e)
  {
    ROS_DEBUG( "LaserScan [%s]: failed to transform scan: %s.  This message should not repeat (tolerance should now be set on our tf2::MessageFilter).",
               qPrintable( getName() ), e.what() );
    return;
  }

  point_cloud_common_->addMessage( cloud );
}

void LaserScanDisplay::update( float wall_dt, float ros_dt )
{
  point_cloud_common_->update( wall_dt, ros_dt );
}

void LaserScanDisplay::reset()
{
  MFDClass::reset();
  point_cloud_common_->reset();
}

} // namespace rviz

#include <pluginlib/class_list_macros.hpp>
PLUGINLIB_EXPORT_CLASS( rviz::LaserScanDisplay, rviz::Display )
<|endoftext|>