std::vector<int> compute_buckets(const T *s, int n, int k) const {
		std::vector<int> buckets(k);
		for(int i = 0; i < n; ++i){ ++buckets[s[i]]; }
		for(int i = 0, sum = 0; i < k; ++i){
			sum += buckets[i];
			buckets[i] = (End ? sum : (sum - buckets[i]));
		}
		return buckets;
	}
예제 #2
0
void InMemoryStorageBackend::updateBucket(const PolicyBucketId &bucketId,
                                          const PolicyResult &defaultPolicy) {
    try {
        auto &bucket = buckets().at(bucketId);
        bucket.setDefaultPolicy(defaultPolicy);
    } catch (const std::out_of_range &) {
        throw BucketNotExistsException(bucketId);
    }
}
예제 #3
0
PolicyBucket::Policies InMemoryStorageBackend::listPolicies(const PolicyBucketId &bucketId,
                                                            const PolicyKey &filter) const {
    try {
        auto &bucket = buckets().at(bucketId);
        return bucket.listPolicies(filter);
    } catch (const std::out_of_range &) {
        throw BucketNotExistsException(bucketId);
    }
}
예제 #4
0
void InMemoryStorageBackend::dumpDatabase(const std::shared_ptr<std::ofstream> &chsStream) {
    auto indexStream = std::make_shared<ChecksumStream>(m_indexFilename, chsStream);
    std::string indexFilename = m_dbPath + m_indexFilename;
    openDumpFileStream<ChecksumStream>(*indexStream, indexFilename + m_backupFilenameSuffix);

    StorageSerializer<ChecksumStream> storageSerializer(indexStream);
    storageSerializer.dump(buckets(), std::bind(&InMemoryStorageBackend::bucketDumpStreamOpener,
                           this, std::placeholders::_1, chsStream));
}
예제 #5
0
void InMemoryStorageBackend::deletePolicy(const PolicyBucketId &bucketId, const PolicyKey &key) {
    try {
        // TODO: Move the erase code to PolicyCollection maybe?
        auto &bucket = buckets().at(bucketId);
        bucket.deletePolicy(key);
    } catch (const std::out_of_range &) {
        throw BucketNotExistsException(bucketId);
    }
}
	void induce_sa_s(
		int *sa, const T *s, int n, int k,
		const std::vector<bool> &types) const
	{
		std::vector<int> buckets(compute_buckets<true>(s, n, k));
		for(int i = n - 1; i >= 0; --i){
			const int j = sa[i] - 1;
			if(j >= 0 && types[j]){ sa[--buckets[s[j]]] = j; }
		}
	}
	void induce_sa_l(
		int *sa, const T *s, int n, int k,
		const std::vector<bool> &types) const
	{
		std::vector<int> buckets(compute_buckets<false>(s, n, k));
		for(int i = 0; i < n; ++i){
			const int j = sa[i] - 1;
			if(j >= 0 && !types[j]){ sa[buckets[s[j]]++] = j; }
		}
	}
