1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/*
FALCON - The Falcon Programming Language.
FILE: set_ext.cpp
Implementation of the RTL Set Falcon class.
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: Sat, 08 Aug 2009 14:55:46 +0200
-------------------------------------------------------------------
(C) Copyright 2009: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/*#
@beginmodule core
*/
/** \file
Implementation of the RTL List Falcon class.
*/
#include <falcon/setup.h>
#include <falcon/types.h>
#include <falcon/itemset.h>
#include <falcon/iterator.h>
#include <falcon/vm.h>
#include "core_module.h"
#include <falcon/eng_messages.h>
namespace Falcon {
namespace core {
/*#
@class Set
@from Sequence ...
@brief Storage for uniquely defined items (and ordering criterion).
@param ... An arbitrary list of parameters.
The Set class implements a binary tree, uniquely and orderly storing a
set of generic Falcon items. Instances of the Set
class can be used as parameters for the Iterator constructor, and an iterator
can be generated for them using first() and last() BOM methods. Also,
instances of the Set class can be used as any other sequence in for/in loops.
Items in the set are ordered using the Falcon standard comparison algorithm;
if they are instances of classes (or blessed dictionaries) implementing the
compare() method, that method is used as a comparison criterion.
If the set constructor is given some parameters, it will be initially filled
with those items; if some of them is duplicated, only one item will be then
found in the set.
*/
FALCON_FUNC Set_init ( ::Falcon::VMachine *vm )
{
ItemSet *set = new ItemSet;
int32 pc = vm->paramCount();
for( int32 p = 0; p < pc; p++ )
{
set->insert( *vm->param(p) );
}
set->owner( vm->self().asObject() );
vm->self().asObject()->setUserData( set );
}
/*#
@method insert Set
@brief Adds an item to the set.
@param item The item to be added.
If an item considered equal to the added one exists, the
previously set item is destroyed.
*/
FALCON_FUNC Set_insert ( ::Falcon::VMachine *vm )
{
Item *data = vm->param( 0 );
if( data == 0 )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
origin( e_orig_runtime ).
extra("X") );
return;
}
ItemSet *set = dyncast<ItemSet *>( vm->self().asObject()->getFalconData() );
set->insert( *data );
}
/*#
@method remove Set
@brief Removes an item from a set.
@param item The item to be removed.
@return True if the item was removed, false if it wasn't found.
*/
FALCON_FUNC Set_remove ( ::Falcon::VMachine *vm )
{
Item *data = vm->param( 0 );
if( data == 0 )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
origin( e_orig_runtime ).
extra("X") );
return;
}
ItemSet *set = dyncast<ItemSet *>( vm->self().asObject()->getFalconData() );
ItemSetElement* elem = set->find( *data );
if( elem == 0 )
{
vm->regA().setBoolean( false );
}
else {
set->erase( elem );
vm->regA().setBoolean( true );
}
}
/*#
@method contains Set
@brief Checks if a certain item is in the set.
@param item The item to be found.
@return True if the item is in the set, false otherwise.
*/
FALCON_FUNC Set_contains ( ::Falcon::VMachine *vm )
{
Item *data = vm->param( 0 );
if( data == 0 )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
origin( e_orig_runtime ).
extra("X") );
return;
}
ItemSet *set = dyncast<ItemSet *>( vm->self().asObject()->getFalconData() );
ItemSetElement* elem = set->find( *data );
vm->regA().setBoolean( elem != 0 );
}
/*#
@method find Set
@brief Checks if a certain item is in the set.
@param item The item to be found.
@return True if the item is in the set, false otherwise.
*/
FALCON_FUNC Set_find ( ::Falcon::VMachine *vm )
{
Item *data = vm->param( 0 );
if( data == 0 )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
origin( e_orig_runtime ).
extra("X") );
return;
}
ItemSet *set = dyncast<ItemSet *>( vm->self().asObject()->getFalconData() );
ItemSetElement* elem = set->find( *data );
if( elem != 0 )
{
Iterator* iter = new Iterator(set);
set->getIteratorAt( *iter, elem );
Item *i_iclass = vm->findWKI( "Iterator" );
fassert( i_iclass != 0 && i_iclass->isClass() );
CoreObject* citer = i_iclass->asClass()->createInstance();
// we need this to declare the iterator as a falcon data
citer->setUserData( iter );
vm->retval( citer );
}
else
vm->retnil();
}
/*#
@method len Set
@brief Returns the number of items stored in this set.
@return Count of items in the set.
*/
FALCON_FUNC Set_len( ::Falcon::VMachine *vm )
{
ItemSet *set = dyncast<ItemSet *>( vm->self().asObject()->getFalconData() );
vm->retval( (int64) set->size() );
}
}
}
/* end of set_ext.cpp */
|