>  기사  >  백엔드 개발  >  PHP의 데이터 구조 DS 확장에 대한 자세한 설명

PHP의 데이터 구조 DS 확장에 대한 자세한 설명

墨辰丷
墨辰丷원래의
2018-05-19 13:37:531637검색

아래 편집기는 PHP의 데이터 구조 DS 확장에 대한 기사를 제공합니다. 에디터가 꽤 좋다고 생각해서 지금 공유해서 참고용으로 올려보겠습니다. 편집기를 따라 살펴보겠습니다

PHP7 이상에서만 이 데이터 구조 확장을 설치하고 사용할 수 있습니다. 설치는 비교적 간단합니다.

1. pecl install ds

2 명령을 실행하세요. php.ini ds.so

3. PHP를 다시 시작하거나 구성을 다시 로드하세요

컬렉션 인터페이스: 이 라이브러리의 모든 데이터 구조에 대한 공통 기능을 포함하는 기본 인터페이스입니다. 모든 구조가 json_encode()를 사용하여 탐색 가능하고 계산 가능하며 json으로 변환될 수 있음을 보장합니다.

Ds\Collection implements Traversable , Countable , JsonSerializable {
/* 方法 */
abstract public void clear ( void )
abstract public Ds\Collection copy ( void )
abstract public bool isEmpty ( void )
abstract public array toArray ( void )
}

해시 가능 인터페이스: 객체를 키로 사용할 수 있습니다.
시퀀스 인터페이스:

A 시퀀스는 다음과 같습니다. 몇 가지 특성을 제외하고 1차원 숫자 키 배열과 동일합니다.

값은 항상 [0, 1, 2, …, 크기 - 1]로 색인화됩니다.

값에 액세스만 허용됩니다. ​[0, 크기 - 1] 범위의 인덱스 기준.

사용 사례:

배열을 목록으로 사용하는 모든 곳(키와 관련 없음).

SplDoublyLinkedList 및 SplFixedArray에 대한 더 효율적인 대안.

Vector 클래스:

Vector는 자동으로 늘어나고 줄어드는 연속 버퍼의 값 시퀀스입니다. 가장 효율적인 순차 구조이며 값의 인덱스가 버퍼의 인덱스에 직접 매핑되며 성장 인자는 특정 배수나 지수에 바인딩되지 않습니다. 다음과 같은 장점과 단점이 있습니다.

배열 구문(대괄호)을 지원합니다.

동일한 수의 값에 대해 배열보다 전체 메모리를 적게 사용합니다.

크기가 충분히 낮아지면 할당된 메모리를 자동으로 해제합니다. 2의 거듭제곱일 필요는 없습니다.

get(), set(), push(), pop()은 모두 O(1)입니다.

그러나 Shift(), unshift(), insert() 및 Remove( )는 모두 O(n)입니다.

Ds\Hashable {
/* 方法 */
abstract public bool equals ( object $obj )
abstract public mixed hash ( void )
}

Deque 클래스: DsQueue에서도 사용되는 "double-ended queue"의 약어에는 head와 tail이라는 두 개의 포인터가 있습니다. 포인터는 버퍼의 끝을 "감쌀" 수 있으므로 공간을 확보하기 위해 다른 값을 이동할 필요가 없습니다. 이는 DsVector와 경쟁할 수 없는 매우 빠른 속도를 제공합니다. 단점:

배열 구문(대괄호)을 지원합니다.

동일한 수의 값에 대해 배열보다 전체 메모리를 덜 사용합니다.

크기가 충분히 낮아지면 할당된 메모리를 자동으로 해제합니다.

get(), set( ), push( ), pop(), Shift(), unshift()는 모두 O(1)입니다.

하지만 용량은 2의 거듭제곱이어야 합니다.insert() 및 Remove()는 O(n)입니다.

Map 클래스:

배열과 거의 동일한 키-값 쌍의 연속 모음입니다. 키는 모든 유형이 될 수 있지만 고유해야 합니다. 동일한 키를 사용하여 맵에 추가하면 값이 대체됩니다. 다음과 같은 장점과 단점이 있습니다.

키와 값은 객체를 포함한 모든 유형이 될 수 있습니다.

배열 구문(대괄호)을 지원합니다.

삽입 순서가 유지됩니다.

성능 및 메모리 효율성은 다음과 매우 유사합니다.

크기가 충분히 낮아지면 할당된 메모리를 자동으로 해제합니다.

객체가 키로 사용될 때 배열로 변환할 수 없습니다.

쌍 클래스:

