static nd::array function_type_construct(const ndt::type& DYND_UNUSED(dt), const nd::array& year, const nd::array& month, const nd::array& day) { // TODO proper buffering nd::array year_as_int = year.ucast(ndt::make_type<int32_t>()).eval(); nd::array month_as_int = month.ucast(ndt::make_type<int32_t>()).eval(); nd::array day_as_int = day.ucast(ndt::make_type<int32_t>()).eval(); nd::array result; array_iter<1,3> iter(ndt::make_date(), result, year_as_int, month_as_int, day_as_int); if (!iter.empty()) { datetime::date_ymd ymd; do { ymd.year = *reinterpret_cast<const int32_t *>(iter.data<1>()); ymd.month = *reinterpret_cast<const int32_t *>(iter.data<2>()); ymd.day = *reinterpret_cast<const int32_t *>(iter.data<3>()); if (!datetime::is_valid_ymd(ymd)) { stringstream ss; ss << "invalid year/month/day " << ymd.year << "/" << ymd.month << "/" << ymd.day; throw runtime_error(ss.str()); } *reinterpret_cast<int32_t *>(iter.data<0>()) = datetime::ymd_to_days(ymd); } while (iter.next()); } return result; }
nd::array dynd::nd::linspace(const nd::array& start, const nd::array& stop, intptr_t count, const ndt::type& dt) { nd::array start_cleaned = start.ucast(dt).eval(); nd::array stop_cleaned = stop.ucast(dt).eval(); if (start_cleaned.is_scalar() && stop_cleaned.is_scalar()) { return linspace(dt, start_cleaned.get_readonly_originptr(), stop_cleaned.get_readonly_originptr(), count); } else { throw runtime_error("dynd::linspace presently only supports scalar parameters"); } }
dynd::busdate_type::busdate_type(busdate_roll_t roll, const bool *weekmask, const nd::array& holidays) : base_type(busdate_type_id, datetime_kind, 4, 4, type_flag_scalar, 0, 0), m_roll(roll) { memcpy(m_workweek, weekmask, sizeof(m_workweek)); m_busdays_in_weekmask = 0; for (int i = 0; i < 7; ++i) { m_busdays_in_weekmask += weekmask[i] ? 1 : 0; } if (!holidays.is_empty()) { nd::array hol = holidays.ucast(ndt::make_date()).eval_immutable(); // TODO: Make sure hol is contiguous and one-dimensional m_holidays = hol; } }
static void json_as_buffer(const nd::array &json, nd::array &out_tmp_ref, const char *&begin, const char *&end) { // Check the type of 'json', and get pointers to the begin/end of a UTF-8 // buffer ndt::type json_type = json.get_type().value_type(); switch (json_type.get_kind()) { case string_kind: { const ndt::base_string_type *sdt = json_type.extended<ndt::base_string_type>(); switch (sdt->get_encoding()) { case string_encoding_ascii: case string_encoding_utf_8: out_tmp_ref = json.eval(); // The data is already UTF-8, so use the buffer directly sdt->get_string_range(&begin, &end, out_tmp_ref.get_arrmeta(), out_tmp_ref.get_readonly_originptr()); break; default: { // The data needs to be converted to UTF-8 before parsing ndt::type utf8_tp = ndt::string_type::make(string_encoding_utf_8); out_tmp_ref = json.ucast(utf8_tp).eval(); sdt = static_cast<const ndt::base_string_type *>(utf8_tp.extended()); sdt->get_string_range(&begin, &end, out_tmp_ref.get_arrmeta(), out_tmp_ref.get_readonly_originptr()); break; } } break; } case bytes_kind: { out_tmp_ref = json.eval(); const ndt::base_bytes_type *bdt = json_type.extended<ndt::base_bytes_type>(); bdt->get_bytes_range(&begin, &end, out_tmp_ref.get_arrmeta(), out_tmp_ref.get_readonly_originptr()); break; } default: { stringstream ss; ss << "Input for JSON parsing must be either bytes (interpreted as UTF-8) " "or a string, not \"" << json_type << "\""; throw runtime_error(ss.str()); break; } } }