예제 #8
0
  void operator()(const Graph& g, ApplyForce apply_force)
  {
    typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
    typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
    typedef std::list<vertex_descriptor> bucket_t;
    typedef std::vector<bucket_t> buckets_t;

    std::size_t columns = std::size_t(topology.extent()[0] / two_k + 1.);
    std::size_t rows = std::size_t(topology.extent()[1] / two_k + 1.);
    buckets_t buckets(rows * columns);
    vertex_iterator v, v_end;
    for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v) {
      std::size_t column =
        std::size_t((get(position, *v)[0] + topology.extent()[0] / 2) / two_k);
      std::size_t row    =
        std::size_t((get(position, *v)[1] + topology.extent()[1] / 2) / two_k);

      if (column >= columns) column = columns - 1;
      if (row >= rows) row = rows - 1;
      buckets[row * columns + column].push_back(*v);
    }

    for (std::size_t row = 0; row < rows; ++row)
      for (std::size_t column = 0; column < columns; ++column) {
        bucket_t& bucket = buckets[row * columns + column];
        typedef typename bucket_t::iterator bucket_iterator;
        for (bucket_iterator u = bucket.begin(); u != bucket.end(); ++u) {
          // Repulse vertices in this bucket
          bucket_iterator v = u;
          for (++v; v != bucket.end(); ++v) {
            apply_force(*u, *v);
            apply_force(*v, *u);
          }

          std::size_t adj_start_row = row == 0? 0 : row - 1;
          std::size_t adj_end_row = row == rows - 1? row : row + 1;
          std::size_t adj_start_column = column == 0? 0 : column - 1;
          std::size_t adj_end_column = column == columns - 1? column : column + 1;
          for (std::size_t other_row = adj_start_row; other_row <= adj_end_row;
               ++other_row)
            for (std::size_t other_column = adj_start_column;
                 other_column <= adj_end_column; ++other_column)
              if (other_row != row || other_column != column) {
                // Repulse vertices in this bucket
                bucket_t& other_bucket
                  = buckets[other_row * columns + other_column];
                for (v = other_bucket.begin(); v != other_bucket.end(); ++v) {
                  double dist =
                    topology.distance(get(position, *u), get(position, *v));
                  if (dist < two_k) apply_force(*u, *v);
                }
              }
        }
      }
  }
