void SortedVectorMapRow<V>::ApplyIncUnsafe(int32_t column_id,
    const void *update) {
  // Go through the array and find column_id
  int32_t vector_idx = FindIndex(column_id);
  V typed_update = *(reinterpret_cast<const V*>(update));
  if (vector_idx != -1) {
    entries_[vector_idx].second += typed_update;

    // Remove vector_idx if vector_idx becomes 0.
    if (entries_[vector_idx].second == V(0)) {
      RemoveOneEntryAndCompact(vector_idx);
      return;
    }

    // Move vector_idx to maintain sorted order.
    bool forward = typed_update <= V(0);
    LinearSearchAndMove(vector_idx, forward);
    return;
  }
  // Add a new entry.
  if (num_entries_ == capacity_) {
    ResetCapacity(capacity_ + K_BLOCK_SIZE_);
  }

  entries_[num_entries_].first = column_id;
  entries_[num_entries_].second = typed_update;
  ++num_entries_;
  // Move new entry to maintain sorted order. Always move backward.
  LinearSearchAndMove(num_entries_ - 1, false);
}
示例#2
0
bool SortedVectorStore<V>::Inc(int32_t key, V delta, size_t *size) {
  if (delta == V(0)) return true;
  int32_t vector_idx = FindIndex(key);
  // Not found, insert it.
  if (vector_idx == -1) {
    if (num_entries_ == capacity_) {
      *size = capacity_ + kBlockSize;
      return false;
    }
    vector_idx = num_entries_;
    ++num_entries_;
    entries_[vector_idx].first = key;
    entries_[vector_idx].second = delta;

    // move backwards
    LinearSearchAndMove(vector_idx, false);
    return true;
  }

  // Found
  entries_[vector_idx].second += delta;

  if (entries_[vector_idx].second == V(0)) {
    RemoveOneEntryAndCompact(vector_idx);
  }

  return true;
}