#ifndef MARISA_INTVECTOR_H_
#define MARISA_INTVECTOR_H_

#include "vector.h"

namespace marisa {

class IntVector {
 public:
  IntVector();

  void build(const Vector<UInt32> &ints);
  void build(UInt32 max_int, std::size_t size);

  void mmap(Mapper *mapper, const char *filename,
      long offset = 0, int whence = SEEK_SET);
  void map(const void *ptr, std::size_t size);
  void map(Mapper &mapper);

  void load(const char *filename,
      long offset = 0, int whence = SEEK_SET);
  void fread(std::FILE *file);
  void read(int fd);
  void read(std::istream &stream);
  void read(Reader &reader);

  void save(const char *filename, bool trunc_flag = true,
      long offset = 0, int whence = SEEK_SET) const;
  void fwrite(std::FILE *file) const;
  void write(int fd) const;
  void write(std::ostream &stream) const;
  void write(Writer &writer) const;

  void set(std::size_t i, UInt32 value) {
    MARISA_DEBUG_IF(i >= size_, MARISA_PARAM_ERROR);
    std::size_t pos = i * num_bits_per_int_;
    std::size_t unit_id = pos / 32;
    std::size_t unit_offset = pos % 32;
    units_[unit_id] &= ~(mask_ << unit_offset);
    units_[unit_id] |= (value & mask_) << unit_offset;
    if ((unit_offset + num_bits_per_int_) > 32) {
      units_[unit_id + 1] &= ~(mask_ >> (32 - unit_offset));
      units_[unit_id + 1] |= (value & mask_) >> (32 - unit_offset);
    }
  }

  UInt32 get(std::size_t i) const {
    MARISA_DEBUG_IF(i >= size_, MARISA_PARAM_ERROR);
    std::size_t pos = i * num_bits_per_int_;
    std::size_t unit_id = pos / 32;
    std::size_t unit_offset = pos % 32;
    if ((unit_offset + num_bits_per_int_) <= 32) {
      return (units_[unit_id] >> unit_offset) & mask_;
    } else {
      return ((units_[unit_id] >> unit_offset)
          | (units_[unit_id + 1] << (32 - unit_offset))) & mask_;
    }
  }

  UInt32 operator[](std::size_t i) const {
    MARISA_DEBUG_IF(i >= size_, MARISA_PARAM_ERROR);
    return get(i);
  }

  std::size_t num_bits_per_int() const {
    return num_bits_per_int_;
  }
  UInt32 mask() const {
    return mask_;
  }
  std::size_t size() const {
    return size_;
  }
  bool empty() const {
    return size_ == 0;
  }
  std::size_t total_size() const {
    return units_.total_size() + sizeof(num_bits_per_int_)
        + sizeof(mask_) + sizeof(size_);
  }

  void clear();
  void swap(IntVector *rhs);

 private:
  Vector<UInt32> units_;
  UInt32 num_bits_per_int_;
  UInt32 mask_;
  UInt32 size_;

  // Disallows copy and assignment.
  IntVector(const IntVector &);
  IntVector &operator=(const IntVector &);
};

}  // namespace marisa

#endif  // MARISA_INTVECTOR_H_
