質問

私はプロジェクトに取り組んでいることが必要で、操作の大行列、具体的には、ピラミッドの合計を記述する計算します。

短いながらプレーする必要がありま跡を比較的少数の値(通常は1で、一以上の1)海の零点のマトリックス(多次元配列)の

に乏配列ではユーザーが店舗の小さな数値として未定義するレコードプリセット値です。されていないため、身体が関与するものと考えられる店内全ての値メモリについては親権者の方の同意が必要だかの数はゼロです。この数は数百万にも作品の応募がありました。

速度は大きな優先順位、そして動的に選択、変数の数のクラスを行います。

私は現在、システムを使用したバイナリ検索ツリー(b-tree)を保存作品の応募がありました。なんだろうけど、日本人のより良いシステム?

役に立ちましたか?

解決

C++、地図より確かなものになるはずだ。数百万の物な問題です。10万件約4.4秒約57megのですが、何が必要ですか。

私の試験であり、そしており

#include <stdio.h>
#include <stdlib.h>
#include <map>

class triple {
public:
    int x;
    int y;
    int z;
    bool operator<(const triple &other) const {
        if (x < other.x) return true;
        if (other.x < x) return false;
        if (y < other.y) return true;
        if (other.y < y) return false;
        return z < other.z;
    }
};

int main(int, char**)
{
    std::map<triple,int> data;
    triple point;
    int i;

    for (i = 0; i < 10000000; ++i) {
        point.x = rand();
        point.y = rand();
        point.z = rand();
        //printf("%d %d %d %d\n", i, point.x, point.y, point.z);
        data[point] = i;
    }
    return 0;
}

今にも動的に選択、変数の数に一番値段が高くなりますが、表 指数の文字列として, 使用して文字列のキーとしての図。例えば、項目内の[23][55]で表現した"23,55"文字列です。したものでありこのソリューションのための高次元;などの三次元任意の指数のような感じになります"34,45,56".簡単な実装でのこの技術は

std::map data<string,int> data;
char ix[100];

sprintf(ix, "%d,%d", x, y); // 2 vars
data[ix] = i;

sprintf(ix, "%d,%d,%d", x, y, z); // 3 vars
data[ix] = i;

他のヒント

として一般的な助言を用いる方法文字列を指標として、実際に 非常に 遅くなります。より効率的な同等の溶液に使うベクトル列.が絶対に必要な書のインデックス文字列です。

typedef vector<size_t> index_t;

struct index_cmp_t : binary_function<index_t, index_t, bool> {
    bool operator ()(index_t const& a, index_t const& b) const {
        for (index_t::size_type i = 0; i < a.size(); ++i)
            if (a[i] != b[i])
                return a[i] < b[i];
        return false;
    }
};

map<index_t, int, index_cmp_t> data;
index_t i(dims);
i[0] = 1;
i[1] = 2;
// … etc.
data[i] = 42;

を使用した場合は、 map 実際にはよくない非常に効率的に実施するバランスのとれたバイナリ検索します。より良いデータ構造のその場合するハッシュテーブルとして提供 std::unordered_map.

Boostは、鋳型の実装ブラスというuBLASを含む疎行列.

http://www.boost.org/doc/libs/1_36_0/libs/numeric/ublas/doc/index.htm

小型の指数と比較しました。だけど、辞書的な比較、それ以外の場合:

a= (1, 2, 1); b= (2, 1, 2);
(a<b) == (b<a) is true, but b!=a

編集:での比較です:

return lhs.x<rhs.x
    ? true 
    : lhs.x==rhs.x 
        ? lhs.y<rhs.y 
            ? true 
            : lhs.y==rhs.y
                ? lhs.z<rhs.z
                : false
        : false

ハッシュテーブル高速挿入を見上げるようにご覧ください.き書簡単なハッシュ関数からいいだけに取り組んで整数値のペアとしての鍵となります。

固有 はC++による線形代数学ライブラリは、 実施 の疎行列.にも対応しマトリクス事業とソルバー(LU分解など)に対して最適化された疎行列.

リストのソリューションでも、わかりやすい。便宜上、引用されるいは関係部署としております。

