--- parser3/src/classes/hash.C 2021/11/04 21:31:15 1.151 +++ parser3/src/classes/hash.C 2023/09/26 20:49:05 1.157 @@ -1,8 +1,8 @@ /** @file Parser: @b hash parser class. - Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #include "classes.h" @@ -17,7 +17,7 @@ #include "pa_vbool.h" #include "pa_vmethod_frame.h" -volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.151 2021/11/04 21:31:15 moko Exp $"; +volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.157 2023/09/26 20:49:05 moko Exp $"; // class @@ -238,19 +238,46 @@ struct Copy_intersection_to_info { HashStringValue* dest; }; #endif -static void copy_intersection_to(HashStringValue::key_type key, HashStringValue::value_type value, Copy_intersection_to_info *info) { + +static void copy_intersection_by_arg(HashStringValue::key_type key, HashStringValue::value_type, Copy_intersection_to_info *info) { + if(HashStringValue::value_type value=info->b->get(key)) + info->dest->put_dont_replace(key, value); +} + +static void copy_intersection_by_self(HashStringValue::key_type key, HashStringValue::value_type value, Copy_intersection_to_info *info) { if(info->b->get(key)) info->dest->put_dont_replace(key, value); } + static void _intersection(Request& r, MethodParams& params) { Value& result=*new VHash; - // dest += b + + bool order_by_arg=false; + if(params.count()>1) + if(HashStringValue* options=params.as_hash(1, "options")) { + int valid_options=0; + if(Value* vorder=options->get("order")) { + const String &sorder=r.process(*vorder).as_string(); + if(sorder == "arg") + order_by_arg=true; + else if(sorder != "self") + throw Exception(PARSER_RUNTIME, &sorder, "'order' must be 'self' or 'arg'"); + valid_options++; + } + if(valid_options!=options->count()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + } + if(HashStringValue* b=params.as_hash(0, "param")) { - Copy_intersection_to_info info={b, result.get_hash()}; - GET_SELF(r, VHashBase).hash().for_each(copy_intersection_to, &info); + if(order_by_arg){ + Copy_intersection_to_info info={&GET_SELF(r, VHashBase).hash(), result.get_hash()}; + b->for_each(copy_intersection_by_arg, &info); + } else { + Copy_intersection_to_info info={b, result.get_hash()}; + GET_SELF(r, VHashBase).hash().for_each(copy_intersection_by_self, &info); + } } - // return result r.write(result); } @@ -622,6 +649,7 @@ static void _at(Request& r, MethodParams extern String table_reverse_name; static void _select(Request& r, MethodParams& params) { + InCycle temp(r); const String* key_var_name=¶ms.as_string(0, "key-var name must be string"); const String* value_var_name=¶ms.as_string(1, "value-var name must be string"); Value& vcondition=params.as_expression(2, "condition must be number, bool or expression"); @@ -658,6 +686,7 @@ static void _select(Request& r, MethodPa HashStringValue& result_hash=*new HashStringValue(); if(limit>0){ + #ifdef HASH_ORDER if(reverse){ for(HashStringValue::ReverseIterator i(source_hash); i; i.prev()){ @@ -666,7 +695,12 @@ static void _select(Request& r, MethodPa if(value_var_name) r.put_element(caller, *value_var_name, i.value()); - if(r.process(vcondition).as_bool()){ + bool condition=r.process(vcondition).as_bool(); + + if(r.check_skip_break()) + break; + + if(condition){ result_hash.put(i.key(), i.value()); if(!--limit) break; @@ -682,7 +716,12 @@ static void _select(Request& r, MethodPa if(value_var_name) r.put_element(caller, *value_var_name, i.value()); - if(r.process(vcondition).as_bool()){ + bool condition=r.process(vcondition).as_bool(); + + if(r.check_skip_break()) + break; + + if(condition){ result_hash.put(i.key(), i.value()); if(!--limit) break; @@ -728,8 +767,8 @@ static void _rename(Request& r, MethodPa const String& key_to=params.as_string(1, "to key must be string"); hash.rename(key_from, key_to); - } else { - HashStringValue* names=params.as_hash(0); + } else { + HashStringValue* names=params.as_hash(0,"single parameter"); for(HashStringValue::Iterator i(*names); i; i.next()) hash.rename(i.key(), i.value()->as_string()); @@ -749,8 +788,8 @@ MHash::MHash(): Methoded("hash") add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1); // ^a.union[b] = hash add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1); - // ^a.intersection[b] = hash - add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 1); + // ^a.intersection[b][options hash] = hash + add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 2); // ^a.intersects[b] = bool add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1);