The Collection interface

(No version information available, might only be in Git)

소개

Collection is the base interface which covers functionality common to all the data structures in this library. It guarantees that all structures are traversable, countable, and can be converted to json using json_encode().

인터페이스 개요

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 )
}

Table of Contents

add a note add a note

User Contributed Notes 1 note

up
-5
depakin at yisangwu dot com
4 years ago
// Collection
   $collection_a = new \Ds\Vector([1, 2, 3]);
   $collection_b = new \Ds\Vector();

   var_dump($collection_a, $collection_b);
    /*
        object(Ds\Vector)[1]
        public 0 => int 1
        public 1 => int 2
        public 2 => int 3

        object(Ds\Vector)[2]
    */

   //json_encode
   var_dump( json_encode($collection_a));
    /*
    string '[1,2,3]
    */
  
    //count
   var_dump(count($collection_a));
    /*
    int 3
    */
  
    // serialize
    var_dump(serialize($collection_a));
    /*
    string 'C:9:"Ds\Vector":12:{i:1;i:2;i:3;}'
    */

    // foreach
    foreach ($collection_a as $key => $value) {
       echo $key ,'--', $value, PHP_EOL;
    }
    /*
       0--1
       1--2
       2--3
     */

    // clone
    $clone = clone($collection_a);
    var_dump($clone);
    /*
      object(Ds\Vector)[1]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
    */

    // push
    $clone->push('aa');
    var_dump($clone);
    /*
    object(Ds\Vector)[3]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
      public 3 => string 'aa' (length=2)
     */

   // isEmpty
   var_dump($collection_a->isEmpty(), $collection_b->isEmpty());
    /*
    boolean false
    boolean true
     */

   // toArray
   var_dump($collection_a->toArray(), $collection_b->toArray());
    /*
     array (size=3)
      0 => int 1
      1 => int 2
      2 => int 3

    array (size=0)
      empty
    */

   // copy ( void )
   //浅拷贝, shallow copy
   $collection_c = $collection_a->copy();

   var_dump($collection_c);
    /*
    object(Ds\Vector)[3]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
    */

   $collection_c->push(4);
   var_dump($collection_a, $collection_c);
    /*
    object(Ds\Vector)[1]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3

    object(Ds\Vector)[3]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
      public 3 => int 4
    */
  
    // clear
    $collection_a->clear();
    $collection_b->clear();
    $collection_c->clear();

    var_dump($collection_a, $collection_b, $collection_c);
    /*
    object(Ds\Vector)[1]
    object(Ds\Vector)[2]
    object(Ds\Vector)[3]
    */
To Top