예제 #9
0
void outlines_to_blobs(               // find blobs
                       BLOCK *block,  // block to scan
                       ICOORD bleft,
                       ICOORD tright,
                       C_OUTLINE_LIST *outlines) {
                                 // make buckets
  OL_BUCKETS buckets(bleft, tright);

  fill_buckets(outlines, &buckets);
  empty_buckets(block, &buckets);
}
예제 #10
0
HOT_FUNC
void ImmutableMap::addVal(int pos, int hash_pos,
                          CVarRef val, bool unserializeObj) {
  // NOTE: no check on duplication because we assume the original array has no
  // duplication
  Bucket* bucket = buckets() + pos;
  new (&bucket->val) SharedVariant(val, false, true, unserializeObj);
  int& hp = hash()[hash_pos];
  bucket->next = hp;
  hp = pos;
}
예제 #11
0
HOT_FUNC
int ImmutableMap::indexOf(int64_t key) {
  int bucket = hash()[key & m.m_capacity_mask];
  Bucket* b = buckets();
  while (bucket != -1) {
    if (b[bucket].hasIntKey() && key == b[bucket].ikey) {
      return bucket;
    }
    bucket = b[bucket].next;
  }
  return -1;
}
예제 #12
0
ssize_t APCArray::indexOf(int64_t key) const {
  ssize_t bucket = hash()[key & m.m_capacity_mask];
  Bucket* b = buckets();
  while (bucket != -1) {
    if (b[bucket].key->type() == KindOfInt64 &&
        key == APCTypedValue::fromHandle(b[bucket].key)->getInt64()) {
      return bucket;
    }
    bucket = b[bucket].next;
  }
  return -1;
}
예제 #13
0
void InMemoryStorageBackend::load(void) {
    bool isBackupValid = m_integrity.backupGuardExists();
    std::string bucketSuffix = "";
    std::string indexFilename = m_dbPath + m_indexFilename;
    std::string chsFilename = m_dbPath + m_chsFilename;

    if (isBackupValid) {
        bucketSuffix += m_backupFilenameSuffix;
        indexFilename += m_backupFilenameSuffix;
        chsFilename += m_backupFilenameSuffix;
    }

    try {
        std::ifstream chsStream;
        openFileStream(chsStream, chsFilename, isBackupValid);
        m_checksum.load(chsStream);

        auto indexStream = std::make_shared<std::ifstream>();
        openFileStream(*indexStream, indexFilename, isBackupValid);

        StorageDeserializer storageDeserializer(indexStream,
            std::bind(&InMemoryStorageBackend::bucketStreamOpener, this,
                      std::placeholders::_1, bucketSuffix, isBackupValid));

        storageDeserializer.initBuckets(buckets());
        storageDeserializer.loadBuckets(buckets());
    } catch (const DatabaseException &) {
        LOGC("Reading cynara database failed.");
        buckets().clear();
        throw DatabaseCorruptedException();
    }
    m_checksum.clear();

    if (!hasBucket(defaultPolicyBucketId)) {
        LOGN("Creating defaultBucket.");
        this->buckets().insert({ defaultPolicyBucketId, PolicyBucket(defaultPolicyBucketId) });
    }

    postLoadCleanup(isBackupValid);
}
예제 #14
0
ssize_t APCArray::indexOf(int64_t key) const {
  Bucket* b = buckets();
  for (ssize_t bucket = hash()[key & m.m_capacity_mask]; bucket != -1;
       bucket = b[bucket].next) {
    auto key_handle = b[bucket].key;
    auto kind = key_handle->kind();
    if (kind == APCKind::Int &&
        key == APCTypedValue::fromHandle(key_handle)->getInt64()) {
      return bucket;
    }
  }
  return -1;
}
예제 #15
0
// -----------------------------------------------------------------------------
void Algorithms::BucketSort(std::vector<int> & v)
{
    size_t v_length = v.size();

    if (v_length <= 1) {
        // No need to sort the vector if it has no or just one element.
        return;
    }

    // Each bucket contains 10 elements at most.
    const size_t BUCKET_SIZE = 10;

    // Determine the value range.
    int min_value = std::numeric_limits<int>::max();
    int max_value = std::numeric_limits<int>::min();
    for (size_t i = 0; i < v_length; ++i) {
        if (v[i] < min_value) {
            min_value = v[i];
        }
        if (v[i] > max_value) {
            max_value = v[i];
        }
    }

    // Special case: If min_value == max_value, that means v contains the
    // elements of the same value. It's already sorted. Just return.
    if (min_value == max_value) {
        return;
    }

    const size_t BUCKET_COUNT = (max_value - min_value) / BUCKET_SIZE + 1;

    std::vector< std::vector<int> > buckets(BUCKET_COUNT);

    // Distribute the elements of v into the buckets.
    for (size_t i = 0; i < v_length; ++i) {
        int bi = (v[i] - min_value) / BUCKET_SIZE;
        buckets[bi].push_back(v[i]);
    }

    // Sort each bucket and put the sorted elements back to the original v.
    size_t vi = 0;
    for (size_t bi = 0; bi < BUCKET_COUNT; ++bi) {
        std::vector<int> & bucket = buckets[bi];
        std::sort(bucket.begin(), bucket.end());
        for (size_t j = 0; j < bucket.size(); ++j) {
            v[vi] = bucket[j];
            ++vi;
        }
    }
}
예제 #16
0
HOT_FUNC
int ImmutableMap::indexOf(const StringData* key) {
  strhash_t h = STR_HASH(key->hash());
  int bucket = hash()[h & m.m_capacity_mask];
  Bucket* b = buckets();
  while (bucket != -1) {
    Bucket* cand = &b[bucket];
    if (cand->hash() == h && (cand->skey == key || key->same(cand->skey))) {
      return bucket;
    }
    bucket = b[bucket].next;
  }
  return -1;
}
예제 #17
0
APCArray::~APCArray() {
  if (isPacked()) {
    APCHandle** v = vals();
    for (size_t i = 0, n = m_size; i < n; i++) {
      v[i]->unreference();
    }
  } else {
    Bucket* bks = buckets();
    for (int i = 0; i < m.m_num; i++) {
      bks[i].key->unreference();
      bks[i].val->unreference();
    }
  }
}
예제 #18
0
파일: cache.cpp 프로젝트: CUITCHE/TagBuf
struct bucket_t *cache_t::find(cache_key_t key)
{
    assert(key != 0);