쌍은 DsMap에서 쌍을 이루는 데 사용됩니다.

Ds\Vector::allocate — Allocates enough memory for a required capacity.
Ds\Vector::apply — Updates all values by applying a callback function to each value.
Ds\Vector::capacity — Returns the current capacity.
Ds\Vector::clear — Removes all values.
Ds\Vector::__construct — Creates a new instance.
Ds\Vector::contains — Determines if the vector contains given values.
Ds\Vector::copy — Returns a shallow copy of the vector.
Ds\Vector::count — Returns the number of values in the collection.
Ds\Vector::filter — Creates a new vector using a callable to determine which values to include.
Ds\Vector::find — Attempts to find a value's index.
Ds\Vector::first — Returns the first value in the vector.
Ds\Vector::get — Returns the value at a given index.
Ds\Vector::insert — Inserts values at a given index.
Ds\Vector::isEmpty — Returns whether the vector is empty
Ds\Vector::join — Joins all values together as a string.
Ds\Vector::jsonSerialize — Returns a representation that can be converted to JSON.
Ds\Vector::last — Returns the last value.
Ds\Vector::map — Returns the result of applying a callback to each value.
Ds\Vector::merge — Returns the result of adding all given values to the vector.
Ds\Vector::pop — Removes and returns the last value.
Ds\Vector::push — Adds values to the end of the vector.
Ds\Vector::reduce — Reduces the vector to a single value using a callback function.
Ds\Vector::remove — Removes and returns a value by index.
Ds\Vector::reverse — Reverses the vector in-place.
Ds\Vector::reversed — Returns a reversed copy.
Ds\Vector::rotate — Rotates the vector by a given number of rotations.
Ds\Vector::set — Updates a value at a given index.
Ds\Vector::shift — Removes and returns the first value.
Ds\Vector::slice — Returns a sub-vector of a given range.
Ds\Vector::sort — Sorts the vector in-place.
Ds\Vector::sorted — Returns a sorted copy.
Ds\Vector::sum — Returns the sum of all values in the vector.
Ds\Vector::toArray — Converts the vector to an array.
Ds\Vector::unshift — Adds values to the front of the vector.

세트 클래스: 고유한 값의 시퀀스. 이 구현은 값이 키로 사용되고 매핑된 값이 무시되는 DsMap과 동일한 해시 테이블을 사용합니다. 여기에는 다음과 같은 장점과 단점이 있습니다.

값은 객체를 포함한 모든 유형이 될 수 있습니다.

배열을 지원합니다.

삽입 순서는 유지됩니다.

크기가 충분히 낮아지면 할당된 메모리를 자동으로 해제합니다.

add(), Remove() 및 Contains()는 모두 O(1)입니다.

하지만 push(), pop(), insert(),shift(), unshift()를 지원하지 않습니다. get()은 액세스된 인덱스 이전에 버퍼에 삭제된 값이 있는 경우 O(n)입니다. 1) 그렇지 않은 경우

스택 클래스:

구조 상단에서만 액세스 및 반복을 허용하는 "후입선출" 컬렉션입니다.

Ds\Pair implements JsonSerializable {
/* 方法 */
public __construct ([ mixed $key [, mixed $value ]] )
}

큐 클래스: "선입 선출" 컬렉션은 구조 전면에서만 액세스 및 반복을 허용합니다.

Ds\Stack implements Ds\Collection {
/* 方法 */
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public Ds\Stack copy ( void )
public bool isEmpty ( void )
public mixed peek ( void )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public array toArray ( void )
}

PriorityQueue 클래스: 우선순위 큐는 큐와 매우 유사하지만, 우선순위가 가장 높은 값이 항상 큐의 맨 앞에 위치합니다. 동일한 우선순위 요소 "선입선출" 순서가 여전히 유지됩니다. PriorityQueue에 대한 반복은 파괴적이며 대기열이 비어 있을 때까지 지속적인 팝 작업과 동일합니다. 최대 힙을 사용하여 구현되었습니다.

Ds\Queue implements Ds\Collection {
/* Constants */
const int MIN_CAPACITY = 8 ;
/* 方法 */
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public Ds\Queue copy ( void )
public bool isEmpty ( void )
public mixed peek ( void )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public array toArray ( void )
}

관련 권장 사항:

PHP에서 일반적으로 사용되는 알고리즘 및

데이터 구조

php 기초 1: 배열 및 데이터 구조

Python 내장 데이터 구조자세한 설명

위 내용은 PHP의 데이터 구조 DS 확장에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.