<commit_before>// Copyright 2020 The Google Research Authors.
//
// 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.

// Generate a stream of inputs based on a given distribution, and sketch them.
// Report error statistics, time taken and memory used.

#include <chrono>
#include <cmath>
#include <cstdio>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include <gflags/gflags.h>

#include "absl/algorithm/container.h"
#include "absl/memory/memory.h"
#include "absl/random/bit_gen_ref.h"
#include "absl/random/uniform_int_distribution.h"
#include "absl/random/zipf_distribution.h"
#include "countmin.h"
#include "frequent.h"
#include "lossy_count.h"
#include "lossy_weight.h"
#include "sketch.h"
#include "utils.h"

DEFINE_int32(stream_size, 1000000, "Number of items in the stream");
DEFINE_int32(lg_stream_range, 20, "Stream elements in 0..2^log_stream_range");
DEFINE_string(distribution, "zipf", "Which distribution?");
DEFINE_double(zipf_param, 1.1, "Parameter for the Zipf distribution");
DEFINE_double(epsilon, 0.0001, "Heavy hitter fraction");
DEFINE_int32(hash_count, 5, "Number of hashes");
DEFINE_int32(hash_size, 2048, "Size of each hash");
DEFINE_int32(frequent_size, 2000, "Items in memory for Frequent (Misra-Gries)");

namespace sketch {

std::pair<std::vector<IntFloatPair>, std::vector<float>> CreateStream() {
  std::vector<IntFloatPair> data(FLAGS_stream_size);
  uint stream_range = 1 << FLAGS_lg_stream_range;
  std::vector<float> counts(stream_range, 0.0);
  BitGenerator bit_gen;
  absl::BitGenRef& gen = *bit_gen.BitGen();
  absl::zipf_distribution<uint> zipf(stream_range, FLAGS_zipf_param, 1.0);
  ULONG a = absl::uniform_int_distribution<int>(0, stream_range - 1)(gen);
  ULONG b = absl::uniform_int_distribution<int>(0, stream_range - 1)(gen);
  for (int i = 0; i < FLAGS_stream_size; ++i) {
    const auto& k = Hash(a, b, zipf(gen), stream_range);
    counts.at(k) += 1.0;
    data.emplace_back(k, 1.0);
  }
  return {data, counts};<commit_msg>Fix the bug of initializing data vector.<commit_after>// Copyright 2020 The Google Research Authors.
//
// 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.

// Generate a stream of inputs based on a given distribution, and sketch them.
// Report error statistics, time taken and memory used.

#include <chrono>
#include <cmath>
#include <cstdio>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include <gflags/gflags.h>

#include "absl/algorithm/container.h"
#include "absl/memory/memory.h"
#include "absl/random/bit_gen_ref.h"
#include "absl/random/uniform_int_distribution.h"
#include "absl/random/zipf_distribution.h"
#include "countmin.h"
#include "frequent.h"
#include "lossy_count.h"
#include "lossy_weight.h"
#include "sketch.h"
#include "utils.h"

DEFINE_int32(stream_size, 1000000, "Number of items in the stream");
DEFINE_int32(lg_stream_range, 20, "Stream elements in 0..2^log_stream_range");
DEFINE_string(distribution, "zipf", "Which distribution?");
DEFINE_double(zipf_param, 1.1, "Parameter for the Zipf distribution");
DEFINE_double(epsilon, 0.0001, "Heavy hitter fraction");
DEFINE_int32(hash_count, 5, "Number of hashes");
DEFINE_int32(hash_size, 2048, "Size of each hash");
DEFINE_int32(frequent_size, 2000, "Items in memory for Frequent (Misra-Gries)");

namespace sketch {

std::pair<std::vector<IntFloatPair>, std::vector<float>> CreateStream() {
  std::vector<IntFloatPair> data;
  uint stream_range = 1 << FLAGS_lg_stream_range;
  std::vector<float> counts(stream_range, 0.0);
  BitGenerator bit_gen;
  absl::BitGenRef& gen = *bit_gen.BitGen();
  absl::zipf_distribution<uint> zipf(stream_range, FLAGS_zipf_param, 1.0);
  ULONG a = absl::uniform_int_distribution<int>(0, stream_range - 1)(gen);
  ULONG b = absl::uniform_int_distribution<int>(0, stream_range - 1)(gen);
  for (int i = 0; i < FLAGS_stream_size; ++i) {
    const auto& k = Hash(a, b, zipf(gen), stream_range);
    counts.at(k) += 1.0;
    data.emplace_back(k, 1.0);
  }
  return {data, counts};
{'fraction_non_alphanumeric': 0.13013428120063192, 'fraction_numerical': 0.02330173775671406, 'mean_word_length': 5.207107843137255, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4300996', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1659, 'n_words': 503}