    bucket_t *b = buckets();
    mask_t m = mask();
    mask_t begin = cache_hash(key, m);
    mask_t i = begin;
    do {
        if (b[i].key() == 0  ||  b[i].key() == key) {
            return &b[i];
        }
    } while ((i = cache_next(i, m)) != begin);
    return nullptr;
}
예제 #19
0
  int maximumGap(vector<int>& num){
    if(num.empty() || num.size() < 2) return 0;
    int gap = 0;

    int maxNum = num[0];
    int minNum = num[0];
    for(int i = 0; i < num.size(); i++){
      maxNum = max(maxNum, num[i]);
      minNum = min(minNum, num[i]);
    }

    // brucket's size: !!!!
    int len = (maxNum - minNum) / num.size() + 1;

    // num of buckets: (a- b)/len + 1
    // each bucket stores the min and max values. only two nums
    int n = (maxNum - minNum) / len + 1;
    vector<vector<int>> buckets(n);
    // vector<pair<int, int>> buckets(n);
    for(auto &x : num){
      // (k - min) / len
      int i = (x - minNum) / len;
      if(buckets[i].empty()){
        buckets[i].reserve(2);
        buckets[i].push_back(x);
        buckets[i].push_back(x);
      }else{
        if(x < buckets[i][0]) buckets[i][0] = x;
        if(x > buckets[i][1]) buckets[i][1] = x;
      }
      /*
      buckets[i].reserve(2);
      buckets[i][0] = min(buckets[i][0], x);
      buckets[i][1] = max(buckets[i][1], x);
      */
    }

    // for each non-empty buckets p, find the next bucktes q.
    // return min(q.min - p.max)
    int prev = 0; // buckets: 1, 3, 4, 6 not consecutive.
    for(int i = 1; i < buckets.size(); i++){
      if(buckets[i].empty()) continue;
      gap = max(gap, buckets[i][0] - buckets[prev][1]); // 后面最小,前面最大
      prev = i;
    }
    return gap;

  }
예제 #20
0
APCArray::~APCArray() {
  // This array is refcounted, but keys/values might be uncounted strings, so
  // we must use unreferenceRoot here (corresponding to Create calls above).
  if (isPacked()) {
    APCHandle** v = vals();
    for (size_t i = 0, n = m_size; i < n; i++) {
      v[i]->unreferenceRoot();
    }
  } else {
    Bucket* bks = buckets();
    for (int i = 0; i < m.m_num; i++) {
      bks[i].key->unreferenceRoot();
      bks[i].val->unreferenceRoot();
    }
  }
}
예제 #21
0
    int maximumGap(const std::vector<int>& nums)
    {
        auto sz = nums.size();
        if(sz<2) return 0;

        //get global max, min
        int g_max = nums[0];
        int g_min = nums[0];
        for(auto x : nums)
        {
            if(x < g_min)
                g_min = x;
            else if(x > g_max)
                g_max = x;
        }

        //get num of buckets
        size_t buckets_len = (g_max-g_min)/sz+1;
        size_t buckets_num = (g_max-g_min)/buckets_len+1;
        std::vector<std::vector<int>> buckets(buckets_num);

        for(int x : nums)
        {
            int loc = (x-g_min)/buckets_len;
            if(buckets[loc].empty())
            {
                //more effective
                buckets[loc].reserve(2);
                buckets[loc].push_back(x);
                buckets[loc].push_back(x);
            }
            else
            {
                buckets[loc][0] = std::min(buckets[loc][0], x);
                buckets[loc][1] = std::max(buckets[loc][1], x);
            }
        }

        int max_gap = 0;
        for(size_t i=0, prev=0; i<buckets.size(); ++i)
        {
            if(buckets[i].empty()) continue;
            max_gap = std::max(max_gap, buckets[i][0]-buckets[prev][1]);
            prev = i;
        }
        return max_gap;
    }