https://en.wikipedia.org/wiki/Sparse_matrix#Dictionary_of_keys_.28DOK.29

辞書のキー(河)

河の辞書と地図(row,column)-のペアを 値します。ている要素が欠けていの辞書 として扱ってゼロになります。フォーマットは良い距 構築疎行列のランダムな順序でも貧困層のための繰り返し処理 非ゼロの値が辞書的です。一般 を構築しますマトリクスをこのフォーマットを変換する。 効率的な形式にも対応しています。[1]

リストのリスト(リル)

LIL店リストの配列で、各エントリを含むカラム 指数の値です。通常、これらのエントリはソートされる 列インデックスが高速にルックアップ.これは他の形式に良い タマトリクス。[2]

座標一覧(COO)

COOの店舗一覧(行列で値の入ったタプル.理想的には、応募 ているソートによる連続指数は、列インデックス)の向上にランダムアクセス ます。これは他の形式の仲良しカップルにオススメンタルマトリクス 建築です。[3]

圧縮疎行(CSR、CRSやイェール形式)

圧縮された疎行(CSR)compressed row storage(CRS)フォーマット を表すマトリックスMによる一次元)の配列、 それぞれを含むゼロ以外の値の大きさの行列 指数などが含まれる。ではCOOが、圧縮を行指標が するためのものでした。このフォーマットでの高速列アクセス行列-ベクトル multiplications(Mx).

最良の方法を実施疎行列ではないし、それらを実施するために-転されていない。私はあなたにBLAS(あるいは、LAPACK)に対応できる本当に大きな行列.

以降のみの価値観を[a][b][c]...[w][x][y][z]の結果、お店の指数自体ではなく、値が1になるだけでも常に同+はなく、ハッシュします。この次元の呪いの存在を示唆して行くと一部の設立ツールNISTは、少なくとも一読のう事態を回避するためにも突.

