데이터 구조
데이터 구조 (Data Structures, ds)
PHP 7에서 배열(array)의 대안으로 쓰는 효율적인 데이터 구조 확장이에요. 벡터, 데크, 맵, 셋, 스택, 큐 같은 자료구조를 성능에 맞게 제공해요. 배열로 하면 부담스러웠던 작업을 더 효율적으로 처리하고 싶을 때 써요.
출처: Data Structures
본문
소개
이 확장은 PHP 7용 효율적인 데이터 구조를 제공하며, 배열의 대안으로 설계됐어요. 벤치마크, 논의, 자주 묻는 질문은 공식 블로그 포스트를 참고하면 돼요.
제공하는 구조는 대략 이렇게 나뉘어요.
- Collection / Sequence 인터페이스: 공통 동작을 정의하는 기반 인터페이스
- Vector: 연속 메모리 배열, 인덱스 접근이 빠른 벡터
- Deque: 앞뒤 양쪽에서 빠르게 넣고 빼는 데크
- Map: 키-값 연관 배열
- Set: 중복 없는 집합
- Stack: LIFO 스택
- Queue / PriorityQueue: FIFO 큐와 우선순위 큐
- Pair: 키-값 쌍
대부분의 작업이 배열에 비해 메모리 사용과 속도 면에서 더 효율적이라, 대량 데이터를 다룰 때 특히 유용해요.
인터페이스·클래스 참조
이 페이지의 전체 API 목록은 아래와 같아요. (메서드는 각 클래스에서 공통 구현되어 유사한 이름을 공유해요.)
Ds\Collection / Ds\Hashable 인터페이스
Ds\Collection::clear— Removes all valuesDs\Collection::copy— Returns a shallow copy of the collectionDs\Collection::isEmpty— Returns whether the collection is emptyDs\Collection::toArray— Converts the collection to an arrayDs\Hashable::equals— Determines whether an object is equal to the current instanceDs\Hashable::hash— Returns a scalar value to be used as a hash value
Ds\Sequence 인터페이스
Ds\Sequence::allocate— Allocates enough memory for a required capacityDs\Sequence::apply— Updates all values by applying a callback function to each valueDs\Sequence::capacity— Returns the current capacityDs\Sequence::contains— Determines if the sequence contains given valuesDs\Sequence::filter— Creates a new sequence using a callable to determine which values to includeDs\Sequence::find— Attempts to find a value's indexDs\Sequence::first— Returns the first value in the sequenceDs\Sequence::get— Returns the value at a given indexDs\Sequence::insert— Inserts values at a given indexDs\Sequence::join— Joins all values together as a stringDs\Sequence::last— Returns the last valueDs\Sequence::map— Returns the result of applying a callback to each valueDs\Sequence::merge— Returns the result of adding all given values to the sequenceDs\Sequence::pop— Removes and returns the last valueDs\Sequence::push— Adds values to the end of the sequenceDs\Sequence::reduce— Reduces the sequence to a single value using a callback functionDs\Sequence::remove— Removes and returns a value by indexDs\Sequence::reverse— Reverses the sequence in-placeDs\Sequence::reversed— Returns a reversed copyDs\Sequence::rotate— Rotates the sequence by a given number of rotationsDs\Sequence::set— Updates a value at a given indexDs\Sequence::shift— Removes and returns the first valueDs\Sequence::slice— Returns a sub-sequence of a given rangeDs\Sequence::sort— Sorts the sequence in-placeDs\Sequence::sorted— Returns a sorted copyDs\Sequence::sum— Returns the sum of all values in the sequenceDs\Sequence::unshift— Adds values to the front of the sequence
Ds\Vector 클래스
Ds\Vector::allocate— Allocates enough memory for a required capacityDs\Vector::apply— Updates all values by applying a callback function to each valueDs\Vector::capacity— Returns the current capacityDs\Vector::clear— Removes all valuesDs\Vector::__construct— Creates a new instanceDs\Vector::contains— Determines if the vector contains given valuesDs\Vector::copy— Returns a shallow copy of the vectorDs\Vector::count— Returns the number of values in the collectionDs\Vector::filter— Creates a new vector using a callable to determine which values to includeDs\Vector::find— Attempts to find a value's indexDs\Vector::first— Returns the first value in the vectorDs\Vector::get— Returns the value at a given indexDs\Vector::insert— Inserts values at a given indexDs\Vector::isEmpty— Returns whether the vector is emptyDs\Vector::join— Joins all values together as a stringDs\Vector::jsonSerialize— Returns a representation that can be converted to JSONDs\Vector::last— Returns the last valueDs\Vector::map— Returns the result of applying a callback to each valueDs\Vector::merge— Returns the result of adding all given values to the vectorDs\Vector::pop— Removes and returns the last valueDs\Vector::push— Adds values to the end of the vectorDs\Vector::reduce— Reduces the vector to a single value using a callback functionDs\Vector::remove— Removes and returns a value by indexDs\Vector::reverse— Reverses the vector in-placeDs\Vector::reversed— Returns a reversed copyDs\Vector::rotate— Rotates the vector by a given number of rotationsDs\Vector::set— Updates a value at a given indexDs\Vector::shift— Removes and returns the first valueDs\Vector::slice— Returns a sub-vector of a given rangeDs\Vector::sort— Sorts the vector in-placeDs\Vector::sorted— Returns a sorted copyDs\Vector::sum— Returns the sum of all values in the vectorDs\Vector::toArray— Converts the vector to an arrayDs\Vector::unshift— Adds values to the front of the vector
Ds\Deque 클래스
Ds\Deque::allocate— Allocates enough memory for a required capacityDs\Deque::apply— Updates all values by applying a callback function to each valueDs\Deque::capacity— Returns the current capacityDs\Deque::clear— Removes all values from the dequeDs\Deque::__construct— Creates a new instanceDs\Deque::contains— Determines if the deque contains given valuesDs\Deque::copy— Returns a shallow copy of the dequeDs\Deque::count— Returns the number of values in the collectionDs\Deque::filter— Creates a new deque using a callable to determine which values to includeDs\Deque::find— Attempts to find a value's indexDs\Deque::first— Returns the first value in the dequeDs\Deque::get— Returns the value at a given indexDs\Deque::insert— Inserts values at a given indexDs\Deque::isEmpty— Returns whether the deque is emptyDs\Deque::join— Joins all values together as a stringDs\Deque::jsonSerialize— Returns a representation that can be converted to JSONDs\Deque::last— Returns the last valueDs\Deque::map— Returns the result of applying a callback to each valueDs\Deque::merge— Returns the result of adding all given values to the dequeDs\Deque::pop— Removes and returns the last valueDs\Deque::push— Adds values to the end of the dequeDs\Deque::reduce— Reduces the deque to a single value using a callback functionDs\Deque::remove— Removes and returns a value by indexDs\Deque::reverse— Reverses the deque in-placeDs\Deque::reversed— Returns a reversed copyDs\Deque::rotate— Rotates the deque by a given number of rotationsDs\Deque::set— Updates a value at a given indexDs\Deque::shift— Removes and returns the first valueDs\Deque::slice— Returns a sub-deque of a given rangeDs\Deque::sort— Sorts the deque in-placeDs\Deque::sorted— Returns a sorted copyDs\Deque::sum— Returns the sum of all values in the dequeDs\Deque::toArray— Converts the deque to an arrayDs\Deque::unshift— Adds values to the front of the deque
Ds\Map 클래스
Ds\Map::allocate— Allocates enough memory for a required capacityDs\Map::apply— Updates all values by applying a callback function to each valueDs\Map::capacity— Returns the current capacityDs\Map::clear— Removes all valuesDs\Map::__construct— Creates a new instanceDs\Map::copy— Returns a shallow copy of the mapDs\Map::count— Returns the number of values in the mapDs\Map::diff— Creates a new map using keys that aren't in another mapDs\Map::filter— Creates a new map using a callable to determine which pairs to includeDs\Map::first— Returns the first pair in the mapDs\Map::get— Returns the value for a given keyDs\Map::hasKey— Determines whether the map contains a given keyDs\Map::hasValue— Determines whether the map contains a given valueDs\Map::intersect— Creates a new map by intersecting keys with another mapDs\Map::isEmpty— Returns whether the map is emptyDs\Map::jsonSerialize— Returns a representation that can be converted to JSONDs\Map::keys— Returns a set of the map's keysDs\Map::ksort— Sorts the map in-place by keyDs\Map::ksorted— Returns a copy, sorted by keyDs\Map::last— Returns the last pair of the mapDs\Map::map— Returns the result of applying a callback to each valueDs\Map::merge— Returns the result of adding all given associationsDs\Map::pairs— Returns a sequence containing all the pairs of the mapDs\Map::put— Associates a key with a valueDs\Map::putAll— Associates all key-value pairs of a traversable object or arrayDs\Map::reduce— Reduces the map to a single value using a callback functionDs\Map::remove— Removes and returns a value by keyDs\Map::reverse— Reverses the map in-placeDs\Map::reversed— Returns a reversed copyDs\Map::skip— Returns the pair at a given positional indexDs\Map::slice— Returns a subset of the map defined by a starting index and lengthDs\Map::sort— Sorts the map in-place by valueDs\Map::sorted— Returns a copy, sorted by valueDs\Map::sum— Returns the sum of all values in the mapDs\Map::toArray— Converts the map to an arrayDs\Map::union— Creates a new map using values from the current instance and another mapDs\Map::values— Returns a sequence of the map's valuesDs\Map::xor— Creates a new map using keys of either the current instance or of another map, but not of both
Ds\Pair 클래스
Ds\Pair::clear— Removes all valuesDs\Pair::__construct— Creates a new instanceDs\Pair::copy— Returns a shallow copy of the pairDs\Pair::isEmpty— Returns whether the pair is emptyDs\Pair::jsonSerialize— Returns a representation that can be converted to JSONDs\Pair::toArray— Converts the pair to an array
Ds\Set 클래스
Ds\Set::add— Adds values to the setDs\Set::allocate— Allocates enough memory for a required capacityDs\Set::capacity— Returns the current capacityDs\Set::clear— Removes all valuesDs\Set::__construct— Creates a new instanceDs\Set::contains— Determines if the set contains all valuesDs\Set::copy— Returns a shallow copy of the setDs\Set::count— Returns the number of values in the setDs\Set::diff— Creates a new set using values that aren't in another setDs\Set::filter— Creates a new set using a callable to determine which values to includeDs\Set::first— Returns the first value in the setDs\Set::get— Returns the value at a given indexDs\Set::intersect— Creates a new set by intersecting values with another setDs\Set::isEmpty— Returns whether the set is emptyDs\Set::join— Joins all values together as a stringDs\Set::jsonSerialize— Returns a representation that can be converted to JSONDs\Set::last— Returns the last value in the setDs\Set::map— Returns the result of applying a callback to each valueDs\Set::merge— Returns the result of adding all given values to the setDs\Set::reduce— Reduces the set to a single value using a callback functionDs\Set::remove— Removes all given values from the setDs\Set::reverse— Reverses the set in-placeDs\Set::reversed— Returns a reversed copyDs\Set::slice— Returns a sub-set of a given rangeDs\Set::sort— Sorts the set in-placeDs\Set::sorted— Returns a sorted copyDs\Set::sum— Returns the sum of all values in the setDs\Set::toArray— Converts the set to an arrayDs\Set::union— Creates a new set using values from the current instance and another setDs\Set::xor— Creates a new set using values in either the current instance or in another set, but not in both
Ds\Stack 클래스
Ds\Stack::allocate— Allocates enough memory for a required capacityDs\Stack::capacity— Returns the current capacityDs\Stack::clear— Removes all valuesDs\Stack::__construct— Creates a new instanceDs\Stack::copy— Returns a shallow copy of the stackDs\Stack::count— Returns the number of values in the stackDs\Stack::isEmpty— Returns whether the stack is emptyDs\Stack::jsonSerialize— Returns a representation that can be converted to JSONDs\Stack::peek— Returns the value at the top of the stackDs\Stack::pop— Removes and returns the value at the top of the stackDs\Stack::push— Pushes values onto the stackDs\Stack::toArray— Converts the stack to an array
Ds\Queue 클래스
Ds\Queue::allocate— Allocates enough memory for a required capacityDs\Queue::capacity— Returns the current capacityDs\Queue::clear— Removes all valuesDs\Queue::__construct— Creates a new instanceDs\Queue::copy— Returns a shallow copy of the queueDs\Queue::count— Returns the number of values in the queueDs\Queue::isEmpty— Returns whether the queue is emptyDs\Queue::jsonSerialize— Returns a representation that can be converted to JSONDs\Queue::peek— Returns the value at the front of the queueDs\Queue::pop— Removes and returns the value at the front of the queueDs\Queue::push— Pushes values into the queueDs\Queue::toArray— Converts the queue to an array
Ds\PriorityQueue 클래스
Ds\PriorityQueue::allocate— Allocates enough memory for a required capacityDs\PriorityQueue::capacity— Returns the current capacityDs\PriorityQueue::clear— Removes all valuesDs\PriorityQueue::__construct— Creates a new instanceDs\PriorityQueue::copy— Returns a shallow copy of the queueDs\PriorityQueue::count— Returns the number of values in the queueDs\PriorityQueue::isEmpty— Returns whether the queue is emptyDs\PriorityQueue::jsonSerialize— Returns a representation that can be converted to JSONDs\PriorityQueue::peek— Returns the value at the front of the queueDs\PriorityQueue::pop— Removes and returns the value with the highest priorityDs\PriorityQueue::push— Pushes values into the queueDs\PriorityQueue::toArray— Converts the queue to an array
더 알아보기
- 대부분의
Ds\*구조는 배열과 비슷한 느낌으로 쓰되 성능 특징이 달라요. 예를 들어 벡터는 인덱스 접근이 빠르고, 데크는 앞뒤 삽입·삭제가 빨라요. 작업 특성에 맞는 구조를 고르는 게 중요해요. - 성능 비교와 자주 묻는 질문은 공식 블로그 포스트에서 확인할 수 있어요.