예제 #22
0
파일: sta_util.cpp 프로젝트: kmurray/tatum
void print_histogram(const std::vector<float>& values, int nbuckets) {
    nbuckets = std::min(values.size(), (size_t) nbuckets);
    int values_per_bucket = ceil((float) values.size() / nbuckets);

    std::vector<float> buckets(nbuckets);

    //Sum up each bucket
    for(size_t i = 0; i < values.size(); i++) {
        int ibucket = i / values_per_bucket;

        buckets[ibucket] += values[i];
    }

    //Normalize to get average value
    for(int i = 0; i < nbuckets; i++) {
        buckets[i] /= values_per_bucket;
    }

    float max_bucket_val = *std::max_element(buckets.begin(), buckets.end());

    //Print the histogram
    std::ios_base::fmtflags saved_flags = cout.flags();
    std::streamsize prec = cout.precision();
    std::streamsize width = cout.width();

    std::streamsize int_width = ceil(log10(values.size()));
    std::streamsize float_prec = 1;

    int histo_char_width = 60;

    //cout << "\t" << endl;
    for(int i = 0; i < nbuckets; i++) {
        cout << std::setw(int_width) << i*values_per_bucket << ":" << std::setw(int_width) << (i+1)*values_per_bucket - 1;
        cout << " " <<  std::scientific << std::setprecision(float_prec) << buckets[i];
        cout << " ";

        for(int j = 0; j < histo_char_width*(buckets[i]/max_bucket_val); j++) {
            cout << "*";
        }
        cout << endl;
    }

    cout.flags(saved_flags);
    cout.precision(prec);
    cout.width(width);
}
예제 #23
0
       /**
        * Buckets/hashes each column based on random uniform probability
        * @param num_buckets number of partitions
        * @return vector representing hashing of columns
        */
        std::vector<std::vector<int> > bucket(const int num_buckets) const {
            if(row < 0 || col < 0) {
                throw std::invalid_argument("Number of buckets must be less than or equal to the number of columns");
            } else {
                std::vector<std::vector<int> > buckets(num_buckets);
                std::random_device rd;
                std::mt19937 gen(rd());
                std::uniform_int_distribution<> dis(0, num_buckets-1);

                for(int i = 0; i < this->num_cols(); i++){
                    int bkt = dis(gen);
                    buckets[bkt].push_back(i);
                }

                return buckets;
            }
        }
예제 #24
0
void InMemoryStorageBackend::deleteLinking(const PolicyBucketId &bucketId) {
    auto bucketIdMatches = [&bucketId] (PolicyPtr policy) -> bool {
        auto policyResult = policy->result();

        // Check bucket id only if policy is a bucket policy
        // TODO: Maybe move the test to PolicyResult
        if (policyResult.policyType() == PredefinedPolicyType::BUCKET) {
            return policyResult.metadata() == bucketId;
        }
        return false;
    };

    for (auto &bucketIter : buckets()) {
        auto &bucket = bucketIter.second;
        bucket.deletePolicy(bucketIdMatches);
    }
}
예제 #25
0
       int maximumGap(vector<int> &num) 
       {
        if(num.size() < 2)
          return 0;
        int min_value = num[0];
        int max_value = num[0];
        for(vector<int>::iterator iter = num.begin(); iter != num.end(); iter++) 
        {
            min_value = min(*iter, min_value);
            max_value = max(*iter, max_value);
        }
        int range = ceil(double(max_value - min_value) / (num.size() - 1));
        vector<vector<int> >buckets(num.size());
        for(vector<int>::iterator iter = num.begin(); iter != num.end(); iter++) 
        {
            int index = (*iter - min_value) / range;
            if(buckets[index].empty()) 
            {
                buckets[index].push_back(*iter);
                buckets[index].push_back(*iter);

            } 
            else 
            {
                if(*iter < buckets[index][0]) 
                {
                    buckets[index][0] = *iter;
                }
                if(*iter > buckets[index][1]) 
                {
                    buckets[index][1] = *iter;
                }
            }
        }
        int gap = 0;
         int pre = 0;
        for(int i = 1; i < buckets.size(); i++) 
        {
            if(buckets[i].empty())
              continue;
            gap = max(gap, buckets[i][0] - buckets[pre][1]);
            pre = i;
        }
        return gap;
    }
