Span.hpp
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   Span.hpp                                           :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2023/12/26 00:12:05 by gychoi            #+#    #+#             */
 9  /*   Updated: 2023/12/31 01:11:30 by gychoi           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #pragma once
14  #ifndef __SPAN_HPP__
15  #define __SPAN_HPP__
16  
17  #include <algorithm>
18  #include <cmath>
19  #include <deque>
20  #include <numeric>
21  #include <stdexcept>
22  #include <vector>
23  
24  class	Span
25  {
26  public:
27  	Span();
28  	Span(unsigned int n);
29  	Span(Span const& target);
30  	Span&				operator=(Span const& target);
31  	~Span();
32  
33  public:
34  	void				addNumber(int number);
35  	template<typename I>
36  	void				addNumbers(I first, I last);
37  	unsigned int		shortestSpan() const;
38  	unsigned int		longestSpan() const;
39  
40  private:
41  	unsigned int		mSize;
42  	std::vector<int>	mVec;
43  };
44  
45  template<typename I>
46  void	Span::addNumbers(I first, I last)
47  {
48  	int	dist = std::distance(first, last);
49  
50  	if (this->mVec.size() + dist > this->mSize)
51  	{
52  		throw std::runtime_error("Not enough vector space to store numbers");
53  	}
54  	else
55  	{
56  		this->mVec.insert(this->mVec.end(), first, last);
57  	}
58  }
59  
60  #endif /* __SPAN_HPP__ */