場のニーズを捉え、時間依存性の分布パラメトリックの傾向は未知のデータを設定し、その地図上またはB-Treeとのユニ評価根れていないことが考えられます。また店舗のみを指数、ハッシュされた場合はご注文(感性表示にでき下位の削減にも時間ドメイン、実行時にすべての1の値です。以降、ゼロでない値以外のも少なく、明らかな候補者の方にはあらゆるデータ構造できない。データが得られない場合には設定は本当に広大な宇宙規模であることを何らかのsliding windowを管理するファイル/ディスク/persistent-io自分自身の可動部分のデータ範囲るものであります。(文字コードを理解することができ故が発生した場合には、直ちに、下するために努力している一方で、実際の解決策のワーキンググループが、いくの慈しみの消費者グレード経営システムにおいて唯一の目標を取ってくれてありがとうごランチから離れています。

こちらは比較的簡単なる実装を提供する必要があり、合理的な高速のルックアップ用のハッシュテーブル)などの高速繰り返し処理の非零要素により、行/列としています。

// Copyright 2014 Leo Osvald
//
// 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.

#ifndef UTIL_IMMUTABLE_SPARSE_MATRIX_HPP_
#define UTIL_IMMUTABLE_SPARSE_MATRIX_HPP_

#include <algorithm>
#include <limits>
#include <map>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>

// A simple time-efficient implementation of an immutable sparse matrix
// Provides efficient iteration of non-zero elements by rows/cols,
// e.g. to iterate over a range [row_from, row_to) x [col_from, col_to):
//   for (int row = row_from; row < row_to; ++row) {
//     for (auto col_range = sm.nonzero_col_range(row, col_from, col_to);
//          col_range.first != col_range.second; ++col_range.first) {
//       int col = *col_range.first;
//       // use sm(row, col)
//       ...
//     }
template<typename T = double, class Coord = int>
class SparseMatrix {
  struct PointHasher;
  typedef std::map< Coord, std::vector<Coord> > NonZeroList;
  typedef std::pair<Coord, Coord> Point;

 public:
  typedef T ValueType;
  typedef Coord CoordType;
  typedef typename NonZeroList::mapped_type::const_iterator CoordIter;
  typedef std::pair<CoordIter, CoordIter> CoordIterRange;

  SparseMatrix() = default;

  // Reads a matrix stored in MatrixMarket-like format, i.e.:
  // <num_rows> <num_cols> <num_entries>
  // <row_1> <col_1> <val_1>
  // ...
  // Note: the header (lines starting with '%' are ignored).
  template<class InputStream, size_t max_line_length = 1024>
  void Init(InputStream& is) {
    rows_.clear(), cols_.clear();
    values_.clear();

    // skip the header (lines beginning with '%', if any)
    decltype(is.tellg()) offset = 0;
    for (char buf[max_line_length + 1];
         is.getline(buf, sizeof(buf)) && buf[0] == '%'; )
      offset = is.tellg();
    is.seekg(offset);

    size_t n;
    is >> row_count_ >> col_count_ >> n;
    values_.reserve(n);
    while (n--) {
      Coord row, col;
      typename std::remove_cv<T>::type val;
      is >> row >> col >> val;
      values_[Point(--row, --col)] = val;
      rows_[col].push_back(row);
      cols_[row].push_back(col);
    }
    SortAndShrink(rows_);
    SortAndShrink(cols_);
  }

  const T& operator()(const Coord& row, const Coord& col) const {
    static const T kZero = T();
    auto it = values_.find(Point(row, col));
    if (it != values_.end())
      return it->second;
    return kZero;
  }

  CoordIterRange
  nonzero_col_range(Coord row, Coord col_from, Coord col_to) const {
    CoordIterRange r;
    GetRange(cols_, row, col_from, col_to, &r);
    return r;
  }

  CoordIterRange
  nonzero_row_range(Coord col, Coord row_from, Coord row_to) const {
    CoordIterRange r;
    GetRange(rows_, col, row_from, row_to, &r);
    return r;
  }

  Coord row_count() const { return row_count_; }
  Coord col_count() const { return col_count_; }
  size_t nonzero_count() const { return values_.size(); }
  size_t element_count() const { return size_t(row_count_) * col_count_; }

 private:
  typedef std::unordered_map<Point,
                             typename std::remove_cv<T>::type,
                             PointHasher> ValueMap;

  struct PointHasher {
    size_t operator()(const Point& p) const {
      return p.first << (std::numeric_limits<Coord>::digits >> 1) ^ p.second;
    }
  };

  static void SortAndShrink(NonZeroList& list) {
    for (auto& it : list) {
      auto& indices = it.second;
      indices.shrink_to_fit();
      std::sort(indices.begin(), indices.end());
    }

    // insert a sentinel vector to handle the case of all zeroes
    if (list.empty())
      list.emplace(Coord(), std::vector<Coord>(Coord()));
  }

  static void GetRange(const NonZeroList& list, Coord i, Coord from, Coord to,
                       CoordIterRange* r) {
    auto lr = list.equal_range(i);
    if (lr.first == lr.second) {
      r->first = r->second = list.begin()->second.end();
      return;
    }

    auto begin = lr.first->second.begin(), end = lr.first->second.end();
    r->first = lower_bound(begin, end, from);
    r->second = lower_bound(r->first, end, to);
  }

  ValueMap values_;
  NonZeroList rows_, cols_;
  Coord row_count_, col_count_;
};

#endif  /* UTIL_IMMUTABLE_SPARSE_MATRIX_HPP_ */

簡単のため、 immutable, が、本イベントは終了いたしました。で変更可能な;ください std::vectorstd::set したい場合は、合理的な効率化"挿入"(変更にゼロゼロ).

私はあなたがこのようなもの:

typedef std::tuple<int, int, int> coord_t;
typedef boost::hash<coord_t> coord_hash_t;
typedef std::unordered_map<coord_hash_t, int, c_hash_t> sparse_array_t;

sparse_array_t the_data;
the_data[ { x, y, z } ] = 1; /* list-initialization is cool */

for( const auto& element : the_data ) {
    int xx, yy, zz, val;
    std::tie( std::tie( xx, yy, zz ), val ) = element;
    /* ... */
}

お求めのデータの視点から解明するために書きクラスのサブクラス unorderd_map, は、反復子は、自動的にスキップ以上(消去)を付けた項目の値を0になります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top