예제 #26
0
파일: 347.cpp 프로젝트: roneilPMH/LeetCode
 vector<int> topKFrequent(vector<int>& nums, int k) {
     int len = (int) nums.size();
     if (k > len) return nums;
     unordered_map<int, int> cache;
     for (int n : nums) cache[n]++;
     vector<vector<int>> buckets(len + 1);
     for (auto p : cache) {
         buckets[p.second].push_back(p.first);
     }
     vector<int> res;
     for (int i = len, j = 0; i > -1 && j <= k; i--) {
         for (int n : buckets[i]) {
             res.push_back(n);
             j++;
             if (j == k) return res;
         }
     }
     return res;
 }
예제 #27
0
 vector<int> topKFrequent(vector<int>& nums, int k) {
     unordered_map<int, int> m;
     for (int num : nums)
         ++m[num];
     
     vector<vector<int>> buckets(nums.size() + 1); 
     for (auto p : m)
         buckets[p.second].push_back(p.first);
     
     vector<int> ans;
     for (int i = buckets.size() - 1; i >= 0 && ans.size() < k; --i) {
         for (int num : buckets[i]) {
             ans.push_back(num);
             if (ans.size() == k)
                 break;
         }
     }
     return ans;
 }
예제 #28
0
ssize_t APCArray::indexOf(const StringData* key) const {
  strhash_t h = key->hash();
  Bucket* b = buckets();
  for (ssize_t bucket = hash()[h & m.m_capacity_mask]; bucket != -1;
       bucket = b[bucket].next) {
    auto kind = b[bucket].key->kind();
    if (kind == APCKind::StaticString || kind == APCKind::UncountedString) {
      auto const k = APCTypedValue::fromHandle(b[bucket].key);
      if (key->same(k->getStringData())) {
        return bucket;
      }
    } else if (kind == APCKind::SharedString) {
      auto const k = APCString::fromHandle(b[bucket].key);
      if (key->same(k->getStringData())) {
        return bucket;
      }
    }
  }
  return -1;
}
예제 #29
0
void InMemoryStorageBackend::erasePolicies(const PolicyBucketId &bucketId, bool recursive,
                                           const PolicyKey &filter) {
    PolicyBucket::BucketIds bucketIds = {bucketId};

    while (!bucketIds.empty()) {
        auto it = bucketIds.begin();
        PolicyBucketId bucketId = *it;
        bucketIds.erase(it);
        try {
            auto &policyBucket = buckets().at(bucketId);
            if (recursive) {
                auto subBuckets = policyBucket.getSubBuckets();
                bucketIds.insert(subBuckets.begin(), subBuckets.end());
            }
            policyBucket.deletePolicy([&filter] (PolicyPtr policy) -> bool {
                 return policy->key().matchFilter(filter);
            });
        } catch (const std::out_of_range &) {
            throw BucketNotExistsException(bucketId);
        }
    }
}
예제 #30
0
void APCArray::add(APCHandle *key, APCHandle *val) {
  int hash_pos;
  auto kind = key->kind();
  if (kind == APCKind::Int) {
    hash_pos = APCTypedValue::fromHandle(key)->getInt64();
  } else if (kind == APCKind::StaticString ||
             kind == APCKind::UncountedString) {
    hash_pos = APCTypedValue::fromHandle(key)->getStringData()->hash();
  } else {
    assert(kind == APCKind::SharedString);
    hash_pos = APCString::fromHandle(key)->getStringData()->hash();
  }
  // NOTE: no check on duplication because we assume the original array has no
  // duplication
  int& hp = hash()[hash_pos & m.m_capacity_mask];
  int pos = m.m_num++;
  Bucket* bucket = buckets() + pos;
  bucket->key = key;
  bucket->val = val;
  bucket->next = hp;
  hp